module Scrivito class EditingContext # +options+: # +:selected_workspace_id+: The id of the selected workspace. If empty +published+ workspace is used. # +:display_mode+: Can be +view+, +editing+, +added+ or +deleted+. If empty +view+ is used. # +:editor+: A block, which is lazy evaluated on first access, to determine the editor (details see {Scrivito::Configuation.editing_auth}) # @param [Hash] options def initialize(options = {}) @selected_workspace_id = options[:selected_workspace_id] @display_mode = options[:display_mode] || "view" @editor_callback = options[:editor] || proc { false } end def editor unless @editor_already_evaluated @editor_already_evaluated = true @editor = @editor_callback.call end @editor end # true iff {editor} truish def authenticated_editor? editor.present? end # @return [String] def display_mode if authenticated_editor? && !selected_workspace.published? @display_mode else 'view' end end # defaults to "published" if no workspace with `selected_workspace_id` can be found. # @return [Workspace] def selected_workspace unless @selected_workspace if @selected_workspace_id begin @selected_workspace = Workspace.find(@selected_workspace_id) rescue Scrivito::ResourceNotFound @selected_workspace = Workspace.default end else @selected_workspace = Workspace.default end end @selected_workspace end # when authenticated_editor? return selected_workspace, otherwise published workspace. # @return [Workspace] def visible_workspace unless @visible_workspace if authenticated_editor? if display_mode == 'deleted' @visible_workspace = Workspace.default else @visible_workspace = selected_workspace end else @visible_workspace = Workspace.default end end @visible_workspace end def comparison @comparison ||= begin revision = compare_revision Comparison.new( revision, display_mode == 'deleted', revision ? display_mode : nil ) end end def to_params { '_scrivito_display_mode' => display_mode, '_scrivito_workspace_id' => selected_workspace.id, } end private # @return [Revision] or +nil+ def compare_revision case display_mode when 'added' selected_workspace.base_revision when 'deleted' selected_workspace.revision when 'diff' selected_workspace.base_revision end end end end # module Scrivito