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.15 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.36 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.14 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.35 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.13 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.34 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.12 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.33 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.11 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.10 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.32 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.9 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.31 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.8 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.30 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.7 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.29 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.6 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.4.28 app/view_models/workarea/storefront/product_view_model/image_collection.rb
workarea-storefront-3.5.5 app/view_models/workarea/storefront/product_view_model/image_collection.rb