Sha256: 34978d0309ea0f5e70e6a01ec4e4eac595ebd3cf1a00dc89cd8f62a139ab0bc9

Contents?: true

Size: 972 Bytes

Versions: 1

Compression:

Stored size: 972 Bytes

Contents

module AmpImageTagHelper
  def amp_image_tag(source, opts = {})
    has_dimensions = (opts[:width] && opts[:height]) || opts[:size]
    set_dimensions(source, opts) if !has_dimensions
    set_scrset(source, opts)
    image_tag_to_amp(image_tag(source, opts))
  end

  private

  def image_tag_to_amp(img_tag)
    img_tag.gsub(/^<img/, '<amp-img').gsub(/>$/, '></amp-img>').html_safe
  end

  def set_dimensions(source, opts)
    if source.kind_of?(CarrierWave::Uploader::Base) &&
       !source.class.processors.empty?
      opts[:width], opts[:height] = source.class.processors[0][1]
    else
      opts[:width], opts[:height] = FastImage.size(source.to_s)
    end
  end

  def set_scrset(source, opts)
    if source.kind_of?(CarrierWave::Uploader::Base) &&
       !source.class.processors.empty? &&
       format_2x = opts.delete(:format_2x)
      name_2x = format_2x % source.version_name
      opts[:srcset] = source.parent_version.send(name_2x).url
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amp_helper-0.1.1 lib/amp_helper/amp_image_tag_helper.rb