Sha256: d8ff1c6c5989306b63c79e39eaf6ccc2268a54040733f44624958d59d2669d76

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

module Scrivito

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

  private

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

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

  def path(obj, attribute_name, editing_options)
    if target = target(obj, attribute_name, editing_options)
      image_options = editing_options.slice(:transform)
      cms_routing = CmsRouting.new(request, main_app, scrivito_engine, image_options)
      cms_routing.path_or_url(target, :path)
    end
  end

  def target(obj, attribute_name, editing_options)
    target = obj[attribute_name]
    target = link_target(target) if target.is_a?(Link)
    target = linklist_target(target) if target.is_a?(Enumerable)
    target
  end

  def link_target(link)
    link.internal? ? link.obj : link
  end

  def linklist_target(linklist)
    if linklist.any?
      link_target(linklist.first)
    end
  end

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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scrivito_sdk-0.66.0 lib/scrivito/image_tag.rb
scrivito_sdk-0.66.0.rc2 lib/scrivito/image_tag.rb
scrivito_sdk-0.66.0.rc1 lib/scrivito/image_tag.rb