Sha256: 87ac8eb537b50adb2685b4462bc83f39f5cb49b8bc7915bc6a9925f62e03b059

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module MasterView
  module Directives

    # creates a image_tag.
    # if path is specified in attr_value then it will be used exactly as listed, otherwise if attr_value is empty
    # then masterview will attempt to find the public/images in the string and infer the path from what follows.
    # otherwise it will simply use the src path.
    # If width and/or height attr values are specified in the html then they will be appended to the output of the
    # image tag helper. These are used instead of the size attribute because they are more flexible and a direct mapping.
    # Other html attributes will be passed into image_tag options.
    class ImageTag < MasterView::DirectiveBase

      # /public\/images\/(.*)/
      IMAGE_SRC_EXTRACT_REGEX = MasterView::ConfigSettings.template_asset_base_ref_pattern[:images]

      metadata :priority => :default,
        :category => 'asset',
        :description => 'Replaces the element using the Rails image_tag helper.',
        :element_usage => 'img'

      attr_arg :src
      attr_arg :options, :append_element_attrs => [:common_html]

      event :element do
        if (@src.nil? or @src.empty?) and src_attr = element_attrs[:src]
          if match = IMAGE_SRC_EXTRACT_REGEX.match(src_attr)
            @src = match[1]
          else
            @src = src_attr
          end
        end
        @src = quote(@src) unless @src.nil?
        render erb_content('image_tag', :src, :options)
      end

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
masterview-0.3.2 lib/masterview/directives/image_tag.rb
masterview-0.3.1 lib/masterview/directives/image_tag.rb
masterview-0.3.3 lib/masterview/directives/image_tag.rb
masterview-0.3.4 lib/masterview/directives/image_tag.rb