iso_countries 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +30 -0
- data/Rakefile +121 -0
- data/init.rb +3 -0
- data/lib/country_list.rb +254 -0
- data/lib/iso/countries/country_field.rb +46 -0
- data/lib/iso/countries/form_helpers.rb +57 -0
- data/lib/iso_countries.rb +67 -0
- data/tasks/iso_countries_tasks.rake +4 -0
- data/test/iso_countries_test.rb +10 -0
- metadata +76 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
= iso_countries - Store countries using ISO 3166 codes
|
2
|
+
|
3
|
+
This rails plugin enables you to store country info only with the country's ISO-3166 code
|
4
|
+
|
5
|
+
== Example
|
6
|
+
|
7
|
+
class Company < ActiveRecord::Base
|
8
|
+
iso_country :country
|
9
|
+
end
|
10
|
+
|
11
|
+
c = Company.new :country => "es"
|
12
|
+
c.country # => "es"
|
13
|
+
c.country_name # => "Spain"
|
14
|
+
c.country_name = "France"
|
15
|
+
c.country # => "fr"
|
16
|
+
ISO::Countries.set_language "es"
|
17
|
+
c.country_name # => "Francia"
|
18
|
+
|
19
|
+
<% form_for @company do |f| %>
|
20
|
+
<%= f.iso_country_select :country, [:es, :en] %>
|
21
|
+
<% end %>
|
22
|
+
|
23
|
+
== Acknowledgements
|
24
|
+
|
25
|
+
Translations are provided by the iso-codes package
|
26
|
+
http://pkg-isocodes.alioth.debian.org/
|
27
|
+
|
28
|
+
== License
|
29
|
+
|
30
|
+
Copyright (c) 2008 Jorge Bernal <jbernal@warp.es>, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the iso_countries plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the iso_countries plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'IsoCountries'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
desc 'Download an updated list from the iso website'
|
25
|
+
task :update do
|
26
|
+
url = "http://www.iso.org/iso/iso3166_en_code_lists.txt"
|
27
|
+
require 'open-uri'
|
28
|
+
iso = open(url)
|
29
|
+
require "iconv"
|
30
|
+
conv = Iconv.new('utf8', 'latin1')
|
31
|
+
require "unicode"
|
32
|
+
|
33
|
+
File.open('lib/country_list.rb', 'w') do |f|
|
34
|
+
f.puts "module ISO"
|
35
|
+
f.puts " module Countries"
|
36
|
+
f.puts " COUNTRIES = {"
|
37
|
+
|
38
|
+
# Skip the first two lines, as they don't contain country information
|
39
|
+
iso.readline
|
40
|
+
iso.readline
|
41
|
+
|
42
|
+
countries = []
|
43
|
+
iso.each_line do |line|
|
44
|
+
country, code = line.split(';')
|
45
|
+
code.chomp!
|
46
|
+
country = Unicode.capitalize(conv.iconv(country))
|
47
|
+
|
48
|
+
puts "#{code} => #{country}"
|
49
|
+
countries << " :#{code.downcase} => N_(\"#{country}\")"
|
50
|
+
end
|
51
|
+
f.puts countries.join(",\n")
|
52
|
+
|
53
|
+
f.puts " }"
|
54
|
+
f.puts " end"
|
55
|
+
f.puts "end"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
desc "Update pot/po files to match new version."
|
61
|
+
task :updatepo do
|
62
|
+
require 'gettext'
|
63
|
+
require 'gettext/utils'
|
64
|
+
|
65
|
+
# GetText::ActiveRecordParser.init(:use_classname => false, :activerecord_classes => ['FakeARClass'])
|
66
|
+
GetText.update_pofiles('iso_countries',
|
67
|
+
Dir.glob("lib/**/*.rb"),
|
68
|
+
"iso_countries plugin")
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Create mo-files"
|
72
|
+
task :makemo do
|
73
|
+
require 'gettext'
|
74
|
+
require 'gettext/utils'
|
75
|
+
GetText.create_mofiles(true, "po", "locale")
|
76
|
+
end
|
77
|
+
|
78
|
+
desc "Downloads translations from iso-codes repository"
|
79
|
+
task :download do
|
80
|
+
repo = "svn://svn.debian.org/pkg-isocodes/trunk/iso-codes/iso_3166"
|
81
|
+
|
82
|
+
FileUtils.rm_rf("tmp")
|
83
|
+
system "svn co #{repo} tmp"
|
84
|
+
Dir.glob("tmp/*.po").each do |pofile|
|
85
|
+
locale = File.basename(pofile, ".po")
|
86
|
+
FileUtils.mkdir_p("po/#{locale}")
|
87
|
+
puts "#{locale} -> po/#{locale}/iso_countries.po"
|
88
|
+
FileUtils.mv(pofile, "po/#{locale}/iso_countries.po")
|
89
|
+
end
|
90
|
+
FileUtils.rm_rf("tmp")
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
spec = Gem::Specification.new do |s|
|
95
|
+
s.name = "iso_countries"
|
96
|
+
s.version = "0.1"
|
97
|
+
s.author = "Jorge Bernal"
|
98
|
+
s.email = "jbernal@warp.es"
|
99
|
+
s.homepage = "http://github.com/koke/iso_countries"
|
100
|
+
s.platform = Gem::Platform::RUBY
|
101
|
+
s.summary = "Country selector with ISO codes"
|
102
|
+
s.files = FileList["README*",
|
103
|
+
"MIT-LICENSE",
|
104
|
+
"Rakefile",
|
105
|
+
"init.rb",
|
106
|
+
"{lib,tasks,test}/**/*"].to_a
|
107
|
+
s.require_path = "lib"
|
108
|
+
s.test_files = FileList["test/**/test_*.rb"].to_a
|
109
|
+
s.rubyforge_project = "iso_countries"
|
110
|
+
s.has_rdoc = false
|
111
|
+
s.extra_rdoc_files = FileList["README*"].to_a
|
112
|
+
s.rdoc_options << '--line-numbers' << '--inline-source'
|
113
|
+
s.requirements << "gettext"
|
114
|
+
end
|
115
|
+
|
116
|
+
desc "Generate a gemspec file for GitHub"
|
117
|
+
task :gemspec do
|
118
|
+
File.open("#{spec.name}.gemspec", 'w') do |f|
|
119
|
+
f.write spec.to_ruby
|
120
|
+
end
|
121
|
+
end
|
data/init.rb
ADDED
data/lib/country_list.rb
ADDED
@@ -0,0 +1,254 @@
|
|
1
|
+
module ISO
|
2
|
+
module Countries
|
3
|
+
include GetText
|
4
|
+
|
5
|
+
COUNTRIES = {
|
6
|
+
:af => N_("Afghanistan"),
|
7
|
+
:ax => N_("Åland islands"),
|
8
|
+
:al => N_("Albania"),
|
9
|
+
:dz => N_("Algeria"),
|
10
|
+
:as => N_("American samoa"),
|
11
|
+
:ad => N_("Andorra"),
|
12
|
+
:ao => N_("Angola"),
|
13
|
+
:ai => N_("Anguilla"),
|
14
|
+
:aq => N_("Antarctica"),
|
15
|
+
:ag => N_("Antigua and barbuda"),
|
16
|
+
:ar => N_("Argentina"),
|
17
|
+
:am => N_("Armenia"),
|
18
|
+
:aw => N_("Aruba"),
|
19
|
+
:au => N_("Australia"),
|
20
|
+
:at => N_("Austria"),
|
21
|
+
:az => N_("Azerbaijan"),
|
22
|
+
:bs => N_("Bahamas"),
|
23
|
+
:bh => N_("Bahrain"),
|
24
|
+
:bd => N_("Bangladesh"),
|
25
|
+
:bb => N_("Barbados"),
|
26
|
+
:by => N_("Belarus"),
|
27
|
+
:be => N_("Belgium"),
|
28
|
+
:bz => N_("Belize"),
|
29
|
+
:bj => N_("Benin"),
|
30
|
+
:bm => N_("Bermuda"),
|
31
|
+
:bt => N_("Bhutan"),
|
32
|
+
:bo => N_("Bolivia"),
|
33
|
+
:ba => N_("Bosnia and herzegovina"),
|
34
|
+
:bw => N_("Botswana"),
|
35
|
+
:bv => N_("Bouvet island"),
|
36
|
+
:br => N_("Brazil"),
|
37
|
+
:io => N_("British indian ocean territory"),
|
38
|
+
:bn => N_("Brunei darussalam"),
|
39
|
+
:bg => N_("Bulgaria"),
|
40
|
+
:bf => N_("Burkina faso"),
|
41
|
+
:bi => N_("Burundi"),
|
42
|
+
:kh => N_("Cambodia"),
|
43
|
+
:cm => N_("Cameroon"),
|
44
|
+
:ca => N_("Canada"),
|
45
|
+
:cv => N_("Cape verde"),
|
46
|
+
:ky => N_("Cayman islands"),
|
47
|
+
:cf => N_("Central african republic"),
|
48
|
+
:td => N_("Chad"),
|
49
|
+
:cl => N_("Chile"),
|
50
|
+
:cn => N_("China"),
|
51
|
+
:cx => N_("Christmas island"),
|
52
|
+
:cc => N_("Cocos (keeling) islands"),
|
53
|
+
:co => N_("Colombia"),
|
54
|
+
:km => N_("Comoros"),
|
55
|
+
:cg => N_("Congo"),
|
56
|
+
:cd => N_("Congo, the democratic republic of the"),
|
57
|
+
:ck => N_("Cook islands"),
|
58
|
+
:cr => N_("Costa rica"),
|
59
|
+
:ci => N_("Côte d'ivoire"),
|
60
|
+
:hr => N_("Croatia"),
|
61
|
+
:cu => N_("Cuba"),
|
62
|
+
:cy => N_("Cyprus"),
|
63
|
+
:cz => N_("Czech republic"),
|
64
|
+
:dk => N_("Denmark"),
|
65
|
+
:dj => N_("Djibouti"),
|
66
|
+
:dm => N_("Dominica"),
|
67
|
+
:do => N_("Dominican republic"),
|
68
|
+
:ec => N_("Ecuador"),
|
69
|
+
:eg => N_("Egypt"),
|
70
|
+
:sv => N_("El salvador"),
|
71
|
+
:gq => N_("Equatorial guinea"),
|
72
|
+
:er => N_("Eritrea"),
|
73
|
+
:ee => N_("Estonia"),
|
74
|
+
:et => N_("Ethiopia"),
|
75
|
+
:fk => N_("Falkland islands (malvinas)"),
|
76
|
+
:fo => N_("Faroe islands"),
|
77
|
+
:fj => N_("Fiji"),
|
78
|
+
:fi => N_("Finland"),
|
79
|
+
:fr => N_("France"),
|
80
|
+
:gf => N_("French guiana"),
|
81
|
+
:pf => N_("French polynesia"),
|
82
|
+
:tf => N_("French southern territories"),
|
83
|
+
:ga => N_("Gabon"),
|
84
|
+
:gm => N_("Gambia"),
|
85
|
+
:ge => N_("Georgia"),
|
86
|
+
:de => N_("Germany"),
|
87
|
+
:gh => N_("Ghana"),
|
88
|
+
:gi => N_("Gibraltar"),
|
89
|
+
:gr => N_("Greece"),
|
90
|
+
:gl => N_("Greenland"),
|
91
|
+
:gd => N_("Grenada"),
|
92
|
+
:gp => N_("Guadeloupe"),
|
93
|
+
:gu => N_("Guam"),
|
94
|
+
:gt => N_("Guatemala"),
|
95
|
+
:gg => N_("Guernsey"),
|
96
|
+
:gn => N_("Guinea"),
|
97
|
+
:gw => N_("Guinea-bissau"),
|
98
|
+
:gy => N_("Guyana"),
|
99
|
+
:ht => N_("Haiti"),
|
100
|
+
:hm => N_("Heard island and mcdonald islands"),
|
101
|
+
:va => N_("Holy see (vatican city state)"),
|
102
|
+
:hn => N_("Honduras"),
|
103
|
+
:hk => N_("Hong kong"),
|
104
|
+
:hu => N_("Hungary"),
|
105
|
+
:is => N_("Iceland"),
|
106
|
+
:in => N_("India"),
|
107
|
+
:id => N_("Indonesia"),
|
108
|
+
:ir => N_("Iran, islamic republic of"),
|
109
|
+
:iq => N_("Iraq"),
|
110
|
+
:ie => N_("Ireland"),
|
111
|
+
:im => N_("Isle of man"),
|
112
|
+
:il => N_("Israel"),
|
113
|
+
:it => N_("Italy"),
|
114
|
+
:jm => N_("Jamaica"),
|
115
|
+
:jp => N_("Japan"),
|
116
|
+
:je => N_("Jersey"),
|
117
|
+
:jo => N_("Jordan"),
|
118
|
+
:kz => N_("Kazakhstan"),
|
119
|
+
:ke => N_("Kenya"),
|
120
|
+
:ki => N_("Kiribati"),
|
121
|
+
:kp => N_("Korea, democratic people's republic of"),
|
122
|
+
:kr => N_("Korea, republic of"),
|
123
|
+
:kw => N_("Kuwait"),
|
124
|
+
:kg => N_("Kyrgyzstan"),
|
125
|
+
:la => N_("Lao people's democratic republic"),
|
126
|
+
:lv => N_("Latvia"),
|
127
|
+
:lb => N_("Lebanon"),
|
128
|
+
:ls => N_("Lesotho"),
|
129
|
+
:lr => N_("Liberia"),
|
130
|
+
:ly => N_("Libyan arab jamahiriya"),
|
131
|
+
:li => N_("Liechtenstein"),
|
132
|
+
:lt => N_("Lithuania"),
|
133
|
+
:lu => N_("Luxembourg"),
|
134
|
+
:mo => N_("Macao"),
|
135
|
+
:mk => N_("Macedonia, the former yugoslav republic of"),
|
136
|
+
:mg => N_("Madagascar"),
|
137
|
+
:mw => N_("Malawi"),
|
138
|
+
:my => N_("Malaysia"),
|
139
|
+
:mv => N_("Maldives"),
|
140
|
+
:ml => N_("Mali"),
|
141
|
+
:mt => N_("Malta"),
|
142
|
+
:mh => N_("Marshall islands"),
|
143
|
+
:mq => N_("Martinique"),
|
144
|
+
:mr => N_("Mauritania"),
|
145
|
+
:mu => N_("Mauritius"),
|
146
|
+
:yt => N_("Mayotte"),
|
147
|
+
:mx => N_("Mexico"),
|
148
|
+
:fm => N_("Micronesia, federated states of"),
|
149
|
+
:md => N_("Moldova, republic of"),
|
150
|
+
:mc => N_("Monaco"),
|
151
|
+
:mn => N_("Mongolia"),
|
152
|
+
:me => N_("Montenegro"),
|
153
|
+
:ms => N_("Montserrat"),
|
154
|
+
:ma => N_("Morocco"),
|
155
|
+
:mz => N_("Mozambique"),
|
156
|
+
:mm => N_("Myanmar"),
|
157
|
+
:na => N_("Namibia"),
|
158
|
+
:nr => N_("Nauru"),
|
159
|
+
:np => N_("Nepal"),
|
160
|
+
:nl => N_("Netherlands"),
|
161
|
+
:an => N_("Netherlands antilles"),
|
162
|
+
:nc => N_("New caledonia"),
|
163
|
+
:nz => N_("New zealand"),
|
164
|
+
:ni => N_("Nicaragua"),
|
165
|
+
:ne => N_("Niger"),
|
166
|
+
:ng => N_("Nigeria"),
|
167
|
+
:nu => N_("Niue"),
|
168
|
+
:nf => N_("Norfolk island"),
|
169
|
+
:mp => N_("Northern mariana islands"),
|
170
|
+
:no => N_("Norway"),
|
171
|
+
:om => N_("Oman"),
|
172
|
+
:pk => N_("Pakistan"),
|
173
|
+
:pw => N_("Palau"),
|
174
|
+
:ps => N_("Palestinian territory, occupied"),
|
175
|
+
:pa => N_("Panama"),
|
176
|
+
:pg => N_("Papua new guinea"),
|
177
|
+
:py => N_("Paraguay"),
|
178
|
+
:pe => N_("Peru"),
|
179
|
+
:ph => N_("Philippines"),
|
180
|
+
:pn => N_("Pitcairn"),
|
181
|
+
:pl => N_("Poland"),
|
182
|
+
:pt => N_("Portugal"),
|
183
|
+
:pr => N_("Puerto rico"),
|
184
|
+
:qa => N_("Qatar"),
|
185
|
+
:re => N_("Reunion"),
|
186
|
+
:ro => N_("Romania"),
|
187
|
+
:ru => N_("Russian federation"),
|
188
|
+
:rw => N_("Rwanda"),
|
189
|
+
:bl => N_("Saint barthélemy"),
|
190
|
+
:sh => N_("Saint helena"),
|
191
|
+
:kn => N_("Saint kitts and nevis"),
|
192
|
+
:lc => N_("Saint lucia"),
|
193
|
+
:mf => N_("Saint martin"),
|
194
|
+
:pm => N_("Saint pierre and miquelon"),
|
195
|
+
:vc => N_("Saint vincent and the grenadines"),
|
196
|
+
:ws => N_("Samoa"),
|
197
|
+
:sm => N_("San marino"),
|
198
|
+
:st => N_("Sao tome and principe"),
|
199
|
+
:sa => N_("Saudi arabia"),
|
200
|
+
:sn => N_("Senegal"),
|
201
|
+
:rs => N_("Serbia"),
|
202
|
+
:sc => N_("Seychelles"),
|
203
|
+
:sl => N_("Sierra leone"),
|
204
|
+
:sg => N_("Singapore"),
|
205
|
+
:sk => N_("Slovakia"),
|
206
|
+
:si => N_("Slovenia"),
|
207
|
+
:sb => N_("Solomon islands"),
|
208
|
+
:so => N_("Somalia"),
|
209
|
+
:za => N_("South africa"),
|
210
|
+
:gs => N_("South georgia and the south sandwich islands"),
|
211
|
+
:es => N_("Spain"),
|
212
|
+
:lk => N_("Sri lanka"),
|
213
|
+
:sd => N_("Sudan"),
|
214
|
+
:sr => N_("Suriname"),
|
215
|
+
:sj => N_("Svalbard and jan mayen"),
|
216
|
+
:sz => N_("Swaziland"),
|
217
|
+
:se => N_("Sweden"),
|
218
|
+
:ch => N_("Switzerland"),
|
219
|
+
:sy => N_("Syrian arab republic"),
|
220
|
+
:tw => N_("Taiwan, province of china"),
|
221
|
+
:tj => N_("Tajikistan"),
|
222
|
+
:tz => N_("Tanzania, united republic of"),
|
223
|
+
:th => N_("Thailand"),
|
224
|
+
:tl => N_("Timor-leste"),
|
225
|
+
:tg => N_("Togo"),
|
226
|
+
:tk => N_("Tokelau"),
|
227
|
+
:to => N_("Tonga"),
|
228
|
+
:tt => N_("Trinidad and tobago"),
|
229
|
+
:tn => N_("Tunisia"),
|
230
|
+
:tr => N_("Turkey"),
|
231
|
+
:tm => N_("Turkmenistan"),
|
232
|
+
:tc => N_("Turks and caicos islands"),
|
233
|
+
:tv => N_("Tuvalu"),
|
234
|
+
:ug => N_("Uganda"),
|
235
|
+
:ua => N_("Ukraine"),
|
236
|
+
:ae => N_("United arab emirates"),
|
237
|
+
:gb => N_("United kingdom"),
|
238
|
+
:us => N_("United states"),
|
239
|
+
:um => N_("United states minor outlying islands"),
|
240
|
+
:uy => N_("Uruguay"),
|
241
|
+
:uz => N_("Uzbekistan"),
|
242
|
+
:vu => N_("Vanuatu"),
|
243
|
+
:ve => N_("Venezuela"),
|
244
|
+
:vn => N_("Viet nam"),
|
245
|
+
:vg => N_("Virgin islands, british"),
|
246
|
+
:vi => N_("Virgin islands, u.s."),
|
247
|
+
:wf => N_("Wallis and futuna"),
|
248
|
+
:eh => N_("Western sahara"),
|
249
|
+
:ye => N_("Yemen"),
|
250
|
+
:zm => N_("Zambia"),
|
251
|
+
:zw => N_("Zimbabwe")
|
252
|
+
}
|
253
|
+
end
|
254
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ISO #:nodoc:
|
2
|
+
module Countries #:nodoc:
|
3
|
+
module CountryField #:nodoc:
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Declares a field from a model as a iso code for a country
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# class Company
|
14
|
+
# iso_country :country
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# c = Company.new(:country => "es")
|
18
|
+
# c.country_name # => "Spain"
|
19
|
+
# c.country_name = "France"
|
20
|
+
# c.country # => "fr"
|
21
|
+
def iso_country(*args)
|
22
|
+
args.each do |f|
|
23
|
+
class_eval <<-EOC
|
24
|
+
|
25
|
+
validates_inclusion_of :#{f}, :in => ISO::Countries.country_codes, :allow_nil => true
|
26
|
+
|
27
|
+
def #{f}_name
|
28
|
+
ISO::Countries.get_country(#{f})
|
29
|
+
end
|
30
|
+
|
31
|
+
def #{f}_name=(name)
|
32
|
+
code = ISO::Countries.get_code(name)
|
33
|
+
if code
|
34
|
+
self.#{f} = code
|
35
|
+
else
|
36
|
+
raise ArgumentError, "Invalid country name"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
EOC
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module ActionView #:nodoc:
|
2
|
+
module Helpers #:nodoc:
|
3
|
+
module FormOptionsHelper
|
4
|
+
# Return select and option tags for the given object and method, using iso_options_for_select to generate the list of option tags.
|
5
|
+
|
6
|
+
def iso_country_select(object, method, priority_countries = nil, options = {}, html_options = {})
|
7
|
+
InstanceTag.new(object, method, self).to_iso_select_tag(priority_countries, options, html_options)
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
# Returns a string of option tags for pretty much any country in the world. Supply a country name as selected to have it marked as the selected option tag. You can also supply an array of countries as priority_countries, so that they will be listed above the rest of the (long) list.
|
12
|
+
#
|
13
|
+
# NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
|
14
|
+
#
|
15
|
+
def iso_options_for_select(selected = nil, priority_countries = nil)
|
16
|
+
countries_for_select = {}
|
17
|
+
ISO::Countries::COUNTRIES.each_pair {|code,name| countries_for_select[ISO::Countries.get_country(code)] = code.to_s }
|
18
|
+
|
19
|
+
country_options = ""
|
20
|
+
|
21
|
+
if priority_countries
|
22
|
+
priority_hash = {}
|
23
|
+
priority_countries.each {|code| priority_hash[ISO::Countries.get_country(code)] = code.to_s }
|
24
|
+
country_options += options_for_select(priority_hash.sort, selected)
|
25
|
+
country_options += "<option value=\"\">-------------</option>\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
if priority_countries && priority_countries.include?(selected)
|
29
|
+
country_options += options_for_select(countries_for_select.sort - priority_countries, selected)
|
30
|
+
else
|
31
|
+
country_options += options_for_select(countries_for_select.sort, selected)
|
32
|
+
end
|
33
|
+
|
34
|
+
return country_options
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_iso_select_tag(priority_countries, options, html_options) #:nodoc:
|
38
|
+
if html_options.has_key?(:class)
|
39
|
+
html_options[:class] = html_options[:class] + " country"
|
40
|
+
else
|
41
|
+
html_options[:class] = "country"
|
42
|
+
end
|
43
|
+
html_options = html_options.stringify_keys
|
44
|
+
add_default_name_and_id(html_options)
|
45
|
+
value = value(object)
|
46
|
+
content_tag("select", add_options(iso_options_for_select(value, priority_countries), options, value), html_options)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class ActionView::Helpers::FormBuilder
|
53
|
+
def iso_country_select(method, priority_countries = nil, options = {}, html_options = {})
|
54
|
+
@template.iso_country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# IsoCountries
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
begin
|
5
|
+
require "gettext"
|
6
|
+
rescue
|
7
|
+
require "ostruct"
|
8
|
+
GetText = OpenStruct.new
|
9
|
+
def _(str)
|
10
|
+
str
|
11
|
+
end
|
12
|
+
end
|
13
|
+
stubs = %w(activesupport activerecord actionpack actionmailer activeresource)
|
14
|
+
stubs.each do |stub|
|
15
|
+
require stub
|
16
|
+
end
|
17
|
+
|
18
|
+
require "country_list"
|
19
|
+
require "iso/countries/form_helpers"
|
20
|
+
require "iso/countries/country_field"
|
21
|
+
ActiveRecord::Base.send :include, ISO::Countries::CountryField
|
22
|
+
|
23
|
+
module ISO
|
24
|
+
module Countries
|
25
|
+
module ClassMethods
|
26
|
+
GetText.bindtextdomain "iso_countries", :path => "#{File.dirname(__FILE__)}/../locale"
|
27
|
+
GetText.locale = 'en'
|
28
|
+
|
29
|
+
# Sets the language for country translation
|
30
|
+
def set_language(lang)
|
31
|
+
@@language = lang
|
32
|
+
GetText.bindtextdomain "iso_countries", :path => "#{File.dirname(__FILE__)}/../locale"
|
33
|
+
GetText.locale = lang
|
34
|
+
end
|
35
|
+
|
36
|
+
# Gets te current translation language
|
37
|
+
def language
|
38
|
+
@@language || "en"
|
39
|
+
end
|
40
|
+
|
41
|
+
# Wrapper to get country name from country code. +code+ can be a symbol or a string containing the country code.
|
42
|
+
def get_country(code)
|
43
|
+
_(COUNTRIES[code.to_sym]) rescue nil
|
44
|
+
end
|
45
|
+
|
46
|
+
# Wrapper to get country code from country name.
|
47
|
+
def get_code(name)
|
48
|
+
if COUNTRIES.value?(name)
|
49
|
+
COUNTRIES.each_pair do |k,v|
|
50
|
+
if v.eql?(name)
|
51
|
+
return k.to_s
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns an array with all the available country codes
|
58
|
+
def country_codes
|
59
|
+
COUNTRIES.keys.map { |key| key.to_s }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class << self
|
64
|
+
include ClassMethods
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require "iso_countries"
|
3
|
+
|
4
|
+
class IsoCountriesTest < Test::Unit::TestCase
|
5
|
+
def test_this_plugin
|
6
|
+
assert_equal("Spain", ISO::Countries.get_country("es"))
|
7
|
+
assert_equal("es", ISO::Countries.set_language("es"))
|
8
|
+
assert_equal("España", ISO::Countries.get_country("es"))
|
9
|
+
end
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iso_countries
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jorge Bernal
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2009-01-19 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: jbernal@warp.es
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README
|
29
|
+
files:
|
30
|
+
- README
|
31
|
+
- MIT-LICENSE
|
32
|
+
- Rakefile
|
33
|
+
- init.rb
|
34
|
+
- lib/country_list.rb
|
35
|
+
- lib/iso/countries/country_field.rb
|
36
|
+
- lib/iso/countries/form_helpers.rb
|
37
|
+
- lib/iso_countries.rb
|
38
|
+
- tasks/iso_countries_tasks.rake
|
39
|
+
- test/iso_countries_test.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/koke/iso_countries
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --line-numbers
|
47
|
+
- --inline-source
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements:
|
69
|
+
- gettext
|
70
|
+
rubyforge_project: iso_countries
|
71
|
+
rubygems_version: 1.3.7
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: Country selector with ISO codes
|
75
|
+
test_files: []
|
76
|
+
|