Clean up tax rate match logic#705
Conversation
There was a problem hiding this comment.
These lines might read cleaner as
rates_for_order_zone = all_rates.select{|rate| rate.zone.contains?(order_tax_zone) }
rates_for_default_zone = all_rates.select{|rate| rate.default_vat? }There was a problem hiding this comment.
I know, but that would mean iterating twice over all the tax rates in the system, and running the expensive contains? call double as often.
There was a problem hiding this comment.
Whoops, you're right. This way I just run the contains? call twice pre iteration, nothing gained.
|
Thank you @mamhoff, this is fair more approachable and understandable. My favourite thing about this is that the previous 👍 |
This commit removes Spree::TaxRate#potentially_applicable and instead creates two arrays of rates: One for the current order's tax zone and one for the default tax zone. Then removes rates from the default tax zone rate array if they have the same tax category as any in the order tax zone rate array. Only at the end the two arrays are merged.
The previous commit clears up which tax rates get deleted in that funny bit of code at the end of `Spree::TaxRate.match`, allowing me to actually run those two intermittently failing specs.
|
Amended for better readability as mentioned. Thank you @jhawthorn! |
|
Note to future self: We'll have to make a change log entry for the removal of Clarke says 👍 |
Clean up tax rate match logic
I've been complaining already in #656 about the absolutely undecipherable bit of code there that removes some tax rates from the matching tax rates. This commit removes
Spree::TaxRate#potentially_applicableand instead creates two arrays of rates: One for the current order's tax zone and one for the default tax zone. Then removes rates from the default tax zone rate array if they have the same tax category as any in the order tax zone rate array.Only at the end the two arrays are merged.
While this is still not correct behaviour, it's at least more understandable and shows the intention of the old behaviour. It should also work with Ruby 2.3 if I understand the new
#delete_ifbehaviour correctly.Some comments moved.