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::Configuration.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 if @editor && !@editor.is_a?(Scrivito::User) raise ScrivitoError.new( "The editing auth callback has to return a Scrivito::User or falsy."+ " To upgrade please return Scrivito::User.define(id) and set the permissions" + " of the user. See the documentation for further details." ) end 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 @selected_workspace ||= find_selected_workspace end # when authenticated_editor? return selected_workspace, otherwise published workspace. # @return [Workspace] def visible_workspace @visible_workspace ||= find_visible_workspace end def comparison @comparison ||= begin revision = compare_revision Comparison.new( revision, display_mode == 'deleted', revision ? display_mode : nil ) end end def editable_display_mode? display_mode == 'editing' end def to_params { '_scrivito_display_mode' => display_mode, '_scrivito_workspace_id' => selected_workspace.id, } end private def find_selected_workspace if @selected_workspace_id begin workspace = Workspace.find(@selected_workspace_id) if editor && editor.can?(:read, workspace) workspace else default_workspace end rescue Scrivito::ResourceNotFound default_workspace end else default_workspace end end def find_visible_workspace if authenticated_editor? display_mode == 'deleted' ? default_workspace : selected_workspace else default_workspace end end def default_workspace Workspace.default end # @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