country_select 1.2.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -42,14 +42,21 @@ module ActionView
42
42
  # have to wrap this call in a regular HTML
43
43
  # select tag.
44
44
  #
45
- def country_options_for_select(selected = nil, priority_countries = nil, use_iso_codes = false)
45
+ def country_options_for_select(selected = nil, priority_countries = nil, use_iso_codes = false, use_locale = nil)
46
46
  country_options = "".html_safe
47
47
 
48
48
  if priority_countries
49
- priority_countries_options = if use_iso_codes
49
+ priority_countries_options = if use_iso_codes || ::CountrySelect.use_iso_codes
50
+ # Codes should not be downcased, but they were previous to 1.3
51
+ # which means they should continue to be until 2.0 is released in
52
+ # order to prevent breakage with existing implementations
53
+ #
54
+ # In 2.0.0, this map should actually be upcasing the codes
55
+ priority_countries.map! { |code| code.to_s.downcase }
56
+
50
57
  priority_countries.map do |code|
51
58
  [
52
- ::CountrySelect::COUNTRIES[code],
59
+ ::CountrySelect::countries(use_locale)[code],
53
60
  code
54
61
  ]
55
62
  end
@@ -70,13 +77,13 @@ module ActionView
70
77
  selected = nil if priority_countries.include?(selected)
71
78
  end
72
79
 
73
- values = if use_iso_codes
74
- ::CountrySelect::ISO_COUNTRIES_FOR_SELECT
80
+ values = if use_iso_codes || ::CountrySelect.use_iso_codes
81
+ ::CountrySelect::countries(use_locale).invert
75
82
  else
76
- ::CountrySelect::COUNTRIES_FOR_SELECT
83
+ ::CountrySelect::countries(use_locale).values
77
84
  end
78
85
 
79
- return country_options + options_for_select(values, selected)
86
+ return country_options + options_for_select(values.sort, selected)
80
87
  end
81
88
 
82
89
  # All the countries included in the country_options output.
@@ -85,12 +92,13 @@ module ActionView
85
92
  module ToCountrySelectTag
86
93
  def to_country_select_tag(priority_countries, options, html_options)
87
94
  use_iso_codes = options.delete(:iso_codes)
95
+ use_locale = options.delete(:locale)
88
96
  html_options = html_options.stringify_keys
89
97
  add_default_name_and_id(html_options)
90
98
  value = value(object)
91
99
  content_tag("select",
92
100
  add_options(
93
- country_options_for_select(value, priority_countries, use_iso_codes),
101
+ country_options_for_select(value, priority_countries, use_iso_codes, use_locale),
94
102
  options, value
95
103
  ), html_options
96
104
  )
@@ -1,256 +1,34 @@
1
1
  # encoding: utf-8
2
+ require 'countries'
3
+
2
4
  module CountrySelect
