country_select 7.0.0 → 8.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.codeclimate.yml +61 -0
- data/.github/workflows/codeql-analysis.yml +3 -3
- data/.github/workflows/test.yml +36 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +34 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +83 -54
- data/README.md +10 -12
- data/Rakefile +23 -0
- data/country_select.gemspec +12 -9
- data/gemfiles/actionpack-6.1.gemfile +7 -4
- data/gemfiles/actionpack-6.1.gemfile.lock +62 -57
- data/gemfiles/actionpack-7.0.gemfile +7 -4
- data/gemfiles/actionpack-7.0.gemfile.lock +63 -57
- data/gemfiles/actionpack-7.1.gemfile +9 -0
- data/gemfiles/actionpack-7.1.gemfile.lock +145 -0
- data/lib/country_select/country_select_helper.rb +10 -1
- data/lib/country_select/defaults.rb +4 -1
- data/lib/country_select/formats.rb +2 -0
- data/lib/country_select/tag_helper.rb +59 -54
- data/lib/country_select/version.rb +3 -1
- data/lib/country_select.rb +1 -2
- data/spec/country_select_spec.rb +142 -95
- data/spec/spec_helper.rb +7 -3
- metadata +21 -24
- data/.github/workflows/build.yml +0 -25
- data/gemfiles/actionpack-5.2.gemfile +0 -6
- data/gemfiles/actionpack-5.2.gemfile.lock +0 -106
- data/gemfiles/actionpack-6.0.gemfile +0 -6
- data/gemfiles/actionpack-6.0.gemfile.lock +0 -107
- data/gemfiles/actionpack-head.gemfile +0 -9
- data/gemfiles/actionpack-head.gemfile.lock +0 -104
- data/lib/country_select_without_sort_alphabetical.rb +0 -9
|
@@ -1,12 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module CountrySelect
|
|
2
|
-
class CountryNotFoundError < StandardError;end
|
|
4
|
+
class CountryNotFoundError < StandardError; end
|
|
5
|
+
|
|
3
6
|
module TagHelper
|
|
7
|
+
unless respond_to?(:options_for_select)
|
|
8
|
+
include ActionView::Helpers::FormOptionsHelper
|
|
9
|
+
include ActionView::Helpers::Tags::SelectRenderer if defined?(ActionView::Helpers::Tags::SelectRenderer)
|
|
10
|
+
end
|
|
11
|
+
|
|
4
12
|
def country_option_tags
|
|
5
13
|
# In Rails 5.2+, `value` accepts no arguments and must also be called
|
|
6
14
|
# with parens to avoid the local variable of the same name
|
|
7
15
|
# https://github.com/rails/rails/pull/29791
|
|
8
16
|
selected_option = @options.fetch(:selected) do
|
|
9
|
-
if self.method(:value).arity
|
|
17
|
+
if self.method(:value).arity.zero?
|
|
10
18
|
value()
|
|
11
19
|
else
|
|
12
20
|
value(@object)
|
|
@@ -14,26 +22,19 @@ module CountrySelect
|
|
|
14
22
|
end
|
|
15
23
|
|
|
16
24
|
option_tags_options = {
|
|
17
|
-
:
|
|
18
|
-
:
|
|
25
|
+
selected: selected_option,
|
|
26
|
+
disabled: @options[:disabled]
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
if priority_countries.present?
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
option_tags = options_for_select(priority_countries_options, option_tags_options)
|
|
25
|
-
option_tags += html_safe_newline + options_for_select([priority_countries_divider], disabled: priority_countries_divider)
|
|
26
|
-
|
|
27
|
-
option_tags_options[:selected] = [option_tags_options[:selected]] unless option_tags_options[:selected].kind_of?(Array)
|
|
28
|
-
option_tags_options[:selected].delete_if{|selected| priority_countries_options.map(&:second).include?(selected)}
|
|
29
|
-
|
|
30
|
-
option_tags += html_safe_newline + options_for_select(country_options, option_tags_options)
|
|
30
|
+
options_for_select_with_priority_countries(country_options, option_tags_options)
|
|
31
31
|
else
|
|
32
|
-
|
|
32
|
+
options_for_select(country_options, option_tags_options)
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
private
|
|
37
|
+
|
|
37
38
|
def locale
|
|
38
39
|
@options.fetch(:locale, ::CountrySelect::DEFAULTS[:locale])
|
|
39
40
|
end
|
|
@@ -59,59 +60,63 @@ module CountrySelect
|
|
|
59
60
|
end
|
|
60
61
|
|
|
61
62
|
def country_options
|
|
62
|
-
country_options_for(all_country_codes, true)
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def all_country_codes
|
|
66
63
|
codes = ISO3166::Country.codes
|
|
67
64
|
|
|
68
65
|
if only_country_codes.present?
|
|
69
|
-
codes &
|
|
70
|
-
|
|
71
|
-
codes - except_country_codes
|
|
66
|
+
codes = only_country_codes & codes
|
|
67
|
+
sort = @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided])
|
|
72
68
|
else
|
|
73
|
-
codes
|
|
69
|
+
codes -= except_country_codes if except_country_codes.present?
|
|
70
|
+
sort = true
|
|
74
71
|
end
|
|
72
|
+
|
|
73
|
+
country_options_for(codes, sorted: sort)
|
|
75
74
|
end
|
|
76
75
|
|
|
77
|
-
def country_options_for(country_codes, sorted
|
|
76
|
+
def country_options_for(country_codes, sorted: true)
|
|
78
77
|
I18n.with_locale(locale) do
|
|
79
|
-
country_list = country_codes.map
|
|
80
|
-
if country = ISO3166::Country.new(code_or_name)
|
|
81
|
-
code = country.alpha2
|
|
82
|
-
elsif country = ISO3166::Country.find_country_by_any_name(code_or_name)
|
|
83
|
-
code = country.alpha2
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
unless country.present?
|
|
87
|
-
msg = "Could not find Country with string '#{code_or_name}'"
|
|
88
|
-
raise CountryNotFoundError.new(msg)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
formatted_country = ::CountrySelect::FORMATS[format].call(country)
|
|
92
|
-
|
|
93
|
-
if formatted_country.is_a?(Array)
|
|
94
|
-
formatted_country
|
|
95
|
-
else
|
|
96
|
-
[formatted_country, code]
|
|
97
|
-
end
|
|
78
|
+
country_list = country_codes.map { |code_or_name| get_formatted_country(code_or_name) }
|
|
98
79
|
|
|
99
|
-
|
|
80
|
+
country_list.sort_by! { |name, _| [I18n.transliterate(name.to_s), name] } if sorted
|
|
81
|
+
country_list
|
|
82
|
+
end
|
|
83
|
+
end
|
|
100
84
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
end
|
|
85
|
+
def options_for_select_with_priority_countries(country_options, tags_options)
|
|
86
|
+
sorted = @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided])
|
|
87
|
+
priority_countries_options = country_options_for(priority_countries, sorted: sorted)
|
|
88
|
+
|
|
89
|
+
option_tags = priority_options_for_select(priority_countries_options, tags_options)
|
|
90
|
+
|
|
91
|
+
tags_options[:selected] = Array(tags_options[:selected]).delete_if do |selected|
|
|
92
|
+
priority_countries_options.map(&:second).include?(selected)
|
|
110
93
|
end
|
|
94
|
+
|
|
95
|
+
option_tags += "\n".html_safe + options_for_select(country_options, tags_options)
|
|
96
|
+
|
|
97
|
+
option_tags
|
|
111
98
|
end
|
|
112
99
|
|
|
113
|
-
def
|
|
114
|
-
|
|
100
|
+
def priority_options_for_select(priority_countries_options, tags_options)
|
|
101
|
+
option_tags = options_for_select(priority_countries_options, tags_options)
|
|
102
|
+
option_tags += "\n".html_safe +
|
|
103
|
+
options_for_select([priority_countries_divider], disabled: priority_countries_divider)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def get_formatted_country(code_or_name)
|
|
107
|
+
country = ISO3166::Country.new(code_or_name) ||
|
|
108
|
+
ISO3166::Country.find_country_by_any_name(code_or_name)
|
|
109
|
+
|
|
110
|
+
raise(CountryNotFoundError, "Could not find Country with string '#{code_or_name}'") unless country.present?
|
|
111
|
+
|
|
112
|
+
code = country.alpha2
|
|
113
|
+
formatted_country = ::CountrySelect::FORMATS[format].call(country)
|
|
114
|
+
|
|
115
|
+
if formatted_country.is_a?(Array)
|
|
116
|
+
formatted_country
|
|
117
|
+
else
|
|
118
|
+
[formatted_country, code]
|
|
119
|
+
end
|
|
115
120
|
end
|
|
116
121
|
end
|
|
117
122
|
end
|
data/lib/country_select.rb
CHANGED
data/spec/country_select_spec.rb
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
5
|
require 'action_view'
|
|
6
6
|
require 'country_select'
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
class Walrus
|
|
9
|
+
attr_accessor :country_code
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe 'CountrySelect' do
|
|
9
13
|
include ActionView::Helpers::TagHelper
|
|
10
14
|
include ActionView::Helpers::FormOptionsHelper
|
|
11
15
|
|
|
@@ -15,10 +19,6 @@ describe "CountrySelect" do
|
|
|
15
19
|
ISO3166.reset
|
|
16
20
|
end
|
|
17
21
|
|
|
18
|
-
class Walrus
|
|
19
|
-
attr_accessor :country_code
|
|
20
|
-
end
|
|
21
|
-
|
|
22
22
|
let(:walrus) { Walrus.new }
|
|
23
23
|
let!(:template) { ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil) }
|
|
24
24
|
|
|
@@ -26,17 +26,17 @@ describe "CountrySelect" do
|
|
|
26
26
|
if defined?(ActionView::Helpers::Tags::Base)
|
|
27
27
|
ActionView::Helpers::FormBuilder.new(:walrus, walrus, template, {})
|
|
28
28
|
else
|
|
29
|
-
ActionView::Helpers::FormBuilder.new(:walrus, walrus, template, {},
|
|
29
|
+
ActionView::Helpers::FormBuilder.new(:walrus, walrus, template, {}, proc {})
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
let(:select_tag) do
|
|
34
|
-
<<-
|
|
34
|
+
<<-HTML.chomp.strip
|
|
35
35
|
<select id="walrus_country_code" name="walrus[country_code]">
|
|
36
|
-
|
|
36
|
+
HTML
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
it
|
|
39
|
+
it 'selects the value of country_code' do
|
|
40
40
|
tag = options_for_select([['United States', 'US']], 'US')
|
|
41
41
|
|
|
42
42
|
walrus.country_code = 'US'
|
|
@@ -44,8 +44,8 @@ describe "CountrySelect" do
|
|
|
44
44
|
expect(t).to include(tag)
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
it
|
|
48
|
-
I18n.available_locales = [
|
|
47
|
+
it 'uses the locale specified by I18n.locale' do
|
|
48
|
+
I18n.available_locales = %i[en es]
|
|
49
49
|
ISO3166.reset
|
|
50
50
|
|
|
51
51
|
tag = options_for_select([['Estados Unidos', 'US']], 'US')
|
|
@@ -62,10 +62,10 @@ describe "CountrySelect" do
|
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
it 'falls back when given a country-specific locale' do
|
|
65
|
-
I18n.available_locales = [
|
|
65
|
+
I18n.available_locales = %i[en de de-AT]
|
|
66
66
|
ISO3166.reset
|
|
67
67
|
|
|
68
|
-
tag = options_for_select([[
|
|
68
|
+
tag = options_for_select([%w[Deutschland DE]], 'DE')
|
|
69
69
|
|
|
70
70
|
walrus.country_code = 'DE'
|
|
71
71
|
original_locale = I18n.locale
|
|
@@ -78,76 +78,142 @@ describe "CountrySelect" do
|
|
|
78
78
|
end
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
-
it
|
|
81
|
+
it 'accepts a locale option' do
|
|
82
82
|
I18n.available_locales = [:fr]
|
|
83
83
|
ISO3166.reset
|
|
84
84
|
|
|
85
|
-
tag = options_for_select([[
|
|
85
|
+
tag = options_for_select([%w[États-Unis US]], 'US')
|
|
86
86
|
|
|
87
87
|
walrus.country_code = 'US'
|
|
88
88
|
t = builder.country_select(:country_code, locale: :fr)
|
|
89
89
|
expect(t).to include(tag)
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
-
it
|
|
92
|
+
it 'accepts priority countries' do
|
|
93
|
+
tag = options_for_select(
|
|
94
|
+
[
|
|
95
|
+
['Denmark', 'DK'],
|
|
96
|
+
['Latvia', 'LV'],
|
|
97
|
+
['United States', 'US'],
|
|
98
|
+
['-' * 15, '-' * 15]
|
|
99
|
+
],
|
|
100
|
+
selected: 'US',
|
|
101
|
+
disabled: '-' * 15
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
walrus.country_code = 'US'
|
|
105
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US DK])
|
|
106
|
+
expect(t).to include(tag)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it 'priority countries are sorted by name by default' do
|
|
110
|
+
tag = options_for_select(
|
|
111
|
+
[
|
|
112
|
+
['Denmark', 'DK'],
|
|
113
|
+
['Latvia', 'LV'],
|
|
114
|
+
['United States', 'US'],
|
|
115
|
+
['-' * 15, '-' * 15]
|
|
116
|
+
],
|
|
117
|
+
selected: 'US',
|
|
118
|
+
disabled: '-' * 15
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
walrus.country_code = 'US'
|
|
122
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US DK])
|
|
123
|
+
expect(t).to include(tag)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it 'priority countries with `sort_provided: false` preserves the provided order' do
|
|
93
127
|
tag = options_for_select(
|
|
94
128
|
[
|
|
95
|
-
['Latvia','LV'],
|
|
96
|
-
['United States','US'],
|
|
129
|
+
['Latvia', 'LV'],
|
|
130
|
+
['United States', 'US'],
|
|
97
131
|
['Denmark', 'DK'],
|
|
98
|
-
['-'*15,'-'*15]
|
|
132
|
+
['-' * 15, '-' * 15]
|
|
99
133
|
],
|
|
100
134
|
selected: 'US',
|
|
101
|
-
disabled: '-'*15
|
|
135
|
+
disabled: '-' * 15
|
|
102
136
|
)
|
|
103
137
|
|
|
104
138
|
walrus.country_code = 'US'
|
|
105
|
-
t = builder.country_select(:country_code, priority_countries: [
|
|
139
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US DK], sort_provided: false)
|
|
140
|
+
expect(t).to include(tag)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it 'priority countries with `sort_provided: false` still sorts the non-priority countries by name' do
|
|
144
|
+
tag = options_for_select(
|
|
145
|
+
[
|
|
146
|
+
['Latvia', 'LV'],
|
|
147
|
+
['United States', 'US'],
|
|
148
|
+
['Denmark', 'DK'],
|
|
149
|
+
['-' * 15, '-' * 15],
|
|
150
|
+
['Afghanistan', 'AF']
|
|
151
|
+
],
|
|
152
|
+
selected: 'AF',
|
|
153
|
+
disabled: '-' * 15
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
walrus.country_code = 'AF'
|
|
157
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US DK], sort_provided: false)
|
|
106
158
|
expect(t).to include(tag)
|
|
107
159
|
end
|
|
108
160
|
|
|
109
|
-
describe
|
|
110
|
-
it
|
|
111
|
-
tag = options_for_select([[
|
|
161
|
+
describe 'when selected options is not an array' do
|
|
162
|
+
it 'selects only the first matching option' do
|
|
163
|
+
tag = options_for_select([['United States', 'US'], ['Uruguay', 'UY']], 'US')
|
|
112
164
|
walrus.country_code = 'US'
|
|
113
|
-
t = builder.country_select(:country_code, priority_countries: [
|
|
165
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US])
|
|
114
166
|
expect(t).to_not include(tag)
|
|
115
167
|
end
|
|
116
168
|
end
|
|
117
169
|
|
|
118
|
-
describe
|
|
119
|
-
it
|
|
170
|
+
describe 'when selected options is an array' do
|
|
171
|
+
it 'selects all options but only once' do
|
|
120
172
|
walrus.country_code = 'US'
|
|
121
|
-
t = builder.country_select(:country_code,
|
|
122
|
-
|
|
123
|
-
|
|
173
|
+
t = builder.country_select(:country_code,
|
|
174
|
+
{ priority_countries: %w[LV US ES], selected: %w[UY US] },
|
|
175
|
+
multiple: true)
|
|
176
|
+
expect(t.scan(options_for_select([['United States', 'US']], 'US')).length).to be(1)
|
|
177
|
+
expect(t.scan(options_for_select([%w[Uruguay UY]], 'UY')).length).to be(1)
|
|
124
178
|
end
|
|
125
179
|
end
|
|
126
180
|
|
|
127
|
-
it
|
|
128
|
-
options = [[
|
|
181
|
+
it 'displays only the chosen countries' do
|
|
182
|
+
options = [%w[Denmark DK], %w[Germany DE]]
|
|
129
183
|
tag = builder.select(:country_code, options)
|
|
130
184
|
walrus.country_code = 'US'
|
|
131
|
-
t = builder.country_select(:country_code, only: [
|
|
185
|
+
t = builder.country_select(:country_code, only: %w[DK DE])
|
|
132
186
|
expect(t).to eql(tag)
|
|
133
187
|
end
|
|
134
188
|
|
|
135
|
-
it
|
|
136
|
-
tag = options_for_select([[
|
|
189
|
+
it 'discards some countries' do
|
|
190
|
+
tag = options_for_select([['United States', 'US']])
|
|
137
191
|
walrus.country_code = 'DE'
|
|
138
192
|
t = builder.country_select(:country_code, except: ['US'])
|
|
139
193
|
expect(t).to_not include(tag)
|
|
140
194
|
end
|
|
141
195
|
|
|
196
|
+
it 'countries provided in `only` are sorted by name by default' do
|
|
197
|
+
t = builder.country_select(:country_code, only: %w[PT DE AR])
|
|
198
|
+
order = t.scan(/value="(\w{2})"/).map { |o| o[0] }
|
|
199
|
+
expect(order).to eq(%w[AR DE PT])
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it 'countries provided in `only` with `sort_provided` to false keeps the order of the provided countries' do
|
|
203
|
+
t = builder.country_select(:country_code, only: %w[PT DE AR], sort_provided: false)
|
|
204
|
+
order = t.scan(/value="(\w{2})"/).map { |o| o[0] }
|
|
205
|
+
expect(order).to eq(%w[PT DE AR])
|
|
206
|
+
end
|
|
207
|
+
|
|
142
208
|
context "when there is a default 'except' configured" do
|
|
143
209
|
around do |example|
|
|
144
|
-
old_value =
|
|
210
|
+
old_value = CountrySelect::DEFAULTS[:except]
|
|
145
211
|
example.run
|
|
146
|
-
|
|
212
|
+
CountrySelect::DEFAULTS[:except] = old_value
|
|
147
213
|
end
|
|
148
214
|
|
|
149
|
-
it
|
|
150
|
-
|
|
215
|
+
it 'discards countries when configured to' do
|
|
216
|
+
CountrySelect::DEFAULTS[:except] = ['US']
|
|
151
217
|
|
|
152
218
|
tag = options_for_select([['United States', 'US']])
|
|
153
219
|
walrus.country_code = 'DE'
|
|
@@ -156,51 +222,49 @@ describe "CountrySelect" do
|
|
|
156
222
|
end
|
|
157
223
|
end
|
|
158
224
|
|
|
159
|
-
context
|
|
160
|
-
it
|
|
225
|
+
context 'using old 1.x syntax' do
|
|
226
|
+
it 'accepts priority countries' do
|
|
161
227
|
tag = options_for_select(
|
|
162
228
|
[
|
|
163
|
-
['Latvia','LV'],
|
|
164
|
-
['United States','US'],
|
|
165
229
|
['Denmark', 'DK'],
|
|
166
|
-
['
|
|
230
|
+
['Latvia', 'LV'],
|
|
231
|
+
['United States', 'US'],
|
|
232
|
+
['-' * 15, '-' * 15]
|
|
167
233
|
],
|
|
168
234
|
selected: 'US',
|
|
169
|
-
disabled: '-'*15
|
|
235
|
+
disabled: '-' * 15
|
|
170
236
|
)
|
|
171
237
|
|
|
172
238
|
walrus.country_code = 'US'
|
|
173
|
-
t = builder.country_select(:country_code, [
|
|
239
|
+
t = builder.country_select(:country_code, %w[LV US DK])
|
|
174
240
|
expect(t).to include(tag)
|
|
175
241
|
end
|
|
176
242
|
|
|
177
|
-
it
|
|
178
|
-
tag = options_for_select([[
|
|
243
|
+
it 'selects only the first matching option' do
|
|
244
|
+
tag = options_for_select([['United States', 'US'], ['Uruguay', 'UY']], 'US')
|
|
179
245
|
walrus.country_code = 'US'
|
|
180
|
-
t = builder.country_select(:country_code, [
|
|
246
|
+
t = builder.country_select(:country_code, %w[LV US])
|
|
181
247
|
expect(t).to_not include(tag)
|
|
182
248
|
end
|
|
183
249
|
|
|
184
|
-
it
|
|
185
|
-
tag = options_for_select([
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
])
|
|
191
|
-
country_names = ["Australia", "Canada", "United Kingdom", "United States"]
|
|
250
|
+
it 'supports the country names as provided by default in Formtastic' do
|
|
251
|
+
tag = options_for_select([['Australia', 'AU'],
|
|
252
|
+
['Canada', 'CA'],
|
|
253
|
+
['United Kingdom', 'GB'],
|
|
254
|
+
['United States', 'US']])
|
|
255
|
+
country_names = ['Australia', 'Canada', 'United Kingdom', 'United States']
|
|
192
256
|
t = builder.country_select(:country_code, country_names)
|
|
193
257
|
expect(t).to include(tag)
|
|
194
258
|
end
|
|
195
259
|
|
|
196
|
-
it
|
|
260
|
+
it 'raises an error when a country code or name is not found' do
|
|
197
261
|
country_names = [
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
262
|
+
'United States',
|
|
263
|
+
'Canada',
|
|
264
|
+
'United Kingdom',
|
|
265
|
+
'Mexico',
|
|
266
|
+
'Australia',
|
|
267
|
+
'Freedonia'
|
|
204
268
|
]
|
|
205
269
|
error_msg = "Could not find Country with string 'Freedonia'"
|
|
206
270
|
|
|
@@ -209,13 +273,13 @@ describe "CountrySelect" do
|
|
|
209
273
|
end.to raise_error(CountrySelect::CountryNotFoundError, error_msg)
|
|
210
274
|
end
|
|
211
275
|
|
|
212
|
-
it
|
|
276
|
+
it 'supports the select prompt' do
|
|
213
277
|
tag = '<option value="">Select your country</option>'
|
|
214
278
|
t = builder.country_select(:country_code, prompt: 'Select your country')
|
|
215
279
|
expect(t).to include(tag)
|
|
216
280
|
end
|
|
217
281
|
|
|
218
|
-
it
|
|
282
|
+
it 'supports the include_blank option' do
|
|
219
283
|
# Rails 6.1 more closely follows the HTML spec for
|
|
220
284
|
# empty option tags.
|
|
221
285
|
# https://github.com/rails/rails/pull/39808
|
|
@@ -230,31 +294,14 @@ describe "CountrySelect" do
|
|
|
230
294
|
end
|
|
231
295
|
|
|
232
296
|
it 'sorts unicode' do
|
|
233
|
-
tag = builder.country_select(:country_code, only: [
|
|
297
|
+
tag = builder.country_select(:country_code, only: %w[AX AL AF ZW])
|
|
234
298
|
order = tag.scan(/value="(\w{2})"/).map { |o| o[0] }
|
|
235
|
-
expect(order).to eq([
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
context "without sort_alphabetical" do
|
|
239
|
-
before do
|
|
240
|
-
Enumerable.send(:alias_method, :_sort_alphabetical, :sort_alphabetical)
|
|
241
|
-
Enumerable.send(:remove_method, :sort_alphabetical)
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
after do
|
|
245
|
-
Enumerable.send(:alias_method, :sort_alphabetical, :_sort_alphabetical)
|
|
246
|
-
end
|
|
247
|
-
|
|
248
|
-
it 'falls back to regular sort' do
|
|
249
|
-
tag = builder.country_select(:country_code, only: ['AX', 'AL', 'AF', 'ZW'])
|
|
250
|
-
order = tag.scan(/value="(\w{2})"/).map { |o| o[0] }
|
|
251
|
-
expect(order).to eq(['AF', 'AL', 'ZW', 'AX'])
|
|
252
|
-
end
|
|
299
|
+
expect(order).to eq(%w[AF AX AL ZW])
|
|
253
300
|
end
|
|
254
301
|
|
|
255
|
-
describe
|
|
256
|
-
it
|
|
257
|
-
|
|
302
|
+
describe 'custom formats' do
|
|
303
|
+
it 'accepts a custom formatter' do
|
|
304
|
+
CountrySelect::FORMATS[:with_alpha2] = lambda do |country|
|
|
258
305
|
"#{country.iso_short_name} (#{country.alpha2})"
|
|
259
306
|
end
|
|
260
307
|
|
|
@@ -265,8 +312,8 @@ describe "CountrySelect" do
|
|
|
265
312
|
expect(t).to include(tag)
|
|
266
313
|
end
|
|
267
314
|
|
|
268
|
-
it
|
|
269
|
-
|
|
315
|
+
it 'accepts an array for formatter' do
|
|
316
|
+
CountrySelect::FORMATS[:with_alpha3] = lambda do |country|
|
|
270
317
|
[country.iso_short_name, country.alpha3]
|
|
271
318
|
end
|
|
272
319
|
|
|
@@ -276,8 +323,8 @@ describe "CountrySelect" do
|
|
|
276
323
|
expect(t).to include(tag)
|
|
277
324
|
end
|
|
278
325
|
|
|
279
|
-
it
|
|
280
|
-
|
|
326
|
+
it 'accepts an array for formatter + custom formatter' do
|
|
327
|
+
CountrySelect::FORMATS[:with_alpha3] = lambda do |country|
|
|
281
328
|
["#{country.iso_short_name} (#{country.alpha2})", country.alpha3]
|
|
282
329
|
end
|
|
283
330
|
|
|
@@ -287,8 +334,8 @@ describe "CountrySelect" do
|
|
|
287
334
|
expect(t).to include(tag)
|
|
288
335
|
end
|
|
289
336
|
|
|
290
|
-
it
|
|
291
|
-
|
|
337
|
+
it 'marks priority countries as selected only once' do
|
|
338
|
+
CountrySelect::FORMATS[:with_alpha3] = lambda do |country|
|
|
292
339
|
[country.iso_short_name, country.alpha3]
|
|
293
340
|
end
|
|
294
341
|
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'pry'
|
|
4
|
+
require 'simplecov'
|
|
5
|
+
SimpleCov.start
|
|
2
6
|
|
|
3
7
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
|
4
8
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
|
@@ -55,11 +59,11 @@ RSpec.configure do |config|
|
|
|
55
59
|
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
|
56
60
|
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
|
57
61
|
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
|
58
|
-
#config.disable_monkey_patching!
|
|
62
|
+
# config.disable_monkey_patching!
|
|
59
63
|
|
|
60
64
|
# This setting enables warnings. It's recommended, but in some cases may
|
|
61
65
|
# be too noisy due to issues in dependencies.
|
|
62
|
-
#config.warnings = true
|
|
66
|
+
# config.warnings = true
|
|
63
67
|
|
|
64
68
|
# Many RSpec users commonly either run the entire suite or an individual
|
|
65
69
|
# file, and it's useful to allow more verbose output when running an
|
|
@@ -74,7 +78,7 @@ RSpec.configure do |config|
|
|
|
74
78
|
# Print the 10 slowest examples and example groups at the
|
|
75
79
|
# end of the spec run, to help surface which specs are running
|
|
76
80
|
# particularly slow.
|
|
77
|
-
#config.profile_examples = 10
|
|
81
|
+
# config.profile_examples = 10
|
|
78
82
|
|
|
79
83
|
# Run specs in random order to surface order dependencies. If you find an
|
|
80
84
|
# order dependency and want to debug it, you can fix the order by providing
|