Skip to content

Commit

Permalink
Merge pull request #213 from countries/fix_priority_sorting_bug
Browse files Browse the repository at this point in the history
Fix priority country sorting bug
  • Loading branch information
pmor authored Jan 3, 2023
2 parents 9b172af + 3530111 commit 6334393
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/country_select/tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,18 @@ def format
end

def country_options
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?
only_country_codes & codes
codes = only_country_codes & ISO3166::Country.codes
sort = @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided])
elsif except_country_codes.present?
codes - except_country_codes
codes = ISO3166::Country.codes - except_country_codes
sort = true
else
codes
codes = ISO3166::Country.codes
sort = true
end

country_options_for(codes, sort)
end

def country_options_for(country_codes, sorted=true)
Expand Down
18 changes: 18 additions & 0 deletions spec/country_select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ class Walrus
expect(t).to include(tag)
end

it "priority countries with `sort_provided: false` still sorts the non-priority countries by name" do
tag = options_for_select(
[
['Latvia','LV'],
['United States','US'],
['Denmark', 'DK'],
['-'*15,'-'*15],
['Afghanistan','AF']
],
selected: 'AF',
disabled: '-'*15
)

walrus.country_code = 'AF'
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

0 comments on commit 6334393

Please sign in to comment.