Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/country-select-iso.rb

Instance Method Summary collapse

Instance Method Details

#country_matches(country, value) ⇒ Object



48
49
50
# File 'lib/country-select-iso.rb', line 48

def country_matches country, value
  country[:name] == value || country[:iso2] == value || country[:iso3] == value
end

#country_options_for_select(selected = nil, options = 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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/country-select-iso.rb', line 20

def country_options_for_select(selected = nil, options = nil)
  options = options || {}
  value = options[:value] || :name
  priority_countries = options[:priority_countries] || ["US", "GB", "CA"]

  countries = ::CountrySelectIso::countries.sort{|c1, c2| c1[:name] <=> c2[:name]}

  country_options = ""

  if priority_countries
    country_options += options_for_select(countries
      .select{|c|  !priority_countries.index{|pc| country_matches(c, pc)}.nil?}
      .map{|c| [c[:name], c[value]]}, selected)
    country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
  end

  country_options + options_for_select(countries.map{|c| [c[:name], c[value]]}, selected)
end

#country_select(object, method, 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.



8
9
10
# File 'lib/country-select-iso.rb', line 8

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

#state_options_for_select(selected = nil, options = nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/country-select-iso.rb', line 39

def state_options_for_select(selected = nil, options = nil)
  options = options || {}
  country = options[:country] || "US"
  value = options[:value] || :full_code
  name = options[:name] || :localized_name
  states = ::CountrySelectIso::states.select{|s| s[:country_iso2] == country}.sort{|s1, s2| s1[name] <=> s2[name]}
  options_for_select(states.map{|s| [s[name], s[value]]})
end

#state_select(object, method, options = {}, html_options = {}) ⇒ Object



12
13
14
# File 'lib/country-select-iso.rb', line 12

def state_select(object, method, options = {}, html_options = {})
  InstanceTag.new(object, method, self, options.delete(:object)).to_state_select_tag(options, html_options)
end