Module: Iron::ImageHelper

Defined in:
app/helpers/iron/image_helper.rb

Constant Summary collapse

IMAGE_BREAKPOINTS =
[ 150, 320, 640, 1024, 1920 ].freeze

Instance Method Summary collapse

Instance Method Details

#iron_image_tag(attachment, sizes: "100vw", loading: "lazy", blur: true, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/iron/image_helper.rb', line 5

def iron_image_tag(attachment, sizes: "100vw", loading: "lazy", blur: true, **options)
  return "" unless attachment&.attached?
  return image_tag(attachment, **options) unless attachment.blob.variable?

  image_options = options.merge(
    srcset: generate_srcset(attachment),
    sizes:,
    loading:,
    width: attachment.blob.["width"],
    height: attachment.blob.["height"]
  ).compact

  if blur && attachment.blob.["blur_data_uri"].present?
    blur_style = "background-image: url('#{attachment.blob.["blur_data_uri"]}'); background-size: cover; background-position: center;"
    image_options[:style] = "#{blur_style} #{options[:style]}".strip
  end

  image_tag attachment.variant(resize_to_limit: [ IMAGE_BREAKPOINTS.third, nil ]), **image_options
end

#iron_picture_tag(attachment, sizes: "100vw", loading: "lazy", blur: true, **options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/iron/image_helper.rb', line 25

def iron_picture_tag(attachment, sizes: "100vw", loading: "lazy", blur: true, **options)
  return "" unless attachment&.attached?
  return image_tag(attachment, **options) unless attachment.blob.variable?

   :picture do
    concat source_tag(attachment, :avif, sizes)
    concat source_tag(attachment, :webp, sizes)
    concat iron_image_tag(attachment, sizes: sizes, loading: loading, blur: blur, **options)
  end
end