Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/uncharted/extensions/rails.rb

Instance Method Summary collapse

Instance Method Details

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



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/uncharted/extensions/rails.rb', line 16

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

  if priority_countries
    country_options += options_from_collection_for_select(Country.find(priority_countries), :alpha2, :name, 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 + options_from_collection_for_select(Uncharted::Country.countries, :alpha2, :name, selected)
end

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



7
8
9
# File 'lib/uncharted/extensions/rails.rb', line 7

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

#territory_options_for_select(selected = nil, priority_territories = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/uncharted/extensions/rails.rb', line 30

def territory_options_for_select(selected = nil, priority_territories = nil)
  territory_options = ""

  if priority_territories
    territory_options += options_from_collection_for_select(Territory.find(priority_territories), :alpha2, :name, selected)
    territory_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_territories.include?(selected)
  end
  territory_options + options_from_collection_for_select(Uncharted::Country.territories, :alpha2, :name, selected)
end