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 +display_title+ of the target object. # 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) # Backwards compatibility. target = args.first tag_options = args.second || {} tag_options.symbolize_keys! tag_options[:src] = cms_path(target) tag_options[:alt] ||= display_title(target) tag('img', tag_options) else obj = args.first field_name = args.second tag_options = args.third || {} editing_options = args.fourth || {} cms_tag('img', obj, field_name, cms_image_tag_options(obj, field_name, tag_options.symbolize_keys, editing_options.symbolize_keys)) end end private def cms_image_tag_options(obj, field_name, tag_options, editing_options) tag_options.reverse_merge(src: cms_image_tag_src(obj, field_name, editing_options), alt: cms_image_tag_alt(obj, field_name)) end def cms_image_tag_src(obj, field_name, editing_options) cms_image_tag_path(obj, field_name) || editing_options[:placeholder] || image_path('rails_connector/image_placeholder.png') end def cms_image_tag_alt(obj, field_name) display_title(obj[field_name]) end def cms_image_tag_path(obj, field_name) field_type = obj.type_of_attribute(field_name) field_value = obj[field_name] case field_type when 'reference' then field_value && cms_path(field_value) when 'linklist' path = cms_path(field_value) path == DefaultCmsRoutingHelper::LINK_TO_EMPTY_LINKLIST ? nil : path end end 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