Sha256: a37c84703a9edbf26a6ba75644012bcd6104ab2be0311d0dcd738f1cb7ab22d8

Contents?: true

Size: 1.53 KB

Versions: 24

Compression:

Stored size: 1.53 KB

Contents

module Scrivito

class ImageTag < Struct.new(:view_context)
  def options(obj_or_widget, attribute_name, tag_options, editing_options)
    tag_options.reverse_merge(
      src: src(obj_or_widget, attribute_name, editing_options),
      alt: alt(obj_or_widget, attribute_name)
    )
  end

  private

  def src(obj_or_widget, attribute_name, editing_options)
    path(obj_or_widget, attribute_name, editing_options) ||
      editing_options[:placeholder] ||
      view_context.image_path('scrivito/image_placeholder.gif')
  end

  def alt(obj_or_widget, attribute_name)
    case target = obj_or_widget[attribute_name]
    when Binary then obj_or_widget.try(:alt_description)
    when Enumerable then target.first.try(:alt_description)
    else target.try(:alt_description)
    end
  end

  def path(obj_or_widget, attribute_name, editing_options)
    attribute_value = obj_or_widget[attribute_name]
    return if attribute_value.blank?

    image_options = editing_options.slice(:transform)

    if attribute_value.is_a?(Binary)
      if obj_or_widget.is_a?(Widget)
        raise ScrivitoError, 'scrivito_image_tag does not accept widgets with binary attributes'
      end
      BinaryRouting.new(request, scrivito_engine)
        .transformed_binary_url(attribute_value, :path, image_options)
    else
      path = CmsRouting.new(request, view_context, scrivito_engine, image_options)
        .path_or_url(attribute_value, :path)
      path unless path == CmsRouting::LINK_TO_EMPTY_BLOB
    end
  end

  delegate :request, :scrivito_engine, to: :view_context
end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
scrivito_sdk-1.12.0.rc2 app/cms/scrivito/image_tag.rb
scrivito_sdk-1.12.0.rc1 app/cms/scrivito/image_tag.rb
scrivito_sdk-1.11.0 app/cms/scrivito/image_tag.rb
scrivito_sdk-1.11.0.rc1 app/cms/scrivito/image_tag.rb