Module: Www::ImagesHelper

Includes:
ActionView::Helpers::TagHelper
Included in:
ApplicationHelper, Liquid::Tags::Image, FullWidthLandingPageHeaderComponent
Defined in:
app/helpers/www/images_helper.rb

Instance Method Summary collapse

Instance Method Details

#image_asset_tag(image_or_id, options = {}) ⇒ Object

Creates an image tag based on a digital asset image or loads up an image based on the friendly id
Pass options supported by image url, see image.rb#image_url
class, alt, itemprop all supported
lazyload: true/false to set all the lazy class
sizes: override the sizes attribute otherwise default to auto
include_srcset: true/false, default false, generates a srcset



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/www/images_helper.rb', line 21

def image_asset_tag(image_or_id, options = {})
  return if image_or_id.blank?

  link_preload = options.delete(:link_preload)
  image = get_image(image_or_id, options)
  image_tag_html = Www::ImagePresenter.new(image, self).image_tag(options)

  # get the srcset and sizes
  # Skip preloading in development to avoid warnings
  if link_preload && !Rails.env.development? && img_tag = Nokogiri::HTML(image_tag_html).css('img').try(:[], 0)
    image_srcset = img_tag['srcset']
    image_href = img_tag['src']
    image_sizes = img_tag['sizes']
    image_sizes = nil if image_sizes == 'auto' # Remove legacy lazysizes 'auto' value
    image_sizes ||= '100vw'
    fetchpriority = img_tag['fetchpriority'].presence

    if respond_to?(:response) && response
      preload_link = "<#{image_href}>; rel=preload; as=image; imagesrcset=\"#{image_srcset}\"; imagesizes=\"#{image_sizes}\""
      response.headers['Link'] = [response.headers['Link'].presence, preload_link].compact.join(', ')
    else
      content_for(:preloads) do
        tag.link(rel: 'preload', as: 'image', href: image_href, imagesrcset: image_srcset,
                 imagesizes: image_sizes, fetchpriority: fetchpriority)
      end
    end
  end
  image_tag_html
end

#image_asset_url(image_id, options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'app/helpers/www/images_helper.rb', line 7

def image_asset_url(image_id, options = {})
  return if image_id.blank?

  image = image_id if image_id.is_a?(Image)
  image ||= get_image(image_id, options)
  image.image_url(options)
end