Sha256: 729d5ff7b1bb9bbe9993823905d390c017ad19c10412d0ae3afc987dd126bc74

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 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 both width and height attr values are specified then it will build the :size option from them.
    # Other html attributes will be passed into image_tag options.
    class Image_tag < MasterView::DirectiveBase
      IMAGE_SRC_EXTRACT_REGEX = /public\/images\/(.*)/

      def stag(dcs)
      end

      def etag(dcs)
        src = attrs_lck['src']
        if attr_value.empty? && src
          match = IMAGE_SRC_EXTRACT_REGEX.match(src)
          image_loc = (match) ? match[1] : src
        else
          image_loc = attr_value
        end

        image_tag_params = quote(image_loc)
        attrs.delete('src') # delete this if exists, path has already been specified

        width = attrs_lck['width']
        height = attrs_lck['height']
        if width && height
          attrs.delete('width') #delete these since being used in size
          attrs.delete('height')
          attrs[:size] = "#{width}x#{height}"
        end

        options = symbolize_sort_and_serialize_hash_to_str(attrs)
        image_tag_params += (', '+options) unless options.empty?
        
        erb_content('image_tag ' + image_tag_params)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
masterview-0.2.1 lib/masterview/directives/image_tag.rb
masterview-0.2.2 lib/masterview/directives/image_tag.rb