Skip to content

Clean up tax rate match logic#705

Merged
jhawthorn merged 3 commits intomasterfrom
unknown repository
Jan 20, 2016
Merged

Clean up tax rate match logic#705
jhawthorn merged 3 commits intomasterfrom
unknown repository

Conversation

@mamhoff
Copy link
Copy Markdown
Contributor

@mamhoff mamhoff commented Jan 16, 2016

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_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.

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_if behaviour correctly.

Some comments moved.

Comment thread core/app/models/spree/tax_rate.rb Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, you're right. This way I just run the contains? call twice pre iteration, nothing gained.

@jhawthorn
Copy link
Copy Markdown
Contributor

Thank you @mamhoff, this is fair more approachable and understandable.

My favourite thing about this is that the previous remaining_rates behaviour varied based on the order of the rates (which was somewhat random) and is now fixed to do the (more) right thing. So this isn't even really a behaviour change.

👍

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.
@mamhoff
Copy link
Copy Markdown
Contributor Author

mamhoff commented Jan 19, 2016

Amended for better readability as mentioned. Thank you @jhawthorn!

@jhawthorn
Copy link
Copy Markdown
Contributor

Note to future self: We'll have to make a change log entry for the removal of potentially_applicable.

Clarke says 👍

jhawthorn added a commit that referenced this pull request Jan 20, 2016
@jhawthorn jhawthorn merged commit ab3735d into solidusio:master Jan 20, 2016
@mamhoff mamhoff mentioned this pull request Mar 16, 2016
23 tasks
@mamhoff mamhoff deleted the clean-up-tax-rate-match branch May 24, 2016 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants