Sha256: 7a8eedb625b0978957984f2101b7911fba29401c6f9ac3cb5c842cefe61ba5d0

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module IziLightup
  module SmartPicture
    class << self
      include ActionView::Helpers::AssetTagHelper

      def render(object, fields = %i[picture], versions = [], params = {})
        return '' if object.blank?

        items = fetch_items(object, fields)
        return '' if items.blank?

        versions = Array.wrap(versions)
        items.each do |item|
          next unless item.respond_to?(:data)

          versions.each do |version_name|
            next unless version_name == :default || item.data.respond_to?(version_name)

            version = version_name == :default ? item.data : item.data.public_send(version_name)
            next unless version&.file&.exists? && version&.url&.present?

            url = version.url
            dimensions = safe_dimentions(item, version)
            params.merge!(dimensions) if dimensions.present?

            return image_tag(url, params) unless block_given?

            yield(url, params)
            return ''
          end
        end

        ''
      end

      private

      def safe_dimentions(item, version)
        dimensions = nil
        begin
          dimensions = version.dimensions
        rescue MiniMagick::Error => _e
          dimensions = [item&.width, item&.height].compact
        end

        return {} if dimensions.blank?

        %i[width height].zip(dimensions).to_h
      end

      def fetch_items(object, fields)
        Array.wrap(fields).map do |name|
          object.respond_to?(name) ? object.public_send(name) : nil
        end.compact
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
izi_lightup-1.0.15 lib/izi_lightup/smart_picture.rb
izi_lightup-1.0.14 lib/izi_lightup/smart_picture.rb
izi_lightup-1.0.13 lib/izi_lightup/smart_picture.rb