3
- COUNTRIES = {
4
- "af" => "Afghanistan",
5
- "ax" => "Åland Islands",
6
- "al" => "Albania",
7
- "dz" => "Algeria",
8
- "as" => "American Samoa",
9
- "ad" => "Andorra",
10
- "ao" => "Angola",
11
- "ai" => "Anguilla",
12
- "aq" => "Antarctica",
13
- "ag" => "Antigua and Barbuda",
14
- "ar" => "Argentina",
15
- "am" => "Armenia",
16
- "aw" => "Aruba",
17
- "au" => "Australia",
18
- "at" => "Austria",
19
- "az" => "Azerbaijan",
20
- "bs" => "Bahamas",
21
- "bh" => "Bahrain",
22
- "bd" => "Bangladesh",
23
- "bb" => "Barbados",
24
- "by" => "Belarus",
25
- "be" => "Belgium",
26
- "bz" => "Belize",
27
- "bj" => "Benin",
28
- "bm" => "Bermuda",
29
- "bt" => "Bhutan",
30
- "bo" => "Bolivia, Plurinational State of",
31
- "bq" => "Bonaire, Sint Eustatius and Saba",
32
- "ba" => "Bosnia and Herzegovina",
33
- "bw" => "Botswana",
34
- "bv" => "Bouvet Island",
35
- "br" => "Brazil",
36
- "io" => "British Indian Ocean Territory",
37
- "bn" => "Brunei Darussalam",
38
- "bg" => "Bulgaria",
39
- "bf" => "Burkina Faso",
40
- "bi" => "Burundi",
41
- "kh" => "Cambodia",
42
- "cm" => "Cameroon",
43
- "ca" => "Canada",
44
- "cv" => "Cape Verde",
45
- "ky" => "Cayman Islands",
46
- "cf" => "Central African Republic",
47
- "td" => "Chad",
48
- "cl" => "Chile",
49
- "cn" => "China",
50
- "cx" => "Christmas Island",
51
- "cc" => "Cocos (Keeling) Islands",
52
- "co" => "Colombia",
53
- "km" => "Comoros",
54
- "cg" => "Congo",
55
- "cd" => "Congo, the Democratic Republic of the",
56
- "ck" => "Cook Islands",
57
- "cr" => "Costa Rica",
58
- "ci" => "Côte d'Ivoire",
59
- "hr" => "Croatia",
60
- "cu" => "Cuba",
61
- "cw" => "Curaçao",
62
- "cy" => "Cyprus",
63
- "cz" => "Czech Republic",
64
- "dk" => "Denmark",
65
- "dj" => "Djibouti",
66
- "dm" => "Dominica",
67
- "do" => "Dominican Republic",
68
- "ec" => "Ecuador",
69
- "eg" => "Egypt",
70
- "sv" => "El Salvador",
71
- "gq" => "Equatorial Guinea",
72
- "er" => "Eritrea",
73
- "ee" => "Estonia",
74
- "et" => "Ethiopia",
75
- "fk" => "Falkland Islands (Malvinas)",
76
- "fo" => "Faroe Islands",
77
- "fj" => "Fiji",
78
- "fi" => "Finland",
79
- "fr" => "France",
80
- "gf" => "French Guiana",
81
- "pf" => "French Polynesia",
82
- "tf" => "French Southern Territories",
83
- "ga" => "Gabon",
84
- "gm" => "Gambia",
85
- "ge" => "Georgia",
86
- "de" => "Germany",
87
- "gh" => "Ghana",
88
- "gi" => "Gibraltar",
89
- "gr" => "Greece",
90
- "gl" => "Greenland",
91
- "gd" => "Grenada",
92
- "gp" => "Guadeloupe",
93
- "gu" => "Guam",
94
- "gt" => "Guatemala",
95
- "gg" => "Guernsey",
96
- "gn" => "Guinea",
97
- "gw" => "Guinea-Bissau",
98
- "gy" => "Guyana",
99
- "ht" => "Haiti",
100
- "hm" => "Heard Island and McDonald Islands",
101
- "va" => "Holy See (Vatican City State)",
102
- "hn" => "Honduras",
103
- "hk" => "Hong Kong",
104
- "hu" => "Hungary",
105
- "is" => "Iceland",
106
- "in" => "India",
107
- "id" => "Indonesia",
108
- "ir" => "Iran, Islamic Republic of",
109
- "iq" => "Iraq",
110
- "ie" => "Ireland",
111
- "im" => "Isle of Man",
112
- "il" => "Israel",
113
- "it" => "Italy",
114
- "jm" => "Jamaica",
115
- "jp" => "Japan",
116
- "je" => "Jersey",
117
- "jo" => "Jordan",
118
- "kz" => "Kazakhstan",
119
- "ke" => "Kenya",
120
- "ki" => "Kiribati",
121
- "kp" => "Korea, Democratic People's Republic of",
122
- "kr" => "Korea, Republic of",
123
- "kw" => "Kuwait",
124
- "kg" => "Kyrgyzstan",
125
- "la" => "Lao People's Democratic Republic",
126
- "lv" => "Latvia",
127
- "lb" => "Lebanon",
128
- "ls" => "Lesotho",
129
- "lr" => "Liberia",
130
- "ly" => "Libya",
131
- "li" => "Liechtenstein",
132
- "lt" => "Lithuania",
133
- "lu" => "Luxembourg",
134
- "mo" => "Macao",
135
- "mk" => "Macedonia, the former Yugoslav Republic of",
136
- "mg" => "Madagascar",
137
- "mw" => "Malawi",
138
- "my" => "Malaysia",
139
- "mv" => "Maldives",
140
- "ml" => "Mali",
141
- "mt" => "Malta",
142
- "mh" => "Marshall Islands",
143
- "mq" => "Martinique",
144
- "mr" => "Mauritania",
145
- "mu" => "Mauritius",
146
- "yt" => "Mayotte",
147
- "mx" => "Mexico",
148
- "fm" => "Micronesia, Federated States of",
149
- "md" => "Moldova, Republic of",
150
- "mc" => "Monaco",
151
- "mn" => "Mongolia",
152
- "me" => "Montenegro",
153
- "ms" => "Montserrat",
154
- "ma" => "Morocco",
155
- "mz" => "Mozambique",
156
- "mm" => "Myanmar",
157
- "na" => "Namibia",
158
- "nr" => "Nauru",
159
- "np" => "Nepal",
160
- "nl" => "Netherlands",
161
- "nc" => "New Caledonia",
162
- "nz" => "New Zealand",
163
- "ni" => "Nicaragua",
164
- "ne" => "Niger",
165
- "ng" => "Nigeria",
166
- "nu" => "Niue",
167
- "nf" => "Norfolk Island",
168
- "mp" => "Northern Mariana Islands",
169
- "no" => "Norway",
170
- "om" => "Oman",
171
- "pk" => "Pakistan",
172
- "pw" => "Palau",
173
- "ps" => "Palestine, State of",
174
- "pa" => "Panama",
175
- "pg" => "Papua New Guinea",
176
- "py" => "Paraguay",
177
- "pe" => "Peru",
178
- "ph" => "Philippines",
179
- "pn" => "Pitcairn",
180
- "pl" => "Poland",
181
- "pt" => "Portugal",
182
- "pr" => "Puerto Rico",
183
- "qa" => "Qatar",
184
- "re" => "Réunion",
185
- "ro" => "Romania",
186
- "ru" => "Russian Federation",
187
- "rw" => "Rwanda",
188
- "bl" => "Saint Barthélemy",
189
- "sh" => "Saint Helena, Ascension and Tristan da Cunha",
190
- "kn" => "Saint Kitts and Nevis",
191
- "lc" => "Saint Lucia",
192
- "mf" => "Saint Martin (French part)",
193
- "pm" => "Saint Pierre and Miquelon",
194
- "vc" => "Saint Vincent and the Grenadines",
195
- "ws" => "Samoa",
196
- "sm" => "San Marino",
197
- "st" => "Sao Tome and Principe",
198
- "sa" => "Saudi Arabia",
199
- "sn" => "Senegal",
200
- "rs" => "Serbia",
201
- "sc" => "Seychelles",
202
- "sl" => "Sierra Leone",
203
- "sg" => "Singapore",
204
- "sx" => "Sint Maarten (Dutch part)",
205
- "sk" => "Slovakia",
206
- "si" => "Slovenia",
207
- "sb" => "Solomon Islands",
208
- "so" => "Somalia",
209
- "za" => "South Africa",
210
- "gs" => "South Georgia and the South Sandwich Islands",
211
- "ss" => "South Sudan",
212
- "es" => "Spain",
213
- "lk" => "Sri Lanka",
214
- "sd" => "Sudan",
215
- "sr" => "Suriname",
216
- "sj" => "Svalbard and Jan Mayen",
217
- "sz" => "Swaziland",
218
- "se" => "Sweden",
219
- "ch" => "Switzerland",
220
- "sy" => "Syrian Arab Republic",
221
- "tw" => "Taiwan, Province of China",
222
- "tj" => "Tajikistan",
223
- "tz" => "Tanzania, United Republic of",
224
- "th" => "Thailand",
225
- "tl" => "Timor-Leste",
226
- "tg" => "Togo",
227
- "tk" => "Tokelau",
228
- "to" => "Tonga",
229
- "tt" => "Trinidad and Tobago",
230
- "tn" => "Tunisia",
231
- "tr" => "Turkey",
232
- "tm" => "Turkmenistan",
233
- "tc" => "Turks and Caicos Islands",
234
- "tv" => "Tuvalu",
235
- "ug" => "Uganda",
236
- "ua" => "Ukraine",
237
- "ae" => "United Arab Emirates",
238
- "gb" => "United Kingdom",
239
- "us" => "United States",
240
- "um" => "United States Minor Outlying Islands",
241
- "uy" => "Uruguay",
242
- "uz" => "Uzbekistan",
243
- "vu" => "Vanuatu",
244
- "ve" => "Venezuela, Bolivarian Republic of",
245
- "vn" => "Viet Nam",
246
- "vg" => "Virgin Islands, British",
247
- "vi" => "Virgin Islands, U.S.",
248
- "wf" => "Wallis and Futuna",
249
- "eh" => "Western Sahara",
250
- "ye" => "Yemen",
251
- "zm" => "Zambia",
252
- } unless const_defined?("COUNTRIES")
5
+ def self.use_iso_codes
6
+ Thread.current[:country_select_use_iso_codes] ||= false
7
+ end
8
+
9
+ def self.use_iso_codes=(val)
10
+ Thread.current[:country_select_use_iso_codes] = val
11
+ end
12
+
13
+ def self.locale
14
+ I18n.locale
15
+ end
16
+
17
+ #Localized Countries list
18
+ def self.countries(with_locale=nil)
19
+ with_locale ||= locale
20
+
21
+ ISO3166::Country.all.inject({}) do |hash,country_pair|
22
+ default_name = country_pair.first
23
+ code = country_pair.last
24
+ country = Country.new(code)
25
+ localized_name = country.translations[with_locale.to_s]
253
26
 
