From 2d33aa578feb6d90051324d9766826a5193c6fd6 Mon Sep 17 00:00:00 2001 From: Christian Jaekl Date: Fri, 8 Dec 2023 16:37:34 -0500 Subject: [PATCH 1/2] (bugfix) Zone lookup by name This fixes two bugs when looking up a zone by name: - Consider the possibility that the zone may not have a name. For example, `ES-EA` (Ceuta and Melilla) is not in the Shopify legacy list of known zones, so `.legacy_name` returns `nil`, and this was preventing zone lookup by name for other zones in Spain, because the search would walk off a `nil` pointer. - Search `.name_alternates` entries as well. This was causing `Worldwide.region(code: "ES").zone(name: "Islas Baleares")` to fail, returing the `ZZ` unknown region. --- lib/worldwide/region.rb | 7 ++++--- test/worldwide/region_test.rb | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/worldwide/region.rb b/lib/worldwide/region.rb index 3e5623dd3..e00c100f1 100644 --- a/lib/worldwide/region.rb +++ b/lib/worldwide/region.rb @@ -373,9 +373,10 @@ def zone(code: nil, name: nil, zip: nil) search_name = name.upcase zones.find do |region| - search_name == region.legacy_name.upcase || - search_name == region.full_name.upcase || - search_name == I18n.with_locale(:en) { region.full_name.upcase } + search_name == region.legacy_name&.upcase || + region.name_alternates&.any? { |a| search_name == a.upcase } || + search_name == region.full_name&.upcase || + search_name == I18n.with_locale(:en) { region.full_name&.upcase } end else # Worldwide::Util.present?(zip) zone_by_normalized_zip(Zip.normalize(country_code: iso_code, zip: zip)) diff --git a/test/worldwide/region_test.rb b/test/worldwide/region_test.rb index 87f893232..d10b369c6 100644 --- a/test/worldwide/region_test.rb +++ b/test/worldwide/region_test.rb @@ -99,6 +99,12 @@ class RegionTest < ActiveSupport::TestCase end end + test "can look up a zone by its alternate names" do + ["Baleares", "Illes Balears", "Islas Baleares"].each do |name| + assert_equal "ES-PM", Worldwide.region(code: "ES").zone(name: name).iso_code + end + end + test "can look up a zone by CLDR code" do zone = Worldwide.region(code: "MY").zone(code: "my14") From f7f5642fa5db519074c249f6e6a44160c7efa3cc Mon Sep 17 00:00:00 2001 From: Christian Jaekl Date: Fri, 8 Dec 2023 16:46:34 -0500 Subject: [PATCH 2/2] (Update CHANGELOG.md) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f49f37c71..538ec9d8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Allow building number in address2 for DK [#53](https://github.com/Shopify/worldwide/pull/53) - Avoid .present? and .blank? so we don't require Rails [#57](https://github.com/Shopify/worldwide/pull/57) +- (bugfix) Zone lookup by name [#58](https://github.com/Shopify/worldwid/pull/58) ---