Module: RailsAmp::ViewHelpers::ImageTagHelper

Defined in:
lib/rails_amp/view_helpers/image_tag_helper.rb

Constant Summary collapse

AMP_IMG_PERMITTED_ATTRIBUTES =
%w[
  src srcset alt attribution height width
  fallback heights layout media noloading on placeholder sizes
  class
].freeze

Instance Method Summary collapse

Instance Method Details

#amp_image_tag(source, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_amp/view_helpers/image_tag_helper.rb', line 14

def amp_image_tag(source, options={})
  options = options.symbolize_keys
  check_for_image_tag_errors(options) if defined?(check_for_image_tag_errors)

  src = options[:src] = path_to_image(source, skip_pipeline: options.delete(:skip_pipeline))

  unless src.start_with?("cid:") || src.start_with?("data:") || src.blank?
    options[:alt] = options.fetch(:alt){ image_alt(src) }
  end

  options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size]

  if options[:width].blank? || options[:height].blank?
    options[:width], options[:height] = compute_image_size(source)
  end

  options[:layout] ||= 'fixed'
  options.select! { |key, _| key.to_s.in?(AMP_IMG_PERMITTED_ATTRIBUTES) }
  tag('amp-img', options) + '</amp-img>'.html_safe
end

#image_tag(source, options = {}) ⇒ Object

override image_tag helper in ActionView::Helpers::AssetTagHelper



36
37
38
39
40
41
42
# File 'lib/rails_amp/view_helpers/image_tag_helper.rb', line 36

def image_tag(source, options={})
  if controller && RailsAmp.amp_renderable?(controller.controller_path, controller.action_name)
    amp_image_tag(source, options)
  else
    super
  end
end