Module: RailsConnector::CmsAssetHelper

Defined in:
app/helpers/rails_connector/cms_asset_helper.rb

Overview

This module contains helpers that can be used to reference images and other assets stored in the CMS.

Instance Method Summary collapse

Instance Method Details

#cms_image_tag(target, tag_options = {}) ⇒ String #cms_image_tag(obj, linklist, tag_options = {}, editing_options = {}) ⇒ String

Note:

There are two different signatures of this method: the first one generates an HTML image tag with no inplace editing possible, the second one generated an HTML image tag for inplace editing.

Calculates an HTML image tag for an image stored in the CMS.

Overloads:

  • #cms_image_tag(target, tag_options = {}) ⇒ String
    Note:

    If you do not specify an HTML alt attribute, the helper method will use target‘s display_title.

    Calculates HTML image tag (no inplace editing possible).

    Examples:

    cms_image_tag @target
    cms_image_tag @target, alt: 'Interesting picture', class: 'my_image'
    

    Parameters:

    • target (Obj, Link)

      Target containing image stored in CMS.

    • tag_options (Hash) (defaults to: {})

      Additional HTML attributes for the tag.

  • #cms_image_tag(obj, linklist, tag_options = {}, editing_options = {}) ⇒ String
    Note:

    If you do not specify an HTML alt attribute, the helper method will use obj‘s display_title.

    Calculates HTML image tag for inplace editing.

    Examples:

    cms_image_tag @obj, :my_linklist
    cms_image_tag @obj, :my_linklist, alt: 'Interesting picture', class: 'my_image'
    cms_image_tag @obj, :my_linklist, {}, placeholder: image_path('my_placeholder.png')
    cms_image_tag @obj, :my_linklist, {class: 'my_image'}, placeholder: 'http://placehold.it/350x150'
    

    Parameters:

    • obj (Obj)

      Obj with an attribute of type LinkList.

    • linklist (String, Symbol)

      Name of LinkList attribute, which contains the image.

    • tag_options (Hash) (defaults to: {})

      Additional HTML attributes for the tag.

    • editing_options (Hash) (defaults to: {})

      Additional options for inplace editing.

    Options Hash (editing_options):

    • :placeholder (String) — default: '/assets/rails_connector/image_placeholder.png'

      URL or path to image to be displayed if target is missing.

Returns:

  • (String)

    HTML image tag



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/rails_connector/cms_asset_helper.rb', line 43

def cms_image_tag(*args)
  if args.second.nil? || args.second.is_a?(Hash)
    target = args.first
    if target.is_a?(LinkList)
      ActiveSupport::Deprecation.warn(%{
        Calling "cms_image_tag" with a "LinkList" is not allowed anymore.
        Please use following syntax instead: cms_image_tag(@obj, :linklist).
      })
    end

    tag_options = args.second || {}
    tag_options.symbolize_keys!
    tag_options[:src] = cms_path(target)
    tag_options[:alt] ||= display_title(target)
  else
    obj = args.first
    attribute_name = args.second
    target = obj[attribute_name]
    tag_options = args.third || {}
    editing_options = args.fourth || {}

    tag_options.symbolize_keys!
    editing_options.symbolize_keys!

    tag_options[:src] ||= begin
      target_path = cms_path(target)
      if target_path == RailsConnector::DefaultCmsRoutingHelper::LINK_TO_EMPTY_LINKLIST
        editing_options[:placeholder] || image_path('rails_connector/image_placeholder.png')
      else
        target_path
      end
    end

    if inplace_editing_allowed?
      tag_options.merge!(
        'data-ip-resource-source-obj-id' => obj.id,
        'data-ip-resource-source-field-name' => attribute_name
      )
    end

    tag_options.reverse_merge!(alt: display_title(target))
  end

  tag('img', tag_options)
end