├── MIT-LICENSE ├── README ├── init.rb ├── install.rb ├── lib └── country_select.rb └── uninstall.rb /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: -------------------------------------------------------------------------------- 1 | CountrySelect 2 | ============= 3 | 4 | Provides a simple helper to get an 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 will still offend some users. 5 | 6 | Users are strongly advised to evaluate the suitability of this list given their user base. 7 | 8 | Example 9 | ======= 10 | 11 | country_select("user", "country_name") 12 | 13 | 14 | Copyright (c) 2008 Michael Koziarski, released under the MIT license 15 | -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | require 'country_select' -------------------------------------------------------------------------------- /install.rb: -------------------------------------------------------------------------------- 1 | # Install hook code here 2 | puts "The list of countries provided by this plugin may offend some users. Please review it carefully before you use it" -------------------------------------------------------------------------------- /lib/country_select.rb: -------------------------------------------------------------------------------- 1 | # CountrySelect 2 | module ActionView 3 | module Helpers 4 | module FormOptionsHelper 5 | # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags. 6 | def country_select(object, method, priority_countries = nil, options = {}, html_options = {}) 7 | InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options) 8 | end 9 | # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to 10 | # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so 11 | # 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 | def country_options_for_select(selected = nil, priority_countries = nil) 15 | country_options = "" 16 | 17 | if priority_countries 18 | country_options += options_for_select(priority_countries, selected) 19 | country_options += "\n" 20 | # prevents selected from being included twice in the HTML which causes 21 | # some browsers to select the second selected option (not priority) 22 | # which makes it harder to select an alternative priority country 23 | selected=nil if priority_countries.include?(selected) 24 | end 25 | 26 | return country_options + options_for_select(COUNTRIES, selected) 27 | end 28 | # All the countries included in the country_options output. 29 | COUNTRIES = ["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", 30 | "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", 31 | "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", 32 | "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", 33 | "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", 34 | "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", 35 | "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", 36 | "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", 37 | "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", 38 | "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", 39 | "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", 40 | "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", 41 | "Guinea-Bissau", "Guyana", "Haiti", "Heard and McDonald Islands", "Holy See (Vatican City State)", 42 | "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq", 43 | "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", 44 | "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", 45 | "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", 46 | "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, The Former Yugoslav Republic Of", 47 | "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", 48 | "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", 49 | "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", 50 | "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", 51 | "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", 52 | "Palestinian Territory, Occupied", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", 53 | "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", 54 | "Rwanda", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", 55 | "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", 56 | "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", 57 | "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", 58 | "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "Sudan", "Suriname", 59 | "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", 60 | "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Timor-Leste", 61 | "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", 62 | "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 63 | "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", 64 | "Viet Nam", "Virgin Islands, British", "Virgin Islands, U.S.", "Wallis and Futuna", "Western Sahara", 65 | "Yemen", "Zambia", "Zimbabwe"] unless const_defined?("COUNTRIES") 66 | end 67 | 68 | class InstanceTag 69 | def to_country_select_tag(priority_countries, options, html_options) 70 | html_options = html_options.stringify_keys 71 | add_default_name_and_id(html_options) 72 | value = value(object) 73 | content_tag("select", 74 | add_options( 75 | country_options_for_select(value, priority_countries), 76 | options, value 77 | ), html_options 78 | ) 79 | end 80 | end 81 | 82 | class FormBuilder 83 | def country_select(method, priority_countries = nil, options = {}, html_options = {}) 84 | @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options) 85 | end 86 | end 87 | end 88 | end -------------------------------------------------------------------------------- /uninstall.rb: -------------------------------------------------------------------------------- 1 | # Uninstall hook code here 2 | --------------------------------------------------------------------------------