From 5221f2bbe8b6b6bb1ab19aec63bceaceedec16b1 Mon Sep 17 00:00:00 2001 From: Pedro Moreira Date: Mon, 11 Apr 2022 17:05:40 +0100 Subject: [PATCH 1/3] WIP - Allow option to preserve the sort order as inputted in the `only` array of countries --- lib/country_select/defaults.rb | 3 ++- lib/country_select/tag_helper.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/country_select/defaults.rb b/lib/country_select/defaults.rb index 681b250..09de327 100644 --- a/lib/country_select/defaults.rb +++ b/lib/country_select/defaults.rb @@ -5,6 +5,7 @@ module CountrySelect locale: nil, only: nil, priority_countries: nil, - priority_countries_divider: "-" * 15 + priority_countries_divider: "-" * 15, + sort_only: true } end diff --git a/lib/country_select/tag_helper.rb b/lib/country_select/tag_helper.rb index 20def47..1a6435f 100644 --- a/lib/country_select/tag_helper.rb +++ b/lib/country_select/tag_helper.rb @@ -59,14 +59,14 @@ def format end def country_options - country_options_for(all_country_codes, true) + country_options_for(all_country_codes, @options.fetch(:sort_only, ::CountrySelect::DEFAULTS[:sort_only])) end def all_country_codes codes = ISO3166::Country.codes if only_country_codes.present? - codes & only_country_codes + only_country_codes & codes elsif except_country_codes.present? codes - except_country_codes else From fe26f97f91e2971eac26c1889116a9191b3d80b9 Mon Sep 17 00:00:00 2001 From: Pedro Moreira Date: Mon, 27 Jun 2022 09:26:08 +0100 Subject: [PATCH 2/3] Rename sort_only to sort_provided and apply the same logic to priority countries. Update tests --- lib/country_select/defaults.rb | 2 +- lib/country_select/tag_helper.rb | 4 +-- spec/country_select_spec.rb | 48 +++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/country_select/defaults.rb b/lib/country_select/defaults.rb index 09de327..0a1c1fa 100644 --- a/lib/country_select/defaults.rb +++ b/lib/country_select/defaults.rb @@ -6,6 +6,6 @@ module CountrySelect only: nil, priority_countries: nil, priority_countries_divider: "-" * 15, - sort_only: true + sort_provided: true } end diff --git a/lib/country_select/tag_helper.rb b/lib/country_select/tag_helper.rb index 1a6435f..3493edf 100644 --- a/lib/country_select/tag_helper.rb +++ b/lib/country_select/tag_helper.rb @@ -19,7 +19,7 @@ def country_option_tags } if priority_countries.present? - priority_countries_options = country_options_for(priority_countries, false) + priority_countries_options = country_options_for(priority_countries, @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided])) option_tags = options_for_select(priority_countries_options, option_tags_options) option_tags += html_safe_newline + options_for_select([priority_countries_divider], disabled: priority_countries_divider) @@ -59,7 +59,7 @@ def format end def country_options - country_options_for(all_country_codes, @options.fetch(:sort_only, ::CountrySelect::DEFAULTS[:sort_only])) + country_options_for(all_country_codes, @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided])) end def all_country_codes diff --git a/spec/country_select_spec.rb b/spec/country_select_spec.rb index 33fda88..72bff83 100644 --- a/spec/country_select_spec.rb +++ b/spec/country_select_spec.rb @@ -92,9 +92,26 @@ class Walrus it "accepts priority countries" do tag = options_for_select( [ + ['Denmark', 'DK'], ['Latvia','LV'], ['United States','US'], + ['-'*15,'-'*15] + ], + selected: 'US', + disabled: '-'*15 + ) + + walrus.country_code = 'US' + t = builder.country_select(:country_code, priority_countries: ['LV','US','DK']) + expect(t).to include(tag) + end + + it "priority countries are sorted by name by default" do + tag = options_for_select( + [ ['Denmark', 'DK'], + ['Latvia','LV'], + ['United States','US'], ['-'*15,'-'*15] ], selected: 'US', @@ -106,6 +123,23 @@ class Walrus expect(t).to include(tag) end + it "priority countries with `sort_provided: false` preserves the provided order" do + tag = options_for_select( + [ + ['Latvia','LV'], + ['United States','US'], + ['Denmark', 'DK'], + ['-'*15,'-'*15] + ], + selected: 'US', + disabled: '-'*15 + ) + + walrus.country_code = 'US' + t = builder.country_select(:country_code, priority_countries: ['LV','US','DK'], sort_provided: false) + expect(t).to include(tag) + end + describe "when selected options is not an array" do it "selects only the first matching option" do tag = options_for_select([["United States", "US"],["Uruguay", "UY"]], "US") @@ -139,6 +173,18 @@ class Walrus expect(t).to_not include(tag) end + it "countries provided in `only` are sorted by name by default" do + t = builder.country_select(:country_code, only: ['PT','DE','AR']) + order = t.scan(/value="(\w{2})"/).map { |o| o[0] } + expect(order).to eq(['AR', 'DE', 'PT']) + end + + it "countries provided in `only` with `sort_provided` to false keeps the order of the provided countries" do + t = builder.country_select(:country_code, only: ['PT','DE','AR'], sort_provided: false) + order = t.scan(/value="(\w{2})"/).map { |o| o[0] } + expect(order).to eq(['PT','DE','AR']) + end + context "when there is a default 'except' configured" do around do |example| old_value = ::CountrySelect::DEFAULTS[:except] @@ -160,9 +206,9 @@ class Walrus it "accepts priority countries" do tag = options_for_select( [ + ['Denmark', 'DK'], ['Latvia','LV'], ['United States','US'], - ['Denmark', 'DK'], ['-'*15,'-'*15] ], selected: 'US', From 34e3891163073096c7d0b10835ef24c0c30016ef Mon Sep 17 00:00:00 2001 From: Pedro Moreira Date: Mon, 27 Jun 2022 09:31:13 +0100 Subject: [PATCH 3/3] Update README --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ed197f7..d022f65 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,17 @@ country_select("user", "country") Supplying priority countries to be placed at the top of the list: ```ruby -country_select("user", "country", priority_countries: ["GB", "FR", "DE"]) +country_select("user", "country", priority_countries: ["GB", "FR", "DE"]) # Countries will be sorted by name according to the current locale +# or +country_select("user", "country", priority_countries: ["GB", "FR", "DE"], sort_provided: false) # Countries will be displayed is the provided order ``` Supplying only certain countries: ```ruby -country_select("user", "country", only: ["GB", "FR", "DE"]) +country_select("user", "country", only: ["GB", "FR", "DE"]) # Countries will be sorted by name according to the current locale +# or +country_select("user", "country", only: ["GB", "FR", "DE"], sort_provided: false) # Countries will be displayed is the provided order ``` Discarding certain countries: