module ActionView::Helpers::FormOptionsHelper

Public Instance Methods

country_options_for_select(countries, selected = nil, options = {}) click to toggle source
# File lib/core_extensions/action_view/helpers/form_options_helper.rb, line 38
def country_options_for_select(countries, selected = nil, options = {})
  language = I18n.locale.to_s.sub(/-.*/, '')
  country_options = ''
  priority_countries_codes = options[:priority] || []
  
  unless priority_countries_codes.empty?
    countries = ISO3166::Country.all unless countries.empty?
    priority_countries = priority_countries_codes.map do |code|
      country = ISO3166::Country[code]
      [country.translation(language) || country.name, country.alpha2] if country
    end.compact
    unless priority_countries.empty?
      country_options += options_for_select(priority_countries, selected)
      country_options += "<option disabled>-------------</option>"

      selected = nil if priority_countries_codes.include?(selected)
    end
  end

  collator = ICU::Collation::Collator.new(I18n.locale.to_s)
  main_options = countries.map { |c| [c.translation(language) || c.name, c.alpha2] }
  main_options.sort! { |a, b| collator.compare(a.first, b.first) }
  main_options.unshift [options['prompt'], ''] if options['prompt']

  country_options += options_for_select(main_options, selected)
  country_options.html_safe
end
country_select(object, method, priority_or_options = {}, options = {}, html_options = {}) click to toggle source

Generate select and country option tags for the given object and method. A common use of this would be to allow users to select a state subregion within a given country.

object - The model object to generate the select for method - The attribute on the object options - Other options pertaining to option tag generation. See

`region_options_for_select`.

html_options - Options to use when generating the select tag- class,

id, etc.

Uses country_options_for_select to generate the list of option tags.

Example:

country_select(@object, :country, {priority: ['US', 'CA']}, class: 'country')

Note that in order to preserve compatibility with various existing libraries, an alternative API is supported but not recommended:

country_select(@object, :region, ['US', 'CA'], class: region)

Returns an `html_safe` string containing the HTML for a select element.

# File lib/core_extensions/action_view/helpers/form_options_helper.rb, line 27
def country_select(object, method, priority_or_options = {}, options = {}, html_options = {})
  if Hash === priority_or_options
    html_options = options
    options = priority_or_options
  else
    options[:priority] = priority_or_options
  end
  select_tag = ActionView::Helpers::Tags::Base.new(object, method, self, options)
  select_tag.to_country_select_tag(ISO3166::Country.all, options, html_options)
end