summaryrefslogtreecommitdiff
path: root/app/helpers/lazy_image_tag_helper.rb
blob: aea1fcc7e91406d3839578046369ce68e280a0fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module LazyImageTagHelper
  def placeholder_image
    "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
  end

  # Override the default ActionView `image_tag` helper to support lazy-loading
  def image_tag(source, options = {})
    options = options.symbolize_keys

    unless options.delete(:lazy) == false
      options[:data] ||= {}
      unless options.delete(:use_original_source) == true
        options[:data][:src] = path_to_image(source)
      else
        options[:data][:src] = source
      end
      options[:class] ||= ""
      options[:class] << " lazy"

      source = placeholder_image
    end

    super(source, options)
  end

  # Required for Banzai::Filter::ImageLazyLoadFilter
  module_function :placeholder_image
end