Module: BlurhashImageHelper

Defined in:
app/helpers/blurhash_image_helper.rb

Instance Method Summary collapse

Instance Method Details

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



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
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/blurhash_image_helper.rb', line 4

def blurhash_image_tag(source, options = {})
  case source
  when String
    # if a URL is passed, we have to manually re-hydrate the blob from it
    path_parameters = Rails.application.routes.recognize_path(source)
    blob = ActiveStorage::Blob.find_signed!(path_parameters[:signed_blob_id] || path_parameters[:signed_id])
  when ActiveStorage::Blob
    blob = source
  when ActiveStorage::Attached::One
    blob = source.blob
  when ActiveStorage::VariantWithRecord
    blob = source.blob
    # dimensions of the transformation need not represent the actual aspect ratio, like in `resize_to_fit`
    # size = source.variation.transformations[:resize]
  end

  blurhash = blob&.&.fetch("blurhash", nil)

  if !!blurhash
    size ||= "#{blob.["width"]}x#{blob.["height"]}"

    options[:loading] = "lazy"
    options[:size] = size

    wrapper_class = options.delete(:wrapper_class)
    canvas_class = options.delete(:canvas_class)
    tag.div class: wrapper_class, data: {blurhash: blurhash}, style: "position: relative" do
      image_tag(source, options) + tag.canvas(style: "position: absolute; inset: 0; transition-property: opacity; transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); transition-duration: 150ms;", class: canvas_class)
    end
  else
    image_tag(source, options)
  end
rescue ActionController::RoutingError
  image_tag(source, options)
end