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.
- #payment ⇒ Object
- #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 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
#discounts ⇒ Discount::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 |
#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.
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 |
#payment ⇒ Object
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 |
#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.
71 72 73 |
# File 'app/models/workarea/pricing/request.rb', line 71 def pricing @pricing ||= Pricing::Collection.new(all_skus) end |
#run ⇒ Object
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
98 99 100 |
# File 'app/models/workarea/pricing/request.rb', line 98 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.
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
102 103 104 |
# File 'app/models/workarea/pricing/request.rb', line 102 def stale? @persisted_order.pricing_cache_key != cache_key.to_s end |