Skip to content
This repository
Browse code

Extract the former country_select helper into a plugin.

The list of countries can be controvertial.  Rather than try and solve the world's political ills, we've removed the functionality, and frozen the list as the ISO 3166 long names list.
  • Loading branch information...
commit c06cd99c0a9e0d3ca9d76326a1f339bd5de4bf0b 0 parents
Michael Koziarski authored September 18, 2008
20  MIT-LICENSE
... ...
@@ -0,0 +1,20 @@
  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.
14  README
... ...
@@ -0,0 +1,14 @@
  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
1  init.rb
... ...
@@ -0,0 +1 @@
  1
+require 'country_select'
2  install.rb
... ...
@@ -0,0 +1,2 @@
  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"
84  lib/country_select.rb
... ...
@@ -0,0 +1,84 @@
  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, nil, 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 += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
  20
+        end
  21
+
  22
+        return country_options + options_for_select(COUNTRIES, selected)
  23
+      end
  24
+      # All the countries included in the country_options output.
  25
+      COUNTRIES = ["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola",
  26
+        "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria",
  27
+        "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
  28
+        "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil",
  29
+        "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",
  30
+        "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
  31
+        "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
  32
+        "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba",
  33
+        "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt",
  34
+        "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)",
  35
+        "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia",
  36
+        "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea",
  37
+        "Guinea-Bissau", "Guyana", "Haiti", "Heard and McDonald Islands", "Holy See (Vatican City State)",
  38
+        "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq",
  39
+        "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya",
  40
+        "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan",
  41
+        "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya",
  42
+        "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, The Former Yugoslav Republic Of",
  43
+        "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique",
  44
+        "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of",
  45
+        "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru",
  46
+        "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger",
  47
+        "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau",
  48
+        "Palestinian Territory, Occupied", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
  49
+        "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation",
  50
+        "Rwanda", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia",
  51
+        "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
  52
+        "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore",
  53
+        "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa",
  54
+        "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "Sudan", "Suriname",
  55
+        "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic",
  56
+        "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Timor-Leste",
  57
+        "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
  58
+        "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
  59
+        "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela",
  60
+        "Viet Nam", "Virgin Islands, British", "Virgin Islands, U.S.", "Wallis and Futuna", "Western Sahara",
  61
+        "Yemen", "Zambia", "Zimbabwe"] unless const_defined?("COUNTRIES")
  62
+    end
  63
+    
  64
+    class InstanceTag
  65
+      def to_country_select_tag(priority_countries, options, html_options)
  66
+        html_options = html_options.stringify_keys
  67
+        add_default_name_and_id(html_options)
  68
+        value = value(object)
  69
+        content_tag("select",
  70
+          add_options(
  71
+            country_options_for_select(value, priority_countries),
  72
+            options, value
  73
+          ), html_options
  74
+        )
  75
+      end
  76
+    end
  77
+    
  78
+    class FormBuilder
  79
+      def country_select(method, priority_countries = nil, options = {}, html_options = {})
  80
+        @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
  81
+      end
  82
+    end
  83
+  end
  84
+end
1  uninstall.rb
... ...
@@ -0,0 +1 @@
  1
+# Uninstall hook code here

0 notes on commit c06cd99

Please sign in to comment.
Something went wrong with that request. Please try again.