Sha256: a3aecb30fac63874511e1b47180991a3e6f304a91bdaf87c24d031775ddd4965

Contents?: true

Size: 1.18 KB

Versions: 34

Compression:

Stored size: 1.18 KB

Contents

module Workarea
  class OrderItemDetails
    class InvalidPurchase < StandardError; end

    attr_reader :product, :sku

    delegate :digital?, to: :product

    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 to_h
      {
        product_id: product.id,
        product_attributes: product.as_document,
        category_ids: category_ids,
        discountable: pricing.discountable?
      }
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/queries/workarea/order_item_details.rb
workarea-core-3.4.44 app/queries/workarea/order_item_details.rb
workarea-core-3.4.43 app/queries/workarea/order_item_details.rb
workarea-core-3.4.42 app/queries/workarea/order_item_details.rb
workarea-core-3.4.41 app/queries/workarea/order_item_details.rb
workarea-core-3.4.40 app/queries/workarea/order_item_details.rb
workarea-core-3.4.39 app/queries/workarea/order_item_details.rb
workarea-core-3.4.38 app/queries/workarea/order_item_details.rb
workarea-core-3.4.37 app/queries/workarea/order_item_details.rb
workarea-core-3.4.36 app/queries/workarea/order_item_details.rb
workarea-core-3.4.35 app/queries/workarea/order_item_details.rb
workarea-core-3.4.34 app/queries/workarea/order_item_details.rb
workarea-core-3.4.33 app/queries/workarea/order_item_details.rb
workarea-core-3.4.32 app/queries/workarea/order_item_details.rb
workarea-core-3.4.31 app/queries/workarea/order_item_details.rb
workarea-core-3.4.30 app/queries/workarea/order_item_details.rb
workarea-core-3.4.29 app/queries/workarea/order_item_details.rb
workarea-core-3.4.28 app/queries/workarea/order_item_details.rb
workarea-core-3.4.27 app/queries/workarea/order_item_details.rb
workarea-core-3.4.26 app/queries/workarea/order_item_details.rb