Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/country_select.rb

Constant Summary collapse

COUNTRIES =
(CountryCodes.countries_for_select('name', 'a2') - 
[["Indonesia", "ID"],["Ghana", "GH"],["Nigeria", "NG"], ["Antartica", "AQ"], ["Puerto Rico", "PR"]]).sort

Instance Method Summary collapse

Instance Method Details

#country_options_for_select(selected = nil, priority_countries = nil) ⇒ Object

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.

NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/country_select.rb', line 47

def country_options_for_select(selected = nil, priority_countries = nil)
  country_options = "".html_safe

  if priority_countries
    country_options += options_for_select(priority_countries, selected)
    country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n".html_safe
    #
    # prevents selected from being included
    # twice in the HTML which causes
    # some browsers to select the second
    # selected option (not priority)
    # which makes it harder to select an
    # alternative priority country
    #
    selected = nil if priority_countries.include?(selected)
  end

  return country_options + options_for_select(COUNTRIES, selected)
end

#country_select(object, method, priority_countries = nil, options = {}, html_options = {}) ⇒ Object

Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/country_select.rb', line 18

def country_select(object, method, priority_countries = nil,
                                   options = {},
                                   html_options = {})

  tag = if defined?(ActionView::Helpers::InstanceTag) &&
          ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0

          InstanceTag.new(object, method, self, options.delete(:object))
        else
          CountrySelect.new(object, method, self, options)
        end

  tag.to_country_select_tag(priority_countries, options, html_options)
end