Skip to content

Commit

Permalink
Fix match method
Browse files Browse the repository at this point in the history
Match cannot call for_address(address).first because for_address does
not return the zone members in order zip_code, state, country.
  • Loading branch information
DanielePalombo committed Dec 20, 2017
1 parent 7969cdd commit e28ee70
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 14 additions & 1 deletion app/models/solidus_zip_zones/zone_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,20 @@ def with_shared_members(zone)
def match(address)
Spree::Deprecation.warn("Spree::Zone.match is deprecated. Please use Spree::Zone.for_address instead.", caller)

for_address(address).first
# This code can't be replaced with for_address because for_address
# does not sort the member in order zip_code, state, country
return unless address && (matches =
for_address(address).
order(:zone_members_count, :created_at, :id).
references(:zones))

['zip_code', 'state', 'country'].each do |zone_kind|
if match = matches.detect { |zone| zone_kind == zone.kind }
return match
end
end

matches.first
end
end

Expand Down
11 changes: 9 additions & 2 deletions spec/models/solidus_zip_zones/zone_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,16 @@
end

context "when there are three qualified zones with different member types" do
let!(:zip_zone) { create(:zone, name: 'ZipZone', zipcodes: '12345,12346') }

let!(:zip_zone) { create(:zone, zip_codes: zip_codes) }
let(:state_zone) { create(:zone, name: 'StateZone') }

let(:zip_codes) do
[
create(:zip_code, state_code: "AL", name: '12345'),
create(:zip_code, state_code: "AL", name: '12346')
]
end

let(:address) do
create(:address,
country: country,
Expand Down

0 comments on commit e28ee70

Please sign in to comment.