Module: ActionView::Helpers::FormOptionsHelper
- Defined in:
- lib/country-select-iso.rb
Instance Method Summary collapse
- #country_matches(country, value) ⇒ Object
-
#country_options_for_select(selected = nil, options = nil) ⇒ Object
Returns a string of option tags for pretty much any country in the world.
-
#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.
- #state_options_for_select(selected = nil, options = nil) ⇒ Object
- #state_select(object, method, options = {}, html_options = {}) ⇒ Object
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 (selected = nil, = nil) = || {} value = [:value] || :name priority_countries = [:priority_countries] || ["US", "GB", "CA"] countries = ::CountrySelectIso::countries.sort{|c1, c2| c1[:name] <=> c2[:name]} = "" if priority_countries += (countries .select{|c| !priority_countries.index{|pc| country_matches(c, pc)}.nil?} .map{|c| [c[:name], c[value]]}, selected) += "<option value=\"\" disabled=\"disabled\">-------------</option>\n" end + (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, = {}, = {}) InstanceTag.new(object, method, self, .delete(:object)).to_country_select_tag(, ) 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 (selected = nil, = nil) = || {} country = [:country] || "US" value = [:value] || :full_code name = [:name] || :localized_name states = ::CountrySelectIso::states.select{|s| s[:country_iso2] == country}.sort{|s1, s2| s1[name] <=> s2[name]} (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, = {}, = {}) InstanceTag.new(object, method, self, .delete(:object)).to_state_select_tag(, ) end |