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



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

def initialize(order, shippings)
  @persisted_order = order
  @persisted_shippings = Array(shippings)
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.



52
53
54
# File 'app/models/workarea/pricing/request.rb', line 52

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.



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

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

#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.



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

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/workarea/pricing/request.rb', line 70

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.



88
89
90
# File 'app/models/workarea/pricing/request.rb', line 88

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.



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

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



92
93
94
# File 'app/models/workarea/pricing/request.rb', line 92

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