Sha256: 8302bdfe4281c2325e3ba3e032b57711cae10d5ad34f062fed9d65cbe53751e3

Contents?: true

Size: 1.28 KB

Versions: 24

Compression:

Stored size: 1.28 KB

Contents

module Scrivito

class ImageTagHelper < Struct.new(:view_context)

  def self.display_title(target)
    if target.respond_to?(:display_title)
      target.display_title
    elsif target.respond_to?(:first) && target.first.respond_to?(:display_title)
      target.first.display_title
    elsif target.respond_to?(:filename)
      target.filename
    end
  end

  def options(obj, field_name, tag_options, editing_options)
    tag_options.reverse_merge(src: scrivito_image_tag_src(obj, field_name, editing_options),
        alt: scrivito_image_tag_alt(obj, field_name))
  end

  private

  def scrivito_image_tag_src(obj, field_name, editing_options)
    scrivito_image_tag_path(obj, field_name) || editing_options[:placeholder] ||
        view_context.image_path('scrivito/image_placeholder.png')
  end

  def scrivito_image_tag_alt(obj, field_name)
    self.class.display_title(obj[field_name])
  end

  def scrivito_image_tag_path(obj, field_name)
    field_type = obj.type_of_attribute(field_name)
    field_value = obj[field_name]

    case field_type
    when 'link', 'binary', 'reference'
      field_value && view_context.scrivito_path(field_value)
    when 'linklist'
      path = view_context.scrivito_path(field_value)
      path == CmsRouting::LINK_TO_EMPTY_LINKLIST ? nil : path
    end
  end

end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
scrivito_sdk-0.41.0.rc1 lib/scrivito/image_tag_helper.rb
scrivito_sdk-0.40.0 lib/scrivito/image_tag_helper.rb
scrivito_sdk-0.40.0.rc2 lib/scrivito/image_tag_helper.rb
scrivito_sdk-0.40.0.rc1 lib/scrivito/image_tag_helper.rb