Module: ActionView::Helpers::FormOptionsHelper
- Defined in:
- lib/country_select_yaml.rb
Constant Summary collapse
- COUNTRIES =
YAML.load_file(path_to_yaml)
Instance Method Summary collapse
-
#country_options_for_select(selected = nil, priority_countries = nil, lang = nil) ⇒ Object
Returns a string of option tags for pretty much any country in the world.
-
#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.
Instance Method Details
#country_options_for_select(selected = nil, priority_countries = nil, lang = 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/country_select_yaml.rb', line 47 def (selected = nil, priority_countries = nil, lang = nil) = "".html_safe if priority_countries += (priority_countries, selected) += "<option value=\"\" disabled=\"disabled\">-------------</option>\n".html_safe # # 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 return + (COUNTRIES[lang || "us"], 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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/country_select_yaml.rb', line 18 def country_select(object, method, priority_countries = nil, = {}, = {}) tag = if defined?(ActionView::Helpers::InstanceTag) && ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0 InstanceTag.new(object, method, self, .delete(:object)) else CountrySelect.new(object, method, self, ) end tag.to_country_select_tag(priority_countries, , ) end |