module Scrivito class ObjsController < WebserviceController around_action :require_selected_workspace_write_authorization, only: [ :copy, :create, :destroy, :destroy_widget, :duplicate, :mark_resolved, :restore, :restore_widget, :revert, :revert_widget, :update, ] before_filter :require_identical_selected_and_visible_workspace, only: [ :copy, :create, :duplicate, :page_class_selection, :update, :widget_class_selection, ] def show in_selected_workspace { render } end def widget in_selected_workspace { render } end def create params_parser = ObjCreateParamsParser.new(request.host, request.port) @obj = Obj.create(params_parser.parse(params[:obj])) render :obj rescue ObjClassNotFound head :not_found end def details assert_dialog_layout render current_obj.details_view_path, layout: 'scrivito_dialog', formats: :html end def update params_parser = ObjUpdateParamsParser.new(current_obj, request.host, request.port) current_obj.update(params_parser.parse(params[:obj])) end def destroy in_selected_workspace do @redirect_to = scrivito_path(current_obj.parent || Obj.homepage) current_obj.destroy end end def destroy_widget in_selected_workspace { current_widget.destroy } render_empty_json end def revert in_selected_workspace { current_obj.revert } render_empty_json end def revert_widget in_selected_workspace { current_widget.revert } render_empty_json end def restore in_selected_workspace { Obj.restore(params[:id]) } render_empty_json end def restore_widget in_selected_workspace { current_obj.restore_widget(params[:widget_id]) } render_empty_json end def mark_resolved in_selected_workspace { current_obj.mark_resolved } render_empty_json end def copy @obj = copy_obj(current_obj, params[:parent_path]) render :obj end def duplicate @obj = copy_obj(current_obj, current_obj.parent_path) render :obj end def page_class_selection @page_class_markup = valid_page_classes.map do |page_class_name| begin markup = render_to_string("#{page_class_name.underscore}/thumbnail", formats: :html) {name: page_class_name, markup: markup} rescue ActionView::MissingTemplate end end @page_class_markup.compact! end def widget_class_selection load_obj @widgets_classes = valid_widget_classes.map do |widget_class_name| template_path = "#{widget_class_name.underscore}/thumbnail" markup = begin render_to_string(template_path, layout: false, formats: :html) rescue ActionView::MissingTemplate render_to_string('scrivito/widget_thumbnail', layout: false, formats: :html, locals: {widget_class_name: widget_class_name, template_path: template_path}) end {name: widget_class_name, markup: markup} end end def search in_selected_workspace do @query = MultiJson.decode(params[:query]).with_indifferent_access @enumerator = ObjSearchBuilder.new(@query).build if params[:query_action] == 'size' render :search_only_size elsif @formatter = fetch_formatter(@query[:format]) render :search else render :format_missing_error, status: :not_found end end end private def require_identical_selected_and_visible_workspace if selected_workspace != editing_context.visible_workspace raise ScrivitoError, 'selected and visible workspace are not identical' end end def require_selected_workspace_write_authorization(&block) authorize_workspace_access(:write, selected_workspace, &block) end def in_selected_workspace(&block) selected_workspace.as_current(&block) end def selected_workspace editing_context.selected_workspace end def current_obj @obj || load_obj end helper_method :current_obj def load_obj @obj = Obj.find(params[:id]) end def current_widget @widget || load_widget end helper_method :current_widget def load_widget raise ScrivitoError, 'no widget_id param specified' if params[:widget_id].blank? unless @widget = current_obj.widget_from_pool(params[:widget_id]) raise ScrivitoError, "widget with ID '#{params[:widget_id]}' not found" end @widget end def copy_obj(obj, parent_path) id = SecureRandom.hex(8) obj.copy(_id: id, _path: parent_path && "#{parent_path}/#{id}") end def fetch_formatter(name) name ? Configuration.obj_formats[name] : proc { |obj, _| obj.id } end def valid_page_classes (page_classes_from_obj || page_descendants).map(&:to_s) end def page_classes_from_obj Obj.valid_page_classes_beneath(params[:parent_path]) end def page_descendants Scrivito.models.pages end def valid_widget_classes current_widget_or_obj.valid_widget_class_names_for(params[:field_name]) - ['Scrivito::ContentWidget'] end def current_widget_or_obj widget_id = params[:widget_id] (widget_id && current_obj.widgets[widget_id]) || current_obj 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