Sha256: c125eed79b88f4279589227fafbe24f6e2a78468a6579287590ac13d73f5f819

Contents?: true

Size: 1.54 KB

Versions: 20

Compression:

Stored size: 1.54 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.png')
  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)
        .binary_obj_url(obj_or_widget, attribute_value, image_options)
    else
      path = CmsRouting.new(request, main_app, scrivito_engine, image_options)
        .path_or_url(attribute_value, :path)
      path unless path == CmsRouting::LINK_TO_EMPTY_BLOB
    end
  end

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

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
scrivito_sdk-1.0.0 lib/scrivito/image_tag.rb
scrivito_sdk-1.0.0.rc4 lib/scrivito/image_tag.rb
scrivito_sdk-1.0.0.rc3 lib/scrivito/image_tag.rb
scrivito_sdk-1.0.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-1.0.0.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-0.90.0 lib/scrivito/image_tag.rb
scrivito_sdk-0.90.0.rc3 lib/scrivito/image_tag.rb
scrivito_sdk-0.90.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-0.90.0.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-0.70.2 lib/scrivito/image_tag.rb
scrivito_sdk-0.71.2 lib/scrivito/image_tag.rb
scrivito_sdk-0.71.1 lib/scrivito/image_tag.rb
scrivito_sdk-0.70.1 lib/scrivito/image_tag.rb
scrivito_sdk-0.71.0 lib/scrivito/image_tag.rb
scrivito_sdk-0.71.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-0.71.0.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-0.70.0 lib/scrivito/image_tag.rb
scrivito_sdk-0.70.0.rc3 lib/scrivito/image_tag.rb
scrivito_sdk-0.70.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-0.70.0.rc1 lib/scrivito/image_tag.rb