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}) # +:workspace_changed+: A boolean, determining if the workspace has been changed with this request. # @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 } @workspace_changed = !!options[:workspace_changed] 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 def workspace_changed? @workspace_changed 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 [Scrivito::Workspace] def selected_workspace @selected_workspace ||= find_selected_workspace end # when authenticated_editor? return selected_workspace, otherwise published workspace. # @return [Scrivito::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 workspace = begin Workspace.find(@selected_workspace_id) rescue Rails.logger.info "Workspace with ID #{@selected_workspace_id} is " + "not available, using 'published' instead." published_workspace end if editor && editor.can?(:read, workspace) workspace else published_workspace end else published_workspace end end def find_visible_workspace if authenticated_editor? display_mode == 'deleted' ? published_workspace : selected_workspace else published_workspace end end def published_workspace Workspace.published end # @return [Scrivito::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