Sha256: a7b465c9227a6ca247e521b46670486ffce5fc22c002a264f85ed9f3be51156f

Contents?: true

Size: 1.53 KB

Versions: 9

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.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, 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

9 entries across 9 versions & 1 rubygems

Version Path
scrivito_sdk-1.2.0 lib/scrivito/image_tag.rb
scrivito_sdk-1.2.0.rc3 lib/scrivito/image_tag.rb
scrivito_sdk-1.2.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-1.2.0.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-1.1.1 lib/scrivito/image_tag.rb
scrivito_sdk-1.1.0 lib/scrivito/image_tag.rb
scrivito_sdk-1.1.0.rc3 lib/scrivito/image_tag.rb
scrivito_sdk-1.1.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-1.1.0.rc1 lib/scrivito/image_tag.rb