├── country-select.gemspec ├── MIT-LICENSE ├── README.md └── lib └── country-select.rb /country-select.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = 'country-select' 3 | s.version = '1.2.1' 4 | s.date = '2012-03-02' 5 | s.summary = 'Country select box' 6 | s.description = 'Provides a form helper to insert a country select box using the ISO 3166 country list' 7 | s.authors = [ 'Michael Koziarski', 'James Dean Shepherd' ] 8 | s.email = 'jamesds2007@gmail.com' 9 | s.files = [ 'lib/country-select.rb', 10 | 'README.md', 11 | 'MIT-LICENSE' ] 12 | s.homepage = 'http://github.com/jamesds/country-select' 13 | end 14 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008 Michael Koziarski 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. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Country-Select 2 | 3 | _Compatible with Rails 5.2_ 4 | 5 | Provides a simple helper to get a HTML select list of countries. The list of countries comes from the ISO 3166 standard. While it is a relatively neutral source of country names, it may still offend some users. 6 | 7 | Users are strongly advised to evaluate the suitability of this list given their user base. 8 | 9 | ## Installation 10 | 11 | Install as a gem using 12 | 13 | gem install country-select 14 | 15 | Or put the following in your Gemfile 16 | 17 | gem 'country-select' 18 | 19 | ## Example 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: 26 | 27 | country_select("user", "country_name", [ "United Kingdom", "France", "Germany" ]) 28 | 29 | Supplying additiontal options: 30 | 31 | country_select("user", "country_name", [ "United Kingdom", "France", "Germany" ], {:prompt => "Choose Country"}, {:class => "country-chooser"}) 32 | 33 | Using together with `form_for` or `fields_for`: 34 | 35 | <%= form_for @user do |f| %> 36 | <%= f.country_select("country_name") %> 37 | <% end %> 38 | 39 | Copyright (c) 2008 Michael Koziarski, released under the MIT license 40 | -------------------------------------------------------------------------------- /lib/country-select.rb: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | # CountrySelect 3 | module ActionView 4 | module Helpers 5 | module FormOptionsHelper 6 | # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags. 7 | def country_select(object, method, priority_countries = nil, options = {}, html_options = {}) 8 | if Rails.version.starts_with?("3") 9 | InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options) 10 | else 11 | Tags::CountrySelect.new(object, method, self, options).to_country_select_tag(priority_countries, options, html_options) 12 | end 13 | end 14 | # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to 15 | # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so 16 | # that they will be listed above the rest of the (long) list. 17 | # 18 | # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag. 19 | def country_options_for_select(selected = nil, priority_countries = nil) 20 | country_options = "" 21 | 22 | if priority_countries 23 | if (unlisted = priority_countries - COUNTRIES).any? 24 | raise RuntimeError.new("Supplied priority countries are not in the main list: #{unlisted}") 25 | end 26 | country_options += options_for_select(priority_countries, selected) 27 | country_options += "\n" 28 | 29 | # prevents selected from being included twice in the HTML which causes 30 | # some browsers to select the second selected option (not priority) 31 | # which makes it harder to select an alternative priority country 32 | selected = nil if priority_countries.include?(selected) 33 | end 34 | 35 | country_options = country_options.html_safe if country_options.respond_to?(:html_safe) 36 | 37 | return country_options + options_for_select(COUNTRIES, selected) 38 | end 39 | 40 | # All the countries included in the country_options output. 41 | COUNTRIES = [ 42 | "Afghanistan", 43 | "Åland Islands", 44 | "Albania", 45 | "Algeria", 46 | "American Samoa", 47 | "Andorra", 48 | "Angola", 49 | "Anguilla", 50 | "Antarctica", 51 | "Antigua and Barbuda", 52 | "Argentina", 53 | "Armenia", 54 | "Aruba", 55 | "Australia", 56 | "Austria", 57 | "Azerbaijan", 58 | "Bahamas", 59 | "Bahrain", 60 | "Bangladesh", 61 | "Barbados", 62 | "Belarus", 63 | "Belgium", 64 | "Belize", 65 | "Benin", 66 | "Bermuda", 67 | "Bhutan", 68 | "Bolivia", 69 | "Bonaire, Sint Eustatius and Saba", 70 | "Bosnia and Herzegovina", 71 | "Botswana", 72 | "Bouvet Island", 73 | "Brazil", 74 | "British Indian Ocean Territory", 75 | "Brunei Darussalam", 76 | "Bulgaria", 77 | "Burkina Faso", 78 | "Burundi", 79 | "Cambodia", 80 | "Cameroon", 81 | "Canada", 82 | "Cape Verde", 83 | "Cayman Islands", 84 | "Central African Republic", 85 | "Chad", 86 | "Chile", 87 | "China", 88 | "Christmas Island", 89 | "Cocos (Keeling) Islands", 90 | "Colombia", 91 | "Comoros", 92 | "Congo", 93 | "Congo, The Democratic Republic of the", 94 | "Cook Islands", 95 | "Costa Rica", 96 | "Côte d'Ivoire", 97 | "Croatia", 98 | "Cuba", 99 | "Curaçao", 100 | "Cyprus", 101 | "Czech Republic", 102 | "Denmark", 103 | "Djibouti", 104 | "Dominica", 105 | "Dominican Republic", 106 | "Ecuador", 107 | "Egypt", 108 | "El Salvador", 109 | "Equatorial Guinea", 110 | "Eritrea", 111 | "Estonia", 112 | "Eswatini", 113 | "Ethiopia", 114 | "Falkland Islands (Malvinas)", 115 | "Faroe Islands", 116 | "Fiji", 117 | "Finland", 118 | "France", 119 | "French Guiana", 120 | "French Polynesia", 121 | "French Southern Territories", 122 | "Gabon", 123 | "Gambia", 124 | "Georgia", 125 | "Germany", 126 | "Ghana", 127 | "Gibraltar", 128 | "Greece", 129 | "Greenland", 130 | "Grenada", 131 | "Guadeloupe", 132 | "Guam", 133 | "Guatemala", 134 | "Guernsey", 135 | "Guinea", 136 | "Guinea-Bissau", 137 | "Guyana", 138 | "Haiti", 139 | "Heard Island and McDonald Islands", 140 | "Holy See (Vatican City State)", 141 | "Honduras", 142 | "Hong Kong", 143 | "Hungary", 144 | "Iceland", 145 | "India", 146 | "Indonesia", 147 | "Iran, Islamic Republic of", 148 | "Iraq", 149 | "Ireland", 150 | "Isle of Man", 151 | "Israel", 152 | "Italy", 153 | "Jamaica", 154 | "Japan", 155 | "Jersey", 156 | "Jordan", 157 | "Kazakhstan", 158 | "Kenya", 159 | "Kiribati", 160 | "Korea, Democratic People's Republic of", 161 | "Korea, Republic of", 162 | "Kuwait", 163 | "Kyrgyzstan", 164 | "Lao People's Democratic Republic", 165 | "Latvia", 166 | "Lebanon", 167 | "Lesotho", 168 | "Liberia", 169 | "Libya", 170 | "Liechtenstein", 171 | "Lithuania", 172 | "Luxembourg", 173 | "Macao", 174 | "Republic of North Macedonia", 175 | "Madagascar", 176 | "Malawi", 177 | "Malaysia", 178 | "Maldives", 179 | "Mali", 180 | "Malta", 181 | "Marshall Islands", 182 | "Martinique", 183 | "Mauritania", 184 | "Mauritius", 185 | "Mayotte", 186 | "Mexico", 187 | "Micronesia, Federated States of", 188 | "Moldova, Republic of", 189 | "Monaco", 190 | "Mongolia", 191 | "Montenegro", 192 | "Montserrat", 193 | "Morocco", 194 | "Mozambique", 195 | "Myanmar", 196 | "Namibia", 197 | "Nauru", 198 | "Nepal", 199 | "Netherlands", 200 | "New Caledonia", 201 | "New Zealand", 202 | "Nicaragua", 203 | "Niger", 204 | "Nigeria", 205 | "Niue", 206 | "Norfolk Island", 207 | "Northern Mariana Islands", 208 | "Norway", 209 | "Oman", 210 | "Pakistan", 211 | "Palau", 212 | "Palestine, State of", 213 | "Panama", 214 | "Papua New Guinea", 215 | "Paraguay", 216 | "Peru", 217 | "Philippines", 218 | "Pitcairn", 219 | "Poland", 220 | "Portugal", 221 | "Puerto Rico", 222 | "Qatar", 223 | "Reunion", 224 | "Romania", 225 | "Russian Federation", 226 | "Rwanda", 227 | "Saint Barthélemy", 228 | "Saint Helena, Ascension and Tristan da Cunha", 229 | "Saint Kitts and Nevis", 230 | "Saint Lucia", 231 | "Saint Martin (French Part)", 232 | "Saint Pierre and Miquelon", 233 | "Saint Vincent and the Grenadines", 234 | "Samoa", 235 | "San Marino", 236 | "Sao Tome and Principe", 237 | "Saudi Arabia", 238 | "Senegal", 239 | "Serbia", 240 | "Seychelles", 241 | "Sierra Leone", 242 | "Singapore", 243 | "Sint Maarten (Dutch Part)", 244 | "Slovakia", 245 | "Slovenia", 246 | "Solomon Islands", 247 | "Somalia", 248 | "South Africa", 249 | "South Georgia and the South Sandwich Islands", 250 | "South Sudan", 251 | "Spain", 252 | "Sri Lanka", 253 | "Sudan", 254 | "Suriname", 255 | "Svalbard and Jan Mayen", 256 | "Sweden", 257 | "Switzerland", 258 | "Syrian Arab Republic", 259 | "Taiwan", 260 | "Tajikistan", 261 | "Tanzania, United Republic of", 262 | "Thailand", 263 | "Timor-Leste", 264 | "Togo", 265 | "Tokelau", 266 | "Tonga", 267 | "Trinidad and Tobago", 268 | "Tunisia", 269 | "Turkey", 270 | "Turkmenistan", 271 | "Turks and Caicos Islands", 272 | "Tuvalu", 273 | "Uganda", 274 | "Ukraine", 275 | "United Arab Emirates", 276 | "United Kingdom", 277 | "United States", 278 | "United States Minor Outlying Islands", 279 | "Uruguay", 280 | "Uzbekistan", 281 | "Vanuatu", 282 | "Venezuela", 283 | "Viet Nam", 284 | "Virgin Islands, British", 285 | "Virgin Islands, U.S.", 286 | "Wallis and Futuna", 287 | "Western Sahara", 288 | "Yemen", 289 | "Zambia", 290 | "Zimbabwe" 291 | ] unless const_defined?("COUNTRIES") 292 | end 293 | 294 | module CountrySelectTag 295 | def to_country_select_tag(priority_countries, options, html_options) 296 | html_options = html_options.stringify_keys 297 | add_default_name_and_id(html_options) 298 | content_tag("select", 299 | 300 | add_options( 301 | country_options_for_select(value, priority_countries), 302 | options, value 303 | ), html_options 304 | ) 305 | end 306 | end 307 | 308 | # For Rails 4.x / 5.x 309 | if Rails.version.to_i >= 4 310 | module Tags 311 | class CountrySelect < Base 312 | include CountrySelectTag 313 | end 314 | end 315 | else 316 | # For Rails 3.x 317 | class InstanceTag 318 | include CountrySelectTag 319 | end 320 | end 321 | 322 | class FormBuilder 323 | def country_select(method, priority_countries = nil, options = {}, html_options = {}) 324 | @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options) 325 | end 326 | end 327 | end 328 | end 329 | --------------------------------------------------------------------------------