Module: ActionView::Helpers::FormOptionsHelper
- Defined in:
- lib/core_extensions/action_view/helpers/form_options_helper.rb
Instance Method Summary collapse
- #country_options_for_select(countries, selected = nil, options = {}) ⇒ Object
-
#country_select(object, method, priority_or_options = {}, options = {}, html_options = {}) ⇒ Object
Generate select and country option tags for the given object and method.
Instance Method Details
#country_options_for_select(countries, selected = nil, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/core_extensions/action_view/helpers/form_options_helper.rb', line 38 def (countries, selected = nil, = {}) language = I18n.locale.to_s.sub(/-.*/, '') = '' priority_countries_codes = [:priority] || [] unless priority_countries_codes.empty? countries = ISO3166::Country.all unless countries.empty? priority_countries = priority_countries_codes.map do |code| country = ISO3166::Country[code] [country.translation(language) || country.name, country.alpha2] if country end.compact unless priority_countries.empty? += (priority_countries, selected) += "<option disabled>-------------</option>" selected = nil if priority_countries_codes.include?(selected) end end collator = ICU::Collation::Collator.new(I18n.locale.to_s) = countries.map { |c| [c.translation(language) || c.name, c.alpha2] } .sort! { |a, b| collator.compare(a.first, b.first) } .unshift [['prompt'], ''] if ['prompt'] += (, selected) .html_safe end |
#country_select(object, method, priority_or_options = {}, options = {}, html_options = {}) ⇒ Object
Generate select and country option tags for the given object and method. A common use of this would be to allow users to select a state subregion within a given country.
object - The model object to generate the select for method - The attribute on the object options - Other options pertaining to option tag generation. See
`region_options_for_select`.
html_options - Options to use when generating the select tag- class,
id, etc.
Uses country_options_for_select to generate the list of option tags.
Example:
country_select(@object, :country, {priority: ['US', 'CA']}, class: 'country')
Note that in order to preserve compatibility with various existing libraries, an alternative API is supported but not recommended:
country_select(@object, :region, ['US', 'CA'], class: region)
Returns an ‘html_safe` string containing the HTML for a select element.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/core_extensions/action_view/helpers/form_options_helper.rb', line 27 def country_select(object, method, = {}, = {}, = {}) if Hash === = = else [:priority] = end select_tag = ActionView::Helpers::Tags::Base.new(object, method, self, ) select_tag.to_country_select_tag(ISO3166::Country.all, , ) end |