Class: Workarea::OrderItemDetails

Inherits:
Object
  • Object
show all
Defined in:
app/queries/workarea/order_item_details.rb

Defined Under Namespace

Classes: InvalidPurchase

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product, sku) ⇒ OrderItemDetails

Returns a new instance of OrderItemDetails.



31
32
33
34
# File 'app/queries/workarea/order_item_details.rb', line 31

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

Instance Attribute Details

#productObject (readonly)

Returns the value of attribute product.



5
6
7
# File 'app/queries/workarea/order_item_details.rb', line 5

def product
  @product
end

#skuObject (readonly)

Returns the value of attribute sku.



5
6
7
# File 'app/queries/workarea/order_item_details.rb', line 5

def sku
  @sku
end

Class Method Details

.find(sku, product_id: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/queries/workarea/order_item_details.rb', line 19

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

.find!(sku, product_id: nil) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
# File 'app/queries/workarea/order_item_details.rb', line 7

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

Instance Method Details

#cache_expirationObject



64
65
66
# File 'app/queries/workarea/order_item_details.rb', line 64

def cache_expiration
  Workarea.config.cache_expirations.order_item_details
end

#cache_keyObject



60
61
62
# File 'app/queries/workarea/order_item_details.rb', line 60

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

#category_idsObject



36
37
38
# File 'app/queries/workarea/order_item_details.rb', line 36

def category_ids
  Categorization.new(product).to_a
end

#fulfillmentObject



44
45
46
# File 'app/queries/workarea/order_item_details.rb', line 44

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

#pricingObject



40
41
42
# File 'app/queries/workarea/order_item_details.rb', line 40

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

#to_hObject



48
49
50
51
52
53
54
55
56
57
58
# File 'app/queries/workarea/order_item_details.rb', line 48

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