country-code-select 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +28 -0
  3. data/lib/country_code_select.rb +322 -0
  4. metadata +49 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 John Tajima
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Country-Code-Select
2
+
3
+ A country-select compatible country selector helper which uses ISO country codes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'country-code-select'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install country-code-select
18
+
19
+ ## Usage
20
+
21
+ Simple use supplying model and attribute as parameters:
22
+
23
+ country_select("user", "country_name")
24
+
25
+ Supplying priority countries to be placed at the top of the list. Countries entered should be the country code.:
26
+
27
+ country_select("user", "country_name", [ "GB", "FR", "US" ])
28
+
@@ -0,0 +1,322 @@
1
+ require "country_code_select/version"
2
+
3
+ module ActionView
4
+ module Helpers
5
+ module FormOptionsHelper
6
+
7
+ # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
8
+ def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
9
+ InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options)
10
+ end
11
+ # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to
12
+ # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so
13
+ # that they will be listed above the rest of the (long) list.
14
+ #
15
+ # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
16
+ def country_options_for_select(selected = nil, priority_countries = nil)
17
+ country_options = ""
18
+
19
+ if priority_countries
20
+ if (unlisted = priority_countries - COUNTRIES.map(&:last)).any?
21
+ raise RuntimeError.new("Supplied priority countries are not in the main list: #{unlisted}")
22
+ end
23
+
24
+ priority_entries = priority_countries.map do |country_name|
25
+ COUNTRIES.detect {|c| c.last == country_name}
26
+ end
27
+
28
+ country_options += options_for_select(priority_entries, selected)
29
+ country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
30
+
31
+ # prevents selected from being included twice in the HTML which causes
32
+ # some browsers to select the second selected option (not priority)
33
+ # which makes it harder to select an alternative priority country
34
+ selected = nil if priority_countries.include?(selected)
35
+ end
36
+
37
+ country_options = country_options.html_safe if country_options.respond_to?(:html_safe)
38
+
39
+ return country_options + options_for_select(COUNTRIES, selected)
40
+ end
41
+
42
+ COUNTRIES = [
43
+ ["Afghanistan", "AF"],
44
+ ["Aland Islands", "AX"],
45
+ ["Albania", "AL"],
46
+ ["Algeria", "DZ"],
47
+ ["American Samoa", "AS"],
48
+ ["Andorra", "AD"],
49
+ ["Angola", "AO"],
50
+ ["Anguilla", "AI"],
51
+ ["Antarctica", "AQ"],
52
+ ["Antigua and Barbuda", "AG"],
53
+ ["Argentina", "AR"],
54
+ ["Armenia", "AM"],
55
+ ["Aruba", "AW"],
56
+ ["Ascension Island", "AC"],
57
+ ["Australia", "AU"],
58
+ ["Austria", "AT"],
59
+ ["Azerbaijan", "AZ"],
60
+ ["Bahamas", "BS"],
61
+ ["Bahrain", "BH"],
62
+ ["Bangladesh", "BD"],
63
+ ["Barbados", "BB"],
64
+ ["Belarus", "BY"],
65
+ ["Belgium", "BE"],
66
+ ["Belize", "BZ"],
67
+ ["Benin", "BJ"],
68
+ ["Bermuda", "BM"],
69
+ ["Bhutan", "BT"],
70
+ ["Bolivia, Plurinational State of", "BO"],
71
+ ["Bonaire, Sint Eustatius and Saba", "BQ"],
72
+ ["Bosnia and Herzegovina", "BA"],
73
+ ["Botswana", "BW"],
74
+ ["Bouvet Island", "BV"],
75
+ ["Brazil", "BR"],
76
+ ["British Indian Ocean Territory", "IO"],
77
+ ["Brunei Darussalam", "BN"],
78
+ ["Bulgaria", "BG"],
79
+ ["Burkina Faso", "BF"],
80
+ ["Burundi", "BI"],
81
+ ["Cambodia", "KH"],
82
+ ["Cameroon", "CM"],
83
+ ["Canada", "CA"],
84
+ ["Cape Verde", "CV"],
85
+ ["Cayman Islands", "KY"],
86
+ ["Central African Republic", "CF"],
87
+ ["Chad", "TD"],
88
+ ["Chile", "CL"],
89
+ ["China", "CN"],
90
+ ["Christmas Island", "CX"],
91
+ ["Cocos (Keeling) Islands", "CC"],
92
+ ["Colombia", "CO"],
93
+ ["Comoros", "KM"],
94
+ ["Congo", "CG"],
95
+ ["Congo, the Democratic Republic of the", "CD"],
96
+ ["Cook Islands", "CK"],
97
+ ["Costa Rica", "CR"],
98
+ ["Cote d'Ivoire", "CI"],
99
+ ["Croatia", "HR"],
100
+ ["Cuba", "CU"],
101
+ ["Curacao", "CW"],
102
+ ["Cyprus", "CY"],
103
+ ["Czech Republic", "CZ"],
104
+ ["Denmark", "DK"],
105
+ ["Djibouti", "DJ"],
106
+ ["Dominica", "DM"],
107
+ ["Dominican Republic", "DO"],
108
+ ["Ecuador", "EC"],
109
+ ["Egypt", "EG"],
110
+ ["El Salvador", "SV"],
111
+ ["Equatorial Guinea", "GQ"],
112
+ ["Eritrea", "ER"],
113
+ ["Estonia", "EE"],
114
+ ["Ethiopia", "ET"],
115
+ ["Falkland Islands (Malvinas)", "FK"],
116
+ ["Faroe Islands", "FO"],
117
+ ["Fiji", "FJ"],
118
+ ["Finland", "FI"],
119
+ ["France", "FR"],
120
+ ["French Guiana", "GF"],
121
+ ["French Polynesia", "PF"],
122
+ ["French Southern Territories", "TF"],
123
+ ["Gabon", "GA"],
124
+ ["Gambia", "GM"],
125
+ ["Georgia", "GE"],
126
+ ["Germany", "DE"],
127
+ ["Ghana", "GH"],
128
+ ["Gibraltar", "GI"],
129
+ ["Greece", "GR"],
130
+ ["Greenland", "GL"],
131
+ ["Grenada", "GD"],
132
+ ["Guadeloupe", "GP"],
133
+ ["Guam", "GU"],
134
+ ["Guatemala", "GT"],
135
+ ["Guernsey", "GG"],
136
+ ["Guinea", "GN"],
137
+ ["Guinea-Bissau", "GW"],
138
+ ["Guyana", "GY"],
139
+ ["Haiti", "HT"],
140
+ ["Heard Island and McDonald Islands", "HM"],
141
+ ["Holy See (Vatican City State)", "VA"],
142
+ ["Honduras", "HN"],
143
+ ["Hong Kong", "HK"],
144
+ ["Hungary", "HU"],
145
+ ["Iceland", "IS"],
146
+ ["India", "IN"],
147
+ ["Indonesia", "ID"],
148
+ ["Iran, Islamic Republic of", "IR"],
149
+ ["Iraq", "IQ"],
150
+ ["Ireland", "IE"],
151
+ ["Isle of Man", "IM"],
152
+ ["Israel", "IL"],
153
+ ["Italy", "IT"],
154
+ ["Jamaica", "JM"],
155
+ ["Japan", "JP"],
156
+ ["Jersey", "JE"],
157
+ ["Jordan", "JO"],
158
+ ["Kazakhstan", "KZ"],
159
+ ["Kenya", "KE"],
160
+ ["Kiribati", "KI"],
161
+ ["Korea, Democratic People's Republic of", "KP"],
162
+ ["Korea, Republic of", "KR"],
163
+ ["Kosovo", "KV"],
164
+ ["Kuwait", "KW"],
165
+ ["Kyrgyzstan", "KG"],
166
+ ["Lao People's Democratic Republic", "LA"],
167
+ ["Latvia", "LV"],
168
+ ["Lebanon", "LB"],
169
+ ["Lesotho", "LS"],
170
+ ["Liberia", "LR"],
171
+ ["Libya", "LY"],
172
+ ["Liechtenstein", "LI"],
173
+ ["Lithuania", "LT"],
174
+ ["Luxembourg", "LU"],
175
+ ["Macao", "MO"],
176
+ ["Macedonia, The Former Yugoslav Republic Of", "MK"],
177
+ ["Madagascar", "MG"],
178
+ ["Malawi", "MW"],
179
+ ["Malaysia", "MY"],
180
+ ["Maldives", "MV"],
181
+ ["Mali", "ML"],
182
+ ["Malta", "MT"],
183
+ ["Marshall Islands", "MH"],
184
+ ["Martinique", "MQ"],
185
+ ["Mauritania", "MR"],
186
+ ["Mauritius", "MU"],
187
+ ["Mayotte", "YT"],
188
+ ["Mexico", "MX"],
189
+ ["Micronesia, Federated States of", "FM"],
190
+ ["Moldova, Republic of", "MD"],
191
+ ["Monaco", "MC"],
192
+ ["Mongolia", "MN"],
193
+ ["Montenegro", "ME"],
194
+ ["Montserrat", "MS"],
195
+ ["Morocco", "MA"],
196
+ ["Mozambique", "MZ"],
197
+ ["Myanmar", "MM"],
198
+ ["Namibia", "NA"],
199
+ ["Nauru", "NR"],
200
+ ["Nepal", "NP"],
201
+ ["Netherlands", "NL"],
202
+ ["Netherlands Antilles", "AN"],
203
+ ["New Caledonia", "NC"],
204
+ ["New Zealand", "NZ"],
205
+ ["Nicaragua", "NI"],
206
+ ["Niger", "NE"],
207
+ ["Nigeria", "NG"],
208
+ ["Niue", "NU"],
209
+ ["Norfolk Island", "NF"],
210
+ ["Northern Mariana Islands", "MP"],
211
+ ["Norway", "NO"],
212
+ ["Oman", "OM"],
213
+ ["Pakistan", "PK"],
214
+ ["Palau", "PW"],
215
+ ["Palestinian Territory, Occupied", "PS"],
216
+ ["Panama", "PA"],
217
+ ["Papua New Guinea", "PG"],
218
+ ["Paraguay", "PY"],
219
+ ["Peru", "PE"],
220
+ ["Philippines", "PH"],
221
+ ["Pitcairn", "PN"],
222
+ ["Poland", "PL"],
223
+ ["Portugal", "PT"],
224
+ ["Puerto Rico", "PR"],
225
+ ["Qatar", "QA"],
226
+ ["Reunion", "RE"],
227
+ ["Romania", "RO"],
228
+ ["Russian Federation", "RU"],
229
+ ["Rwanda", "RW"],
230
+ ["Saint Barthelemy", "BL"],
231
+ ["Saint Helena, Ascension and Tristan da Cunha", "SH"],
232
+ ["Saint Kitts and Nevis", "KN"],
233
+ ["Saint Lucia", "LC"],
234
+ ["Saint Martin (French part)", "MF"],
235
+ ["Saint Pierre and Miquelon", "PM"],
236
+ ["Saint Vincent and the Grenadines", "VC"],
237
+ ["Samoa", "WS"],
238
+ ["San Marino", "SM"],
239
+ ["Sao Tome and Principe", "ST"],
240
+ ["Saudi Arabia", "SA"],
241
+ ["Senegal", "SN"],
242
+ ["Serbia", "RS"],
243
+ ["Seychelles", "SC"],
244
+ ["Sierra Leone", "SL"],
245
+ ["Singapore", "SG"],
246
+ ["Sint Maarten (Dutch part)", "SX"],
247
+ ["Slovakia", "SK"],
248
+ ["Slovenia", "SI"],
249
+ ["Solomon Islands", "SB"],
250
+ ["Somalia", "SO"],
251
+ ["South Africa", "ZA"],
252
+ ["South Georgia and the South Sandwich Islands", "GS"],
253
+ ["South Sudan, Republic of", "SS"],
254
+ ["Spain", "ES"],
255
+ ["Sri Lanka", "LK"],
256
+ ["Sudan", "SD"],
257
+ ["Suriname", "SR"],
258
+ ["Svalbard and Jan Mayen", "SJ"],
259
+ ["Swaziland", "SZ"],
260
+ ["Sweden", "SE"],
261
+ ["Switzerland", "CH"],
262
+ ["Syrian Arab Republic", "SY"],
263
+ ["Taiwan", "TW"],
264
+ ["Tajikistan", "TJ"],
265
+ ["Tanzania, United Republic of", "TZ"],
266
+ ["Thailand", "TH"],
267
+ ["Timor-Leste", "TL"],
268
+ ["Togo", "TG"],
269
+ ["Tokelau", "TK"],
270
+ ["Tonga", "TO"],
271
+ ["Trinidad and Tobago", "TT"],
272
+ ["Tristan da Cunha", "TA"],
273
+ ["Tunisia", "TN"],
274
+ ["Turkey", "TR"],
275
+ ["Turkmenistan", "TM"],
276
+ ["Turks and Caicos Islands", "TC"],
277
+ ["Tuvalu", "TV"],
278
+ ["Uganda", "UG"],
279
+ ["Ukraine", "UA"],
280
+ ["United Arab Emirates", "AE"],
281
+ ["United Kingdom", "GB"],
282
+ ["United States", "US"],
283
+ ["United States Minor Outlying Islands", "UM"],
284
+ ["Uruguay", "UY"],
285
+ ["Uzbekistan", "UZ"],
286
+ ["Vanuatu", "VU"],
287
+ ["Venezuela, Bolivarian Republic of", "VE"],
288
+ ["Viet Nam", "VN"],
289
+ ["Virgin Islands, British", "VG"],
290
+ ["Virgin Islands, U.S.", "VI"],
291
+ ["Wallis and Futuna", "WF"],
292
+ ["Western Sahara", "EH"],
293
+ ["Yemen", "YE"],
294
+ ["Zambia", "ZM"],
295
+ ["Zimbabwe", "ZW"]
296
+ ]
297
+
298
+ end
299
+
300
+
301
+ class InstanceTag
302
+ def to_country_select_tag(priority_countries, options, html_options)
303
+ html_options = html_options.stringify_keys
304
+ add_default_name_and_id(html_options)
305
+ value = value(object)
306
+ content_tag("select",
307
+ add_options(
308
+ country_options_for_select(value, priority_countries),
309
+ options, value
310
+ ), html_options
311
+ )
312
+ end
313
+ end
314
+
315
+ class FormBuilder
316
+ def country_select(method, priority_countries = nil, options = {}, html_options = {})
317
+ @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
318
+ end
319
+ end
320
+
321
+ end
322
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: country-code-select
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Tajima
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: country-select compatible gem which uses country codes
15
+ email:
16
+ - john@shopify.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/country_code_select.rb
22
+ - README.md
23
+ - LICENSE
24
+ homepage: ''
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.11
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Provides a form helper to insert a country select box using the ISO 3166
48
+ country list
49
+ test_files: []