Sha256: e84d755a851197709242ddb18f6d11b547d3a3c089e7a2248473ca9fa1c3f874

Contents?: true

Size: 1.55 KB

Versions: 62

Compression:

Stored size: 1.55 KB

Contents

module Workarea
  module Storefront
    class ProductViewModel
      class ImageCollection
        include Enumerable

        attr_reader :all

        def initialize(product, options = {}, images = nil)
          @product = product
          @options = options.with_indifferent_access
          @all = images || @product.images
        end

        def primary
          @primary ||= selected_facet_images ||
                       first ||
                       Catalog::ProductPlaceholderImage.cached
        end

        def +(other)
          ImageCollection.new(@product, @options, @all + other.all)
        end

        def respond_to_missing?(method_name, include_private = false)
          super || @all.respond_to?(method_name)
        end

        def method_missing(sym, *args, &block)
          if @all.respond_to?(sym)
            @all.send(sym, *args, &block)
          else
            super
          end
        end

        private

        def selected_facet_images
          return unless selected_facet_values.present?
          @all.detect { |i| i.option.to_s.systemize.in?(selected_facet_values) }
        end

        def selected_facet_values
          @selected_facet_values ||=
            if @options[:sku].present?
              variant = @product.variants.find_by(sku: @options[:sku])
              variant.details.values.flatten.map(&:systemize)
            else
              @options.values.flat_map do |value|
                Array.wrap(value).map(&:to_s).map(&:optionize)
              end
            end
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-storefront-3.5.27 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.26 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.45 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.25 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.23 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.44 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.22 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.43 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.21 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.42 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.20 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.41 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.19 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.40 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.18 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.39 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.17 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.38 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.16 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.37 app/view_models/workarea/storefront/product_view_model/image_collection.rb