Class: Spree::InMemoryOrderUpdater
- Inherits:
-
Object
- Object
- Spree::InMemoryOrderUpdater
- Defined in:
- app/models/spree/in_memory_order_updater.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Instance Method Summary collapse
-
#initialize(order) ⇒ InMemoryOrderUpdater
constructor
A new instance of InMemoryOrderUpdater.
-
#recalculate(persist: true) ⇒ Object
(also: #update)
This is a multi-purpose method for processing logic related to changes in the Order.
-
#recalculate_payment_state ⇒ Object
(also: #update_payment_state)
Recalculates the
payment_stateattribute according to the following logic:. -
#recalculate_shipment_state ⇒ Object
(also: #update_shipment_state)
Recalculates the state on all of them shipments, then recalculates the
shipment_stateattribute according to the following logic:.
Constructor Details
#initialize(order) ⇒ InMemoryOrderUpdater
Returns a new instance of InMemoryOrderUpdater.
15 16 17 |
# File 'app/models/spree/in_memory_order_updater.rb', line 15 def initialize(order) @order = order end |
Instance Attribute Details
#order ⇒ Object (readonly)
Returns the value of attribute order.
7 8 9 |
# File 'app/models/spree/in_memory_order_updater.rb', line 7 def order @order end |
Instance Method Details
#recalculate(persist: true) ⇒ Object Also known as: update
This is a multi-purpose method for processing logic related to changes in the Order. It is meant to be called from various observers so that the Order is aware of changes that affect totals and other values stored in the Order.
This method should never do anything to the Order that results in a save call on the object with callbacks (otherwise you will end up in an infinite recursion as the associations try to save and then in turn try to call update! again.)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/spree/in_memory_order_updater.rb', line 26 def recalculate(persist: true) monitor = if log_manipulative_queries Spree::ManipulativeQueryMonitor else proc { |&block| block.call } end order.transaction do monitor.call do recalculate_line_item_prices recalculate_item_count assign_shipment_amounts end if persist update_totals(persist:) else monitor.call do update_totals(persist:) end end monitor.call do if order.completed? recalculate_payment_state recalculate_shipment_state end end Spree::Bus.publish(:order_recalculated, order:) persist_totals if persist end end |
#recalculate_payment_state ⇒ Object Also known as: update_payment_state
Recalculates the payment_state attribute according to the following logic:
paid when payment_total is equal to total balance_due when payment_total is less than total credit_owed when payment_total is greater than total failed when most recent payment is in the failed state void when the order has been canceled and the payment total is 0
The payment_state value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.
93 94 95 96 |
# File 'app/models/spree/in_memory_order_updater.rb', line 93 def recalculate_payment_state order.payment_state = determine_payment_state order.payment_state end |
#recalculate_shipment_state ⇒ Object Also known as: update_shipment_state
Recalculates the state on all of them shipments, then recalculates the shipment_state attribute according to the following logic:
shipped when all Shipments are in the “shipped” state partial when at least one Shipment has a state of “shipped” and there is another Shipment with a state other than “shipped”
or there are InventoryUnits associated with the order that have a state of "sold" but are not associated with a Shipment.
ready when all Shipments are in the “ready” state backorder when there is backordered inventory associated with an order pending when all Shipments are in the “pending” state
The shipment_state value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.
75 76 77 78 79 80 |
# File 'app/models/spree/in_memory_order_updater.rb', line 75 def recalculate_shipment_state shipments.each(&:recalculate_state) order.shipment_state = determine_shipment_state order.shipment_state end |