Sha256: 7e3ba3e5612e6ca3f527f28bc21337e68fb5cb6c113b7529d5216048c555448b

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

require 'active_support/core_ext/object/blank'

module HisrcRails
  module ResponsiveImageTagHelper
    # Returns a hisrc-ready html image tag for the +src+.
    # If not otherwise specified, it will add two data attributes
    # which are required for hisrc to work.
    #
    # ==== Options
    # +responsive_image_tag+ accepts the same options as +image_tag+,
    # and two additional options as well:
    #
    # * <tt>:'1x'</tt> - If no 1x option is provided, the +src+ is used.
    # * <tt>:'2x'</tt> - If no 2x option is provided, "@2x" is inserted into
    # the +src+. So "rails.png" becomes "rails@2x.png".
    #
    # ==== Examples
    #  responsive_image_tag("rails.png") # =>
    #    <img src="/assets/rails.png" data-1x="/assets/rails.png" data-2x="/assets/rails@2x.png" />
    #  responsive_image_tag("http://placehold.it/100x100", :'1x' => "http://placehold.it/200x200", :'2x' => "http://placehold.it/400x400") # =>
    #    <img src="http://placehold.it/100x100" data-1x="http://placehold.it/200x200" data-2x="http://placehold.it/200x200" />
    def responsive_image_tag(src, options = {})
      options[:data] ||= {}
      options[:data][:'1x'] ||= path_to_image(options.delete(:'1x').presence || options.delete('1x').presence || src)
      options[:data][:'2x'] ||= path_to_image(options.delete(:'2x').presence || options.delete('2x').presence || src.gsub(/([\w\/]+)\.(\w+)$/, '\1@2x.\2'))
      
      image_tag(src, options)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hisrc-rails-2.0.0 lib/hisrc-rails/responsive_image_tag_helper.rb
hisrc-rails-0.3.0 lib/hisrc-rails/responsive_image_tag_helper.rb
hisrc-rails-0.2.0 lib/hisrc-rails/responsive_image_tag_helper.rb