Skip to content

Commit

Permalink
Merge pull request #202 from countries/custom_order_for_only_array
Browse files Browse the repository at this point in the history
Allow option to preserve the sort order as inputted in the `only` array of countries
  • Loading branch information
pmor authored Jun 30, 2022
2 parents 85bb681 + 34e3891 commit 7e73c4c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion lib/country_select/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module CountrySelect
locale: nil,
only: nil,
priority_countries: nil,
priority_countries_divider: "-" * 15
priority_countries_divider: "-" * 15,
sort_provided: true
}
end
6 changes: 3 additions & 3 deletions lib/country_select/tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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_provided, ::CountrySelect::DEFAULTS[:sort_provided]))
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
Expand Down
48 changes: 47 additions & 1 deletion spec/country_select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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")
Expand Down Expand Up @@ -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]
Expand All @@ -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',
Expand Down

0 comments on commit 7e73c4c

Please sign in to comment.