Sha256: f1d3f842a8b7dbafbc9f9976ad4a9f4d27e5548dd4c3d1737dba3188a35475aa

Contents?: true

Size: 1.54 KB

Versions: 27

Compression:

Stored size: 1.54 KB

Contents

module Workarea
  class OrderItemDetails
    class InvalidPurchase < StandardError; end

    attr_reader :product, :sku

    def self.find!(sku, product_id: nil)
      product =
        if product_id.present?
          Catalog::Product.where(id: product_id).find_by_sku(sku)
        else
          Catalog::Product.find_by_sku(sku)
        end
      raise InvalidPurchase, sku unless product && product.purchasable?

      new(product, sku)
    end

    def self.find(sku, product_id: nil)
      product =
        if product_id.present?
          Catalog::Product.where(id: product_id).find_by_sku(sku)
        else
          Catalog::Product.find_by_sku(sku)
        end

      return nil if product.blank?
      new(product, sku)
    end

    def initialize(product, sku)
      @product = product
      @sku = sku
    end

    def category_ids
      Categorization.new(product).to_a
    end

    def pricing
      @pricing ||= Pricing::Sku.find(sku)
    end

    def fulfillment
      @fulfillment ||= Fulfillment::Sku.find_or_initialize_by(id: sku)
    end

    def to_h
      Rails.cache.fetch(cache_key, expires_in: cache_expiration) do
        {
          product_id: product.id,
          product_attributes: product.as_document,
          category_ids: category_ids,
          discountable: pricing.discountable?,
          fulfillment: fulfillment.policy
        }
      end
    end

    def cache_key
      "order_item_details/#{product.cache_key}/#{sku}"
    end

    def cache_expiration
      Workarea.config.cache_expirations.order_item_details
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/queries/workarea/order_item_details.rb
workarea-core-3.5.26 app/queries/workarea/order_item_details.rb
workarea-core-3.5.25 app/queries/workarea/order_item_details.rb
workarea-core-3.5.23 app/queries/workarea/order_item_details.rb
workarea-core-3.5.22 app/queries/workarea/order_item_details.rb
workarea-core-3.5.21 app/queries/workarea/order_item_details.rb
workarea-core-3.5.20 app/queries/workarea/order_item_details.rb
workarea-core-3.5.19 app/queries/workarea/order_item_details.rb
workarea-core-3.5.18 app/queries/workarea/order_item_details.rb
workarea-core-3.5.17 app/queries/workarea/order_item_details.rb
workarea-core-3.5.16 app/queries/workarea/order_item_details.rb
workarea-core-3.5.15 app/queries/workarea/order_item_details.rb
workarea-core-3.5.14 app/queries/workarea/order_item_details.rb
workarea-core-3.5.13 app/queries/workarea/order_item_details.rb
workarea-core-3.5.12 app/queries/workarea/order_item_details.rb
workarea-core-3.5.11 app/queries/workarea/order_item_details.rb
workarea-core-3.5.10 app/queries/workarea/order_item_details.rb
workarea-core-3.5.9 app/queries/workarea/order_item_details.rb
workarea-core-3.5.8 app/queries/workarea/order_item_details.rb
workarea-core-3.5.7 app/queries/workarea/order_item_details.rb