Sha256: ff8c27fd21b38bc09d1893f3f7b0e271391ed11d0aa090bb212761c815749149

Contents?: true

Size: 991 Bytes

Versions: 13

Compression:

Stored size: 991 Bytes

Contents

# frozen_string_literal: true

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

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

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

      blob = source.try(:blob) || source
      Shimmer::FileProxy.new(blob_id: blob.id, width: width, height: height).url
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shimmer-0.0.16 lib/shimmer/utils/file_helper.rb
shimmer-0.0.15 lib/shimmer/utils/file_helper.rb
shimmer-0.0.14 lib/shimmer/utils/file_helper.rb
shimmer-0.0.13 lib/shimmer/utils/file_helper.rb
shimmer-0.0.12 lib/shimmer/utils/file_helper.rb
shimmer-0.0.11 lib/shimmer/utils/file_helper.rb
shimmer-0.0.10 lib/shimmer/utils/file_helper.rb
shimmer-0.0.9 lib/shimmer/utils/file_helper.rb
shimmer-0.0.8 lib/shimmer/utils/file_helper.rb
shimmer-0.0.7 lib/shimmer/utils/file_helper.rb
shimmer-0.0.6 lib/shimmer/utils/file_helper.rb
shimmer-0.0.5 lib/shimmer/utils/file_helper.rb
shimmer-0.0.4 lib/shimmer/utils/file_helper.rb