Sha256: 03c115c930cd334af1bb9ae07b7813ff3193e850ba2eecdcc4a291919ca50588

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

module Scrivito

class WidgetTag < Struct.new(:view, :widget, :placement_modification, :template_name, :inner_tag)
  DEFAULT_TAG = "div"

  include TagRenderer

  def render
    super
  rescue => error
    if !['development', 'test'].include?(Rails.env) &&
        view.controller.respond_to?(:on_scrivito_widget_error)
      view.controller.on_scrivito_widget_error(widget, error)
    else
      raise error
    end
  end

  def tag_name
    inner_tag || DEFAULT_TAG
  end

  def content
    view.render(template: template_path, locals: {widget: widget})
  rescue ActionView::MissingTemplate
    raise if Rails.env.development? || Rails.env.test?
    "Missing show template for #{widget.description_for_editor}" if view.scrivito_user
  end

  def options
    options = {}

    if inplace_editing_allowed?
      options['private-widget-id'] = widget.id
      options['widget-obj-class'] = widget.obj_class_name
      options['private-widget-description-for-editor'] = widget.description_for_editor

      if has_widget_details_view?(widget)
        options['private-widget-has-details-view'] = true
      end

      comparison = editing_context.comparison
      options['private-widget-modification'] =
        if comparison.revision == widget.revision
          if placement_modification
            unless widget.in_revision(Workspace.current.revision)
              Modification::DELETED
            end
          end
        else
          comparison.modification(widget)
        end

      options['private-widget-placement-modification'] =
        placement_modification
    end

    options
  end

  private

  def template_path
    widget.public_send("to_#{template_name}_view_path")
  end

  def has_widget_details_view?(widget)
    view.lookup_context.find(widget.to_details_view_path).present?
  rescue ActionView::MissingTemplate
    false
  end

  def inplace_editing_allowed?
    editing_context.authenticated_editor?
  end

  def editing_context
    EditingContextMiddleware.from_request(view.request)
  end

end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
scrivito_sdk-0.70.2 lib/scrivito/widget_tag.rb
scrivito_sdk-0.70.1 lib/scrivito/widget_tag.rb
scrivito_sdk-0.70.0 lib/scrivito/widget_tag.rb
scrivito_sdk-0.70.0.rc3 lib/scrivito/widget_tag.rb
scrivito_sdk-0.70.0.rc2 lib/scrivito/widget_tag.rb
scrivito_sdk-0.70.0.rc1 lib/scrivito/widget_tag.rb