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
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
end
blurhash = blob&.metadata&.fetch("blurhash", nil)
if !!blurhash
size ||= "#{blob.metadata["width"]}x#{blob.metadata["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
|