Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use of contains? in tax_rate.rb #657

Merged
merged 3 commits into from Jan 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions core/app/models/spree/tax_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ def self.adjust(order_tax_zone, items)
#
# Those rates should never come into play at all and only the French rates should apply.
def potentially_applicable?(order_tax_zone)
# If the rate's zone matches the order's tax zone, then it's applicable.
self.zone == order_tax_zone ||
# If the rate's zone *contains* the order's tax zone, then it's applicable.
self.zone.contains?(order_tax_zone) ||
# 1) The rate's zone is the default zone, then it's always applicable.
(self.included_in_price? && self.zone.default_tax)
# The rate is a VAT and its zone contains the default zone, then it's applicable.
(self.included_in_price? && self.zone.contains?(Spree::Zone.default_tax))
end

# Creates necessary tax adjustments for the order.
Expand Down Expand Up @@ -188,8 +186,7 @@ def compute_amount(item)
end

def default_zone_or_zone_match?(order_tax_zone)
default_tax = Zone.default_tax
(default_tax && default_tax.contains?(order_tax_zone)) || order_tax_zone == self.zone
Zone.default_tax.try!(:contains?, order_tax_zone) || self.zone.contains?(order_tax_zone)
end

private
Expand Down
1 change: 1 addition & 0 deletions core/app/models/spree/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def state_ids=(ids)
# Indicates whether the specified zone falls entirely within the zone performing
# the check.
def contains?(target)
return true if self == target
return false if kind == 'state' && target.kind == 'country'
return false if zone_members.empty? || target.zone_members.empty?

Expand Down
11 changes: 10 additions & 1 deletion core/spec/models/spree/zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@
@target = create(:zone, name: 'target', zone_members: [])
end

it "should contain itself" do
expect(@source.contains?(@source)).to be true
end

context "when both source and target have no members" do
it "should be false" do
expect(@source.contains?(@target)).to be false
end
end

context "when the target has no members" do
before { @source.members.create(zoneable: country1) }

Expand All @@ -155,7 +165,6 @@

context "when both zones are the same zone" do
before do
@source.members.create(zoneable: country1)
@target = @source
end

Expand Down