Sha256: 87e68f9f2a53a14f6942078d5737148b22142ac6cf2490022bba7a5db9c1743e

Contents?: true

Size: 1.53 KB

Versions: 54

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

54 entries across 54 versions & 1 rubygems

Version Path
scrivito_sdk-1.10.0 app/cms/scrivito/image_tag.rb
scrivito_sdk-1.10.0.rc5 app/cms/scrivito/image_tag.rb
scrivito_sdk-1.10.0.rc4 app/cms/scrivito/image_tag.rb
scrivito_sdk-1.10.0.rc3 app/cms/scrivito/image_tag.rb
scrivito_sdk-1.10.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-1.10.0.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-1.9.1 lib/scrivito/image_tag.rb
scrivito_sdk-1.9.1.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-1.9.0 lib/scrivito/image_tag.rb
scrivito_sdk-1.9.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-1.9.0.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-1.8.1 lib/scrivito/image_tag.rb
scrivito_sdk-1.8.1.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-1.8.0 lib/scrivito/image_tag.rb
scrivito_sdk-1.8.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-1.8.0.rc1 lib/scrivito/image_tag.rb
scrivito_sdk-1.7.0 lib/scrivito/image_tag.rb
scrivito_sdk-1.7.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-1.5.5 lib/scrivito/image_tag.rb
scrivito_sdk-1.6.1 lib/scrivito/image_tag.rb