country_select 8.0.0 → 8.0.2
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 +14 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +56 -40
- data/README.md +2 -8
- data/Rakefile +11 -14
- data/country_select.gemspec +12 -8
- data/gemfiles/actionpack-5.2.gemfile +7 -4
- data/gemfiles/actionpack-5.2.gemfile.lock +52 -40
- data/gemfiles/actionpack-6.0.gemfile +7 -4
- data/gemfiles/actionpack-6.0.gemfile.lock +53 -41
- data/gemfiles/actionpack-6.1.gemfile +7 -4
- data/gemfiles/actionpack-6.1.gemfile.lock +53 -41
- data/gemfiles/actionpack-7.0.gemfile +7 -4
- data/gemfiles/actionpack-7.0.gemfile.lock +53 -41
- data/gemfiles/actionpack-7.1.gemfile +9 -0
- data/gemfiles/actionpack-7.1.gemfile.lock +133 -0
- data/lib/country_select/country_select_helper.rb +10 -3
- data/lib/country_select/defaults.rb +3 -1
- data/lib/country_select/formats.rb +2 -0
- data/lib/country_select/tag_helper.rb +59 -50
- data/lib/country_select/version.rb +3 -1
- data/lib/country_select.rb +1 -1
- data/spec/country_select_spec.rb +114 -96
- data/spec/spec_helper.rb +7 -3
- metadata +28 -10
- data/.github/workflows/build.yml +0 -25
@@ -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,55 +60,63 @@ module CountrySelect
|
|
59
60
|
end
|
60
61
|
|
61
62
|
def country_options
|
62
|
-
country_options_for(all_country_codes, @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided]))
|
63
|
-
end
|
64
|
-
|
65
|
-
def all_country_codes
|
66
63
|
codes = ISO3166::Country.codes
|
67
64
|
|
68
65
|
if only_country_codes.present?
|
69
|
-
only_country_codes & 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: rue)
|
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), name] } if sorted
|
81
|
+
country_list
|
82
|
+
end
|
83
|
+
end
|
100
84
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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)
|
106
93
|
end
|
94
|
+
|
95
|
+
option_tags += "\n".html_safe + options_for_select(country_options, tags_options)
|
96
|
+
|
97
|
+
option_tags
|
107
98
|
end
|
108
99
|
|
109
|
-
def
|
110
|
-
|
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
|
111
120
|
end
|
112
121
|
end
|
113
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,122 +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
93
|
tag = options_for_select(
|
94
94
|
[
|
95
95
|
['Denmark', 'DK'],
|
96
|
-
['Latvia','LV'],
|
97
|
-
['United States','US'],
|
98
|
-
['-'*15,'-'*15]
|
96
|
+
['Latvia', 'LV'],
|
97
|
+
['United States', 'US'],
|
98
|
+
['-' * 15, '-' * 15]
|
99
99
|
],
|
100
100
|
selected: 'US',
|
101
|
-
disabled: '-'*15
|
101
|
+
disabled: '-' * 15
|
102
102
|
)
|
103
103
|
|
104
104
|
walrus.country_code = 'US'
|
105
|
-
t = builder.country_select(:country_code, priority_countries: [
|
105
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US DK])
|
106
106
|
expect(t).to include(tag)
|
107
107
|
end
|
108
108
|
|
109
|
-
it
|
109
|
+
it 'priority countries are sorted by name by default' do
|
110
110
|
tag = options_for_select(
|
111
111
|
[
|
112
112
|
['Denmark', 'DK'],
|
113
|
-
['Latvia','LV'],
|
114
|
-
['United States','US'],
|
115
|
-
['-'*15,'-'*15]
|
113
|
+
['Latvia', 'LV'],
|
114
|
+
['United States', 'US'],
|
115
|
+
['-' * 15, '-' * 15]
|
116
116
|
],
|
117
117
|
selected: 'US',
|
118
|
-
disabled: '-'*15
|
118
|
+
disabled: '-' * 15
|
119
119
|
)
|
120
120
|
|
121
121
|
walrus.country_code = 'US'
|
122
|
-
t = builder.country_select(:country_code, priority_countries: [
|
122
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US DK])
|
123
123
|
expect(t).to include(tag)
|
124
124
|
end
|
125
125
|
|
126
|
-
it
|
126
|
+
it 'priority countries with `sort_provided: false` preserves the provided order' do
|
127
127
|
tag = options_for_select(
|
128
128
|
[
|
129
|
-
['Latvia','LV'],
|
130
|
-
['United States','US'],
|
129
|
+
['Latvia', 'LV'],
|
130
|
+
['United States', 'US'],
|
131
131
|
['Denmark', 'DK'],
|
132
|
-
['-'*15,'-'*15]
|
132
|
+
['-' * 15, '-' * 15]
|
133
133
|
],
|
134
134
|
selected: 'US',
|
135
|
-
disabled: '-'*15
|
135
|
+
disabled: '-' * 15
|
136
136
|
)
|
137
137
|
|
138
138
|
walrus.country_code = 'US'
|
139
|
-
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)
|
140
158
|
expect(t).to include(tag)
|
141
159
|
end
|
142
160
|
|
143
|
-
describe
|
144
|
-
it
|
145
|
-
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')
|
146
164
|
walrus.country_code = 'US'
|
147
|
-
t = builder.country_select(:country_code, priority_countries: [
|
165
|
+
t = builder.country_select(:country_code, priority_countries: %w[LV US])
|
148
166
|
expect(t).to_not include(tag)
|
149
167
|
end
|
150
168
|
end
|
151
169
|
|
152
|
-
describe
|
153
|
-
it
|
170
|
+
describe 'when selected options is an array' do
|
171
|
+
it 'selects all options but only once' do
|
154
172
|
walrus.country_code = 'US'
|
155
|
-
t = builder.country_select(:country_code,
|
156
|
-
|
157
|
-
|
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)
|
158
178
|
end
|
159
179
|
end
|
160
180
|
|
161
|
-
it
|
162
|
-
options = [[
|
181
|
+
it 'displays only the chosen countries' do
|
182
|
+
options = [%w[Denmark DK], %w[Germany DE]]
|
163
183
|
tag = builder.select(:country_code, options)
|
164
184
|
walrus.country_code = 'US'
|
165
|
-
t = builder.country_select(:country_code, only: [
|
185
|
+
t = builder.country_select(:country_code, only: %w[DK DE])
|
166
186
|
expect(t).to eql(tag)
|
167
187
|
end
|
168
188
|
|
169
|
-
it
|
170
|
-
tag = options_for_select([[
|
189
|
+
it 'discards some countries' do
|
190
|
+
tag = options_for_select([['United States', 'US']])
|
171
191
|
walrus.country_code = 'DE'
|
172
192
|
t = builder.country_select(:country_code, except: ['US'])
|
173
193
|
expect(t).to_not include(tag)
|
174
194
|
end
|
175
195
|
|
176
|
-
it
|
177
|
-
t = builder.country_select(:country_code, only: [
|
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])
|
178
198
|
order = t.scan(/value="(\w{2})"/).map { |o| o[0] }
|
179
|
-
expect(order).to eq([
|
199
|
+
expect(order).to eq(%w[AR DE PT])
|
180
200
|
end
|
181
201
|
|
182
|
-
it
|
183
|
-
t = builder.country_select(:country_code, only: [
|
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)
|
184
204
|
order = t.scan(/value="(\w{2})"/).map { |o| o[0] }
|
185
|
-
expect(order).to eq([
|
205
|
+
expect(order).to eq(%w[PT DE AR])
|
186
206
|
end
|
187
207
|
|
188
208
|
context "when there is a default 'except' configured" do
|
189
209
|
around do |example|
|
190
|
-
old_value =
|
210
|
+
old_value = CountrySelect::DEFAULTS[:except]
|
191
211
|
example.run
|
192
|
-
|
212
|
+
CountrySelect::DEFAULTS[:except] = old_value
|
193
213
|
end
|
194
214
|
|
195
|
-
it
|
196
|
-
|
215
|
+
it 'discards countries when configured to' do
|
216
|
+
CountrySelect::DEFAULTS[:except] = ['US']
|
197
217
|
|
198
218
|
tag = options_for_select([['United States', 'US']])
|
199
219
|
walrus.country_code = 'DE'
|
@@ -202,51 +222,49 @@ describe "CountrySelect" do
|
|
202
222
|
end
|
203
223
|
end
|
204
224
|
|
205
|
-
context
|
206
|
-
it
|
225
|
+
context 'using old 1.x syntax' do
|
226
|
+
it 'accepts priority countries' do
|
207
227
|
tag = options_for_select(
|
208
228
|
[
|
209
229
|
['Denmark', 'DK'],
|
210
|
-
['Latvia','LV'],
|
211
|
-
['United States','US'],
|
212
|
-
['-'*15,'-'*15]
|
230
|
+
['Latvia', 'LV'],
|
231
|
+
['United States', 'US'],
|
232
|
+
['-' * 15, '-' * 15]
|
213
233
|
],
|
214
234
|
selected: 'US',
|
215
|
-
disabled: '-'*15
|
235
|
+
disabled: '-' * 15
|
216
236
|
)
|
217
237
|
|
218
238
|
walrus.country_code = 'US'
|
219
|
-
t = builder.country_select(:country_code, [
|
239
|
+
t = builder.country_select(:country_code, %w[LV US DK])
|
220
240
|
expect(t).to include(tag)
|
221
241
|
end
|
222
242
|
|
223
|
-
it
|
224
|
-
tag = options_for_select([[
|
243
|
+
it 'selects only the first matching option' do
|
244
|
+
tag = options_for_select([['United States', 'US'], ['Uruguay', 'UY']], 'US')
|
225
245
|
walrus.country_code = 'US'
|
226
|
-
t = builder.country_select(:country_code, [
|
246
|
+
t = builder.country_select(:country_code, %w[LV US])
|
227
247
|
expect(t).to_not include(tag)
|
228
248
|
end
|
229
249
|
|
230
|
-
it
|
231
|
-
tag = options_for_select([
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
])
|
237
|
-
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']
|
238
256
|
t = builder.country_select(:country_code, country_names)
|
239
257
|
expect(t).to include(tag)
|
240
258
|
end
|
241
259
|
|
242
|
-
it
|
260
|
+
it 'raises an error when a country code or name is not found' do
|
243
261
|
country_names = [
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
262
|
+
'United States',
|
263
|
+
'Canada',
|
264
|
+
'United Kingdom',
|
265
|
+
'Mexico',
|
266
|
+
'Australia',
|
267
|
+
'Freedonia'
|
250
268
|
]
|
251
269
|
error_msg = "Could not find Country with string 'Freedonia'"
|
252
270
|
|
@@ -255,13 +273,13 @@ describe "CountrySelect" do
|
|
255
273
|
end.to raise_error(CountrySelect::CountryNotFoundError, error_msg)
|
256
274
|
end
|
257
275
|
|
258
|
-
it
|
276
|
+
it 'supports the select prompt' do
|
259
277
|
tag = '<option value="">Select your country</option>'
|
260
278
|
t = builder.country_select(:country_code, prompt: 'Select your country')
|
261
279
|
expect(t).to include(tag)
|
262
280
|
end
|
263
281
|
|
264
|
-
it
|
282
|
+
it 'supports the include_blank option' do
|
265
283
|
# Rails 6.1 more closely follows the HTML spec for
|
266
284
|
# empty option tags.
|
267
285
|
# https://github.com/rails/rails/pull/39808
|
@@ -276,14 +294,14 @@ describe "CountrySelect" do
|
|
276
294
|
end
|
277
295
|
|
278
296
|
it 'sorts unicode' do
|
279
|
-
tag = builder.country_select(:country_code, only: [
|
297
|
+
tag = builder.country_select(:country_code, only: %w[AX AL AF ZW])
|
280
298
|
order = tag.scan(/value="(\w{2})"/).map { |o| o[0] }
|
281
|
-
expect(order).to eq([
|
299
|
+
expect(order).to eq(%w[AF AX AL ZW])
|
282
300
|
end
|
283
301
|
|
284
|
-
describe
|
285
|
-
it
|
286
|
-
|
302
|
+
describe 'custom formats' do
|
303
|
+
it 'accepts a custom formatter' do
|
304
|
+
CountrySelect::FORMATS[:with_alpha2] = lambda do |country|
|
287
305
|
"#{country.iso_short_name} (#{country.alpha2})"
|
288
306
|
end
|
289
307
|
|
@@ -294,8 +312,8 @@ describe "CountrySelect" do
|
|
294
312
|
expect(t).to include(tag)
|
295
313
|
end
|
296
314
|
|
297
|
-
it
|
298
|
-
|
315
|
+
it 'accepts an array for formatter' do
|
316
|
+
CountrySelect::FORMATS[:with_alpha3] = lambda do |country|
|
299
317
|
[country.iso_short_name, country.alpha3]
|
300
318
|
end
|
301
319
|
|
@@ -305,8 +323,8 @@ describe "CountrySelect" do
|
|
305
323
|
expect(t).to include(tag)
|
306
324
|
end
|
307
325
|
|
308
|
-
it
|
309
|
-
|
326
|
+
it 'accepts an array for formatter + custom formatter' do
|
327
|
+
CountrySelect::FORMATS[:with_alpha3] = lambda do |country|
|
310
328
|
["#{country.iso_short_name} (#{country.alpha2})", country.alpha3]
|
311
329
|
end
|
312
330
|
|
@@ -316,8 +334,8 @@ describe "CountrySelect" do
|
|
316
334
|
expect(t).to include(tag)
|
317
335
|
end
|
318
336
|
|
319
|
-
it
|
320
|
-
|
337
|
+
it 'marks priority countries as selected only once' do
|
338
|
+
CountrySelect::FORMATS[:with_alpha3] = lambda do |country|
|
321
339
|
[country.iso_short_name, country.alpha3]
|
322
340
|
end
|
323
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
|