Sha256: 6838df53399b474fe8092d3abd85b56410b8c6988a9f73941c6f06d26c7495f7

Contents?: true

Size: 1.13 KB

Versions: 62

Compression:

Stored size: 1.13 KB

Contents

module Workarea
  module Storefront
    class ProductViewModel
      class CacheKey
        def initialize(product, options = {})
          @product = product
          @options = options
        end

        def to_s
          (product_parts + option_parts).join('/')
        end

        private

        # This method exists to duplicate Mongoid cache for a Catalog::Product
        # that has been loaded from Elasticsearch. Since not persisted, Mongoid
        # returns a hardcoded cache key.
        def product_parts
          [
            @product.send(:model_key), # Mongoid has this as a private method
            "#{@product.id}-#{@product.updated_at.utc.to_s(:nsec)}"
          ]
        end

        def option_parts
          common_options.values + details_in_options.values
        end

        def details_in_options
          @options.select { |option, _value| detail_keys.include?(option&.systemize) }
        end

        def common_options
          @options.slice(:current_sku, :via)
        end

        def detail_keys
          @product.variants.flat_map(&:detail_names).map(&:systemize).uniq
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-storefront-3.4.27 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.4 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.26 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.3 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.25 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.2 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.24 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.1 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.23 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.22 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.0 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.21 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.0.beta.1 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.20 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.19 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.18 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.17 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.16 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.15 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.14 app/view_models/workarea/storefront/product_view_model/cache_key.rb