Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/core_extensions/action_view/helpers/form_options_helper.rb

Instance Method Summary collapse

Instance Method Details

#country_options_for_select(countries, selected = nil, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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 = {}) ⇒ Object

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.



27
28
29
30
31
32
33
34
35
36
# 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