module ActionView::Helpers::FormOptionsHelper

Constants

COUNTRIES

All the countries included in the country_options output.

COUNTRIES_WITH_CODE

All the countries included in the country_options output with the code.

Public Instance Methods

country_options_for_select(selected = nil, priority_countries = nil, options = nil) click to toggle source

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.

# File lib/country-select-code.rb, line 25
def country_options_for_select(selected = nil, priority_countries = nil, options = nil)
  country_options = ""

  countries = options[:with_country_code].nil? ? COUNTRIES : COUNTRIES_WITH_CODE

  if priority_countries
    if (unlisted = priority_countries - countries).any?
      raise RuntimeError.new("Supplied priority countries are not in the main list: #{unlisted}")
    end
    country_options += options_for_select(priority_countries, selected)
    country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"

    # 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

  country_options = country_options.html_safe if country_options.respond_to?(:html_safe)

  return country_options + options_for_select(countries, selected)
end
country_select_code(object, method, priority_countries = nil, options = {}, html_options = {}) click to toggle source

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

# File lib/country-select-code.rb, line 9
def country_select_code(object, method, priority_countries = nil, options = {}, html_options = {})

  if defined?(ActionView::Helpers::InstanceTag) && ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0
    tag = InstanceTag.new(object, method, self, options.delete(:object))
  else
    tag = CountrySelectCode.new(object, method, self, options)
  end

   tag.to_country_select_code_tag(priority_countries, options, html_options)
end