Commit c263747
committed
Autosave shipment adjustments to persist taxes on recalculate
A commit was made in solidusio#6315 (5979f25) in service of the in memory order updater solidusio#5872 that changed the behavior in the OrderTaxation class to set the adjustment value but not persist it. Instead, the persistence happens when the order is saved. If a shipment is changed, and an associated adjustment is changed, the shipment adjustment will be autosaved when the order is.
However, if a shipment is *not* changed, but the adjustment is, the adjustment will *not* be autosaved when the order is.
A scenario where this can happen is a recalculation that just adjusts shipment taxes on an order that would otherwise not change. Let's walk through what happens in the OrderUpdater
1. `update_totals` is called which in turn...
2. calls `update_adjustment_total`, which in turn...
3. calls `recalculate_adjustments` which in turn...
4. Calls `update_tax_adjustments`
- `update_tax_adjustments` creates and sets the values of the tax adjustments for a shipment. At this point, the shipment adjustment has been changed, but not persisted, and the shipment has *not* been changed.
5. Calls `update_item_totals` which calls...
6. `Spree::Config.item_total_class.new(item).recalculate!`
- `Spree::Config.item_total_class` sets the `included_tax_total`, `additional_tax_total` and `adjustment_total` on the shipment to their new values, calculated from the non-persisted adjustment. At this point, both the shipment *and* adjustment have been changed but not persisted.
- If you were to call `order.save!` at this point, the shipment adjustment would autosave.
7. Next, `item.update_columns` is called with the columns that the `item_total_class` updated.
- At this point, the shipment, it's changes having been persisted to the database, will no longer be marked as `changed`.
- This means that the default behavior of autosave in rails will *not* save the shipment when the order is saved, which means the associated (and changed) adjustment will not get saved either
In solidusio#6315, we ensured that these changed associations cannot get left behind by setting `autosave: true`, which forces the model to check if it has any associations that need saving instead of telling it's relation "nope I haven't changed"
We can do the same thing here for the shipment to ensure changing adjustments will make the shipment tell it's order "I have changed", even if none of it's direct properties have changed.
You can test this change by removing the `autosave: true` option the adjustments and running 'when the address has changed to a different state' specs. They fail with the error "expected `order.shipments.first.adjustments.first.amount` to have changed from 1 to 2, but did not change", demonstrating a scenario where the shipment adjustment changes do not get persisted.1 parent d070f94 commit c263747
File tree
2 files changed
+60
-14
lines changed- core
- app/models/spree
- spec/models/spree
2 files changed
+60
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
115 | 139 | | |
116 | 140 | | |
117 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
118 | 144 | | |
119 | 145 | | |
120 | | - | |
| 146 | + | |
121 | 147 | | |
122 | | - | |
| 148 | + | |
123 | 149 | | |
124 | | - | |
125 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
126 | 153 | | |
127 | | - | |
128 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
129 | 175 | | |
130 | 176 | | |
131 | 177 | | |
| |||
179 | 225 | | |
180 | 226 | | |
181 | 227 | | |
182 | | - | |
| 228 | + | |
183 | 229 | | |
184 | 230 | | |
185 | 231 | | |
| |||
188 | 234 | | |
189 | 235 | | |
190 | 236 | | |
191 | | - | |
| 237 | + | |
192 | 238 | | |
193 | 239 | | |
194 | 240 | | |
| |||
0 commit comments