Module: Shimmer::FileAdditions

Defined in:
lib/shimmer/utils/file_helper.rb

Instance Method Summary collapse

Instance Method Details

#image_file_path(source) ⇒ Object



37
38
39
# File 'lib/shimmer/utils/file_helper.rb', line 37

def image_file_path(source, **)
  image_file_proxy(source, **, return_type: :path)
end

#image_file_proxy(source, width: nil, height: nil, return_type: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shimmer/utils/file_helper.rb', line 45

def image_file_proxy(source, width: nil, height: nil, return_type: nil)
  return if source.blank?
  return source if source.is_a?(String)

  blob = source.try(:blob) || source
  proxy = Shimmer::FileProxy.new(blob_id: blob.id, width: width, height: height)
  case return_type
  when nil
    proxy
  when :path
    proxy.path
  when :url
    proxy.url
  end
end

#image_file_url(source) ⇒ Object



41
42
43
# File 'lib/shimmer/utils/file_helper.rb', line 41

def image_file_url(source, **)
  image_file_proxy(source, **, return_type: :url)
end

#image_tag(source, **options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/shimmer/utils/file_helper.rb', line 15

def image_tag(source, **options)
  return nil if source.blank?

  if source.is_a?(ActiveStorage::Variant) ||
      source.is_a?(ActiveStorage::VariantWithRecord) ||
      source.is_a?(ActiveStorage::Attached) ||
      source.is_a?(ActiveStorage::Attachment) ||
      (Object.const_defined?("ActionText::Attachment") && source.is_a?(ActionText::Attachment))
    attachment = source
    width = options[:width]
    height = options[:height]
    source = image_file_path(source, width: width, height: height)
    options[:loading] ||= :lazy
    if options[:width].present?
      source_2x = image_file_path(attachment, width: width.to_i * 2, height: height ? height.to_i * 2 : nil)
      options[:srcset] = "#{source} 1x, #{source_2x} 2x"
    end
  end

  super(source, options)
end