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.5.27 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.26 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.45 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.25 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.23 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.44 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.22 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.43 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.21 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.42 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.20 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.41 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.19 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.40 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.18 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.39 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.17 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.38 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.5.16 app/view_models/workarea/storefront/product_view_model/cache_key.rb
workarea-storefront-3.4.37 app/view_models/workarea/storefront/product_view_model/cache_key.rb