Class: Workarea::Pricing::Request

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/pricing/request.rb

Direct Known Subclasses

Calculator::TestRequest

Instance Method Summary collapse

Constructor Details

#initialize(order, shippings) ⇒ Request

Returns a new instance of Request.



4
5
6
7
8
# File 'app/models/workarea/pricing/request.rb', line 4

def initialize(order, shippings)
  @persisted_order = order
  @persisted_shippings = Array(shippings)
  @persisted_payment = Workarea::Payment.find(order.id) rescue nil
end

Instance Method Details

#discountsDiscount::Collection

An enumerable of discounts, which allows single a single db query for discounts to fix N+1 discount queries.



62
63
64
# File 'app/models/workarea/pricing/request.rb', line 62

def discounts
  @discounts ||= Discount::Collection.new
end

#orderWorkarea::Order

Builds a duplicate, non-persisted version of the Order for manipulation during pricing. This allows persisting all pricing changes to the order at once.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/workarea/pricing/request.rb', line 16

def order
  @order ||=
    begin
      result = @persisted_order.clone
      result.id = @persisted_order.id # Ensure this isn't persisted

      # This exists to fix a problem with doing price_adjustments = [] on
      # an unpersisted Order::Item on MongoDB >= 2.6 (which works on 2.4).
      # On 2.4 it doesn't try to make the write so all is fine. I have no
      # idea why this is required only on that version, but it does fix
      # the problem.
      #
      result.attributes = clone_order_attributes
      result
    end
end

#paymentObject



48
49
50
51
52
53
54
55
# File 'app/models/workarea/pricing/request.rb', line 48

def payment
  return unless @persisted_payment.present?

  @payment ||=
    Workarea::Payment.instantiate(@persisted_payment.as_document).tap do |payment|
      payment.new_record = true # Ensure this isn't persisted by raising duplicate key if saved
    end
end

#pricingDiscount::Collection

An enumerable of Sku, which allows single a single db query for SKUs to fix N+1 SKU queries while running a Workarea::Pricing::Request.



71
72
73
# File 'app/models/workarea/pricing/request.rb', line 71

def pricing
  @pricing ||= Pricing::Collection.new(all_skus)
end

#runObject

Calls each calculator which in turn are what modifies the price adjustments on the order. Does not persist it’s changes. This method is how a Order and corresponding Shipping are priced.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/workarea/pricing/request.rb', line 80

def run
  return unless stale?

  # TODO fix this hack
  order.items.where(free_gift: true).delete_all

  Workarea.config.pricing_calculators.each do |class_name|
    class_name.constantize.new(self).adjust
  end

  shippings.each { |s| ShippingTotals.new(s).total }
  OrderTotals.new(order, shippings).total
end

#save!Object

Persist the changes made to the temporary Order and its corresponding Shipping in as few DB writes as possible.



98
99
100
# File 'app/models/workarea/pricing/request.rb', line 98

def save!
  !stale? || save_order && save_shippings
end

#shippingsArray<Shipping>

Builds a list of duplicate, non-persisted versions of the Shipping for manipulation during pricing. This allows persisting all pricing changes to the shipping all at once.

Returns:



39
40
41
42
43
44
45
46
# File 'app/models/workarea/pricing/request.rb', line 39

def shippings
  @shippings ||= @persisted_shippings.map do |shipping|
    result = shipping.clone
    result.id = shipping.id # Ensure this isn't persisted
    result.reset_adjusted_shipping_pricing
    result
  end
end

#stale?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/workarea/pricing/request.rb', line 102

def stale?
  @persisted_order.pricing_cache_key != cache_key.to_s
end