Module: ActionView::Helpers::FormOptionsHelper
- Defined in:
- lib/carmen/rails/action_view/form_helper.rb
Instance Method Summary collapse
-
#country_select(object, method, priorities_or_options = {}, options_or_html_options = {}, html_options = {}) ⇒ Object
Generate select and country option tags for the given object and method.
-
#country_select_tag(name, value, options = {}) ⇒ Object
Generate select and country option tags with the provided name.
-
#region_options_for_select(regions, selected = nil, options = {}) ⇒ Object
Generate option tags for a collection of regions.
-
#subregion_select(object, method, parent_region_or_code, options = {}, html_options = {}) ⇒ Object
Generate select and subregion option tags for the given object and method.
-
#subregion_select_tag(name, value, parent_region_or_code, options = {}, html_options = {}) ⇒ Object
Generate select and subregion option tags for the given object and method.
Instance Method Details
#country_select(object, method, priorities_or_options = {}, options_or_html_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 region_options_or_select to generate the list of option tags.
Example:
country_select(@object, :region, {priority: ['US', 'CA']}, class: 'region')
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.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/carmen/rails/action_view/form_helper.rb', line 54 def country_select(object, method, = {}, = {}, = {}) if .is_a? Array = [:priority] = else = = end tag = instance_tag(object, method, self, ) tag.to_region_select_tag(Carmen::World.instance, , ) end |
#country_select_tag(name, value, options = {}) ⇒ Object
Generate select and country option tags with the provided name. A common use of this would be to allow users to select a country name inside a web form.
name - The name attribute for the select element. 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 region_options_or_select to generate the list of option tags.
Example:
country_select_tag('country_code', {priority: ['US', 'CA']}, class: 'region')
Returns an html_safe string containing the HTML for a select element.
130 131 132 |
# File 'lib/carmen/rails/action_view/form_helper.rb', line 130 def country_select_tag(name, value, ={}) subregion_select_tag(name, value, Carmen::World.instance, ) end |
#region_options_for_select(regions, selected = nil, options = {}) ⇒ Object
Generate option tags for a collection of regions.
regions - An array or Carmen::RegionCollection containing Carmen::Regions selected - the code of the region that should be selected options - The hash of options used to customize the output (default: {}):
To use priority regions (which are included in a special section at the top of the list), provide an array of region codes at the :priority option:
(@region.subregions, 'US', priority: ['US', 'CA'])
Returns an html_safe string containing option tags.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/carmen/rails/action_view/form_helper.rb', line 80 def (regions, selected=nil, ={}) .stringify_keys! priority_region_codes = ['priority'] || [] = "" unless priority_region_codes.empty? unless regions.respond_to?(:coded) regions = Carmen::RegionCollection.new(regions) end priority_regions = priority_region_codes.map do |code| region = regions.coded(code) [region.name, region.code] if region end.compact unless priority_regions.empty? += (priority_regions, selected) += "<option disabled>-------------</option>" # If a priority region is selected, don't select it again in the main list. # This prevents some browsers from selecting the second occurance of this region, # which makes it difficult to select an alternative priority region. selected = nil if priority_region_codes.include?(selected) end end = regions.map { |r| [r.name, r.code] } .sort!{|a, b| a.first.to_s <=> b.first.to_s} .unshift [['prompt'], ''] if ['prompt'] += (, selected) .html_safe end |
#subregion_select(object, method, parent_region_or_code, options = {}, html_options = {}) ⇒ Object
Generate select and subregion 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 parent_region_or_code - An instance of Carmen::Region or a 2-character
country code.
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 region_options_for_select to generate the list of option tags.
Example:
subregion_select(@object, :region, {priority: ['US', 'CA']}, class: 'region')
Returns an html_safe string containing the HTML for a select element.
25 26 27 28 29 |
# File 'lib/carmen/rails/action_view/form_helper.rb', line 25 def subregion_select(object, method, parent_region_or_code, ={}, ={}) parent_region = determine_parent(parent_region_or_code) tag = instance_tag(object, method, self, ) tag.to_region_select_tag(parent_region, , ) end |
#subregion_select_tag(name, value, parent_region_or_code, options = {}, html_options = {}) ⇒ Object
Generate select and subregion 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.
name - The name attribute for the select element. parent_region_or_code - An instance of Carmen::Region or a 2-character
country code.
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 region_options_or_select to generate the list of option tags.
Example:
subregion_select_tag('state_code', 'US', {priority: ['US', 'CA']}, class: 'region')
Returns an html_safe string containing the HTML for a select element.
153 154 155 156 157 158 159 160 |
# File 'lib/carmen/rails/action_view/form_helper.rb', line 153 def subregion_select_tag(name, value, parent_region_or_code, = {}, = {}) .stringify_keys! parent_region = determine_parent(parent_region_or_code) opts = (parent_region.subregions, value, ) = {"name" => name, "id" => sanitize_to_id(name)}.update(.stringify_keys) content_tag(:select, opts, ) end |