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

Allow option to preserve the sort order as inputted in the only array of countries #202

Merged
merged 3 commits into from
Jun 30, 2022
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
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
Copy link
Member Author

@pmor pmor Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, the swapping the order of the arrays here is what preserves the order of the only array. The new option is there to control if the final output is sorted by country_options_for or not

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