lib/carmen/rails/action_view/form_helper.rb
# 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_options_for_select(@region.subregions, 'US', priority: ['US', 'CA'])
#
# Returns an `html_safe` string containing option tags.
Generate option tags for a collection of regions.

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.



Returns an `html_safe` string containing option tags.
Generate option tags for a collection of regions . 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 . Returns an ` html_safe ` string containing option tags .
def region_options_for_select(regions, selected=nil, options={})
        options.stringify_keys!
        priority_region_codes = options['priority'] || []
        region_options = ""
        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?
            region_options += options_for_select(priority_regions, selected)
            region_options += "<option disabled>-------------</option>"
          end
        end
        main_options = regions.map { |r| [r.name, r.code] }
        main_options.sort!{|a, b| a.first.to_s <=> b.first.to_s}
        main_options.unshift [options['prompt'], ''] if options['prompt']
        region_options += options_for_select(main_options, selected)
        region_options.html_safe
      end
def region_options_for_select ( regions , selected = nil , options = { } ) options . stringify_keys! priority_region_codes = options [ 'priority' ] || [ ] region_options = "" 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? region_options += options_for_select ( priority_regions , selected ) region_options += "<option disabled>-------------</option>" end end main_options = regions . map { | r | [ r . name , r . code ] } main_options . sort! { | a , b | a . first . to_s <=> b . first . to_s } main_options . unshift [ options [ 'prompt' ] , '' ] if options [ 'prompt' ] region_options += options_for_select ( main_options , selected ) region_options . html_safe end
Generate option tags for a collection of regions.
Generate option tags for a collection of regions .
regions
selected
options