Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/countries/select_helper.rb

Instance Method Summary collapse

Instance Method Details

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/countries/select_helper.rb', line 10

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

  if priority_countries
    priority_countries = [*priority_countries].map {|x| [x,ISO3166::Country::NameIndex[x]] }
    country_options += options_for_select(priority_countries, selected)
    country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"

    # prevent 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?([ISO3166::Country[selected].name, selected])
  end

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

  countries = ISO3166::Country::Names.map{|(name,alpha2)| [name.html_safe,alpha2] }

  return country_options + options_for_select(countries, selected)
end

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



6
7
8
# File 'lib/countries/select_helper.rb', line 6

def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
  InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options)
end