254
- ISO_COUNTRIES_FOR_SELECT = COUNTRIES.invert unless const_defined?("ISO_COUNTRIES_FOR_SELECT")
255
- COUNTRIES_FOR_SELECT = COUNTRIES.values unless const_defined?("COUNTRIES_FOR_SELECT")
27
+ # Codes should not be downcased, but they were previous to 1.3
28
+ # which means they should continue to be until 2.0 is released in
29
+ # order to prevent breakage with existing implementations
30
+ hash[code.downcase] = localized_name || default_name
31
+ hash
32
+ end
33
+ end
256
34
  end
@@ -1,3 +1,3 @@
1
1
  module CountrySelect
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -23,17 +23,17 @@ module ActionView
23
23
 
24
24
  let(:selected_us_option) do
25
25
  if defined?(Tags::Base)
26
- content_tag(:option, 'United States', :selected => :selected, :value => "United States")
26
+ content_tag(:option, 'United States of America', :selected => :selected, :value => "United States of America")
27
27
  else
28
- "<option value=\"United States\" selected=\"selected\">United States</option>"
28
+ "<option value=\"United States of America\" selected=\"selected\">United States of America</option>"
29
29
  end
30
30
  end
31
31
 
32
32
  let(:selected_iso_us_option) do
33
33
  if defined?(Tags::Base)
