module RailsConnector # This module contains helpers that can be used to reference images and other assets stored in the CMS. # # @api public module CmsAssetHelper # Calculates an HTML image tag for an image stored in the CMS. # # @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. # # @overload cms_image_tag target, tag_options={} # @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). # # @param [Obj, Link] target Target containing image stored in CMS. # @param [Hash] tag_options Additional HTML attributes for the tag. # # @example # cms_image_tag @target # cms_image_tag @target, alt: 'Interesting picture', class: 'my_image' # # @overload cms_image_tag obj, linklist, tag_options={}, editing_options={} # @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. # # @param [Obj] obj Obj with an attribute of type {LinkList}. # @param [String, Symbol] linklist Name of {LinkList} attribute, which contains the image. # @param [Hash] tag_options Additional HTML attributes for the tag. # # @param [Hash] editing_options Additional options for inplace editing. # @option editing_options [String] :placeholder ('/assets/rails_connector/image_placeholder.png') URL or path to image to be displayed if target is missing. # # @example # 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' # # @return [String] HTML image tag # @api public 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 private def display_title(target) if target.respond_to?(:display_title) return target.display_title elsif target.respond_to?(:first) && target.first.respond_to?(:display_title) return target.first.display_title end end end end