Class: Workarea::Pricing::Request
- Inherits:
-
Object
- Object
- Workarea::Pricing::Request
- Defined in:
- app/models/workarea/pricing/request.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#discounts ⇒ Discount::Collection
An enumerable of discounts, which allows single a single db query for discounts to fix N+1 discount queries.
-
#initialize(order, shippings) ⇒ Request
constructor
A new instance of Request.
-
#order ⇒ Workarea::Order
Builds a duplicate, non-persisted version of the Order for manipulation during pricing.
- #pricing ⇒ Discount::Collection
-
#run ⇒ Object
Calls each calculator which in turn are what modifies the price adjustments on the order.
- #save! ⇒ Object
-
#shippings ⇒ Array<Shipping>
Builds a list of duplicate, non-persisted versions of the Shipping for manipulation during pricing.
- #stale? ⇒ Boolean
Constructor Details
#initialize(order, shippings) ⇒ Request
Returns a new instance of 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
#discounts ⇒ Discount::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 |
#order ⇒ Workarea::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 |
#pricing ⇒ Discount::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 |
#run ⇒ Object
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
88 89 90 |
# File 'app/models/workarea/pricing/request.rb', line 88 def save! !stale? || save_order && save_shippings end |
#shippings ⇒ Array<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 |