34
- content_tag(:option, 'United States', :selected => :selected, :value => 'us')
34
+ content_tag(:option, 'United States of America', :selected => :selected, :value => 'us')
35
35
  else
36
- "<option value=\"us\" selected=\"selected\">United States</option>"
36
+ "<option value=\"us\" selected=\"selected\">United States of America</option>"
37
37
  end
38
38
  end
39
39
 
@@ -54,33 +54,33 @@ module ActionView
54
54
  end
55
55
 
56
56
  it "creates option tags of the countries" do
57
- ::CountrySelect::COUNTRIES.each do |code,name|
57
+ ::CountrySelect::countries("en").each do |code,name|
58
58
  tag.should include(content_tag(:option, name, :value => name))
59
59
  end
60
60
  end
61
61
 
62
62
  it "selects the value of country_name" do
63
- walrus.country_name = 'United States'
63
+ walrus.country_name = 'United States of America'
64
64
  t = builder.country_select(:country_name)
65
65
  t.should include(selected_us_option)
66
66
  end
67
67
  end
68
68
 
69
69
  describe "#priority_countries" do
70
- let(:tag) { builder.country_select(:country_name, ['United States']) }
70
+ let(:tag) { builder.country_select(:country_name, ['United States of America']) }
71
71
 
72
72
  it "puts the countries at the top" do
73
- tag.should include("#{select_tag}<option value=\"United States")
73
+ tag.should include("#{select_tag}<option value=\"United States of America")
74
74
  end
75
75
 
76
76
  it "inserts a divider" do
77
- tag.should include(">United States</option><option value=\"\" disabled=\"disabled\">-------------</option>")
77
+ tag.should include(">United States of America</option><option value=\"\" disabled=\"disabled\">-------------</option>")
78
78
  end
79
79
 
80
80
  it "does not mark two countries as selected" do
81
- walrus.country_name = "United States"
81
+ walrus.country_name = "United States of America"
82
82
  str = <<-EOS.strip
83
- </option>\n<option value="United States" selected="selected">United States</option>
83
+ </option>\n<option value="United States of America" selected="selected">United States of America</option>
84
84
  EOS
85
85
  tag.should_not include(str)
86
86
  end
@@ -96,7 +96,7 @@ module ActionView
96
96
  end
97
97
 
98
98
  it "creates option tags of the countries" do
99
- ::CountrySelect::COUNTRIES.each do |code,name|
99
+ ::CountrySelect::countries("en").each do |code,name|
100
100
  tag.should include(content_tag(:option, name, :value => code))
101
101
  end
102
102
  end
@@ -108,6 +108,30 @@ module ActionView
108
108
  end
109
109
  end
110
110
 
111
+ describe "#country_select with global option" do
112
+ before do
113
+ ::CountrySelect.use_iso_codes = true
114
+ end
115
+
116
+ after do
117
+ ::CountrySelect.use_iso_codes = false
118
+ end
119
+
120
+ let(:tag) { builder.country_select(:country_name, nil) }
121
+
122
+ it "creates option tags of the countries" do
123
+ ::CountrySelect::countries("en").each do |code,name|
124
+ tag.should include(content_tag(:option, name, :value => code))
125
+ end
126
+ end
127
+
128
+ it "selects the value of country_name" do
129
+ walrus.country_name = 'us'
130
+ t = builder.country_select(:country_name)
131
+ t.should include(selected_iso_us_option)
132
+ end
133
+ end
134
+
111
135
  describe "#priority_countries" do
112
136
  let(:tag) { builder.country_select(:country_name, ['us'], :iso_codes => true) }
113
137
 
@@ -116,18 +140,27 @@ module ActionView
116
140
  end
117
141
 
118
142
  it "inserts a divider" do
119
- tag.should include(">United States</option><option value=\"\" disabled=\"disabled\">-------------</option>")
143
+ tag.should include(">United States of America</option><option value=\"\" disabled=\"disabled\">-------------</option>")
120
144
  end
121
145
 
122
146
  it "does not mark two countries as selected" do
123
147
  walrus.country_name = "us"
124
148
  str = <<-EOS.strip
125
- </option>\n<option value="us" selected="selected">United States</option>
149
+ </option>\n<option value="us" selected="selected">United States of America</option>
126
150
  EOS
127
151
  tag.should_not include(str)
128
152
  end
129
153
  end
130
154
  end
155
+ context "different language selected" do
156
+ describe "'es' selected as the instance language" do
157
+ let(:tag) { builder.country_select(:country_name, ['us'], {:iso_codes => true, :locale => 'es'}) }
158
+
159
+ it "displays spanish names" do
160
+ tag.should include(">Estados Unidos</option><option value=\"\" disabled=\"disabled\">-------------</option>")
161
+ end
162
+ end
163
+ end
131
164
  end
132
165
  end
133
166
  end