Sha256: b1611938f244c71c2b8934e423b8474c3b0f4bf50f3eda2eaa18020dd9f2a85f

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 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)
    if field_name.nil?
      field_name = determine_field_name(obj)
    end

    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 determine_field_name(obj)
    if obj.binary?
      :blob
    else
      raise ScrivitoError, "when omitting `field_name' you have to pass a binary obj"
    end
  end

  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

1 entries across 1 versions & 1 rubygems

Version Path
scrivito_sdk-0.30.0 lib/scrivito/image_tag_helper.rb