module Scrivito # # This module provides CMS controller actions. # Include it in controller you want to make able to deliver +Obj+s. # # @api public # # @see Scrivito::TestRequest#for_scrivito_obj # # @example # class PageCmsController < ApplicationController # include Scrivito::ControllerActions # end # module ControllerActions extend ActiveSupport::Concern included do before_filter :require_authenticated_editor, only: [ :details_page, :show_widget, :widget_details, ] before_filter :load_object end # # Default action. # Delivers files directly if +Obj+ is binary. # Otherwise the view is rendered. # # @api public # def index deliver_file if @obj.binary? end def show_widget widget = load_widget render text: Scrivito::WidgetTag.new(view_context, widget).render, layout: false end def widget_details assert_dialog_layout widget = load_widget template_path = "#{widget.obj_class_name.underscore}/details" render template_path, layout: 'scrivito_dialog', locals: {widget: widget} end def details_page assert_dialog_layout @scrivito_resource = editing_context.selected_workspace.objs .find_including_deleted(params[:resource_id]) render text: '', layout: 'scrivito_dialog' end module ClassMethods # # This method indicates if this controller should be used automatically when an +Obj+ is # requested via the SDK's standard routes. It returns +true+ by default. # # Overwrite it to return +false+ if you do want your controller to be excluded from +Obj+ # dispatching. # # @api public # @see Obj#controller_name # def use_for_obj_dispatch? true end end private def require_authenticated_editor head(:forbidden) unless editing_context.authenticated_editor? end def editing_context request.env[EditingContextMiddleware::ENVKEY] || EditingContext.new end delegate :comparison, to: :editing_context def load_object CmsEnv.new(request.env).load loaded_obj = request.env[CmsEnv::OBJ_ENV_KEY] raise loaded_obj if loaded_obj.is_a?(StandardError) @obj = loaded_obj end def load_widget if widget = @obj.widget_from_pool(params[:widget_id]) widget elsif comparison.active? # The "diff" mode. @obj.in_revision(editing_context.selected_workspace.base_revision) .widget_from_pool(params[:widget_id]) end end # # Deliver a binary +Obj+ by redirecting to its {BasicObj#binary Obj#binary}'s url. # Will respond with 404 if the +Obj+ has no +blob+. # # @api public # def deliver_file if url = @obj.binary_url redirect_to CmsRouting.match_protocol(url, request) else render text: 'Empty Blob', status: 404 end end def assert_dialog_layout view_context.lookup_context.find('layouts/scrivito_dialog') rescue ActionView::MissingTemplate raise %{ Missing the Scrivito dialog layout! Scrivito requires a special view layout in order to render the details dialog. Normally the install generator places it in `app/views/layouts/scrivito_dialog.html.erb`. If upgrading Scrivito, please re-run the install generator: `rails g scrivito:install`. } end end end