From b34eab1ffe232e3db07a5ed1e8361796bf98e241 Mon Sep 17 00:00:00 2001 From: Dan Melnick Date: Wed, 17 Jul 2013 14:30:16 -0500 Subject: [PATCH] Allow users to pass an abbreviations option to subregion_select to display region abbreviations as the visible element in the option --- README.md | 8 ++++++++ lib/carmen/rails/action_view/form_helper.rb | 15 ++++++++++++--- .../action_view/helpers/form_helper_spec.rb | 17 ++++++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 02c0772..2c6c465 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,14 @@ And then in your form something like this: More docs coming soon. In the meantime, all of the public methods in carmen-rails [have been thoroughly TomDoc'ed](https://github.com/jim/carmen-rails/blob/master/lib/carmen/rails/action_view/form_helper.rb). +## How do I show just the abbreivated names of regions? + +You can pass an abbreviations option to the region select: + +``` erb +<%= subregion_select(object, method, parent_region, abbreviations: true) %> +``` + ### Demo app There is a [live demo app](http://carmen-rails-demo.herokuapp.com) that shows diff --git a/lib/carmen/rails/action_view/form_helper.rb b/lib/carmen/rails/action_view/form_helper.rb index 8aee626..1a1c44b 100644 --- a/lib/carmen/rails/action_view/form_helper.rb +++ b/lib/carmen/rails/action_view/form_helper.rb @@ -91,7 +91,7 @@ def region_options_for_select(regions, selected=nil, options={}) priority_regions = priority_region_codes.map do |code| region = regions.coded(code) - [region.name, region.code] if region + option_values(region, options['abbreviations']) end.compact unless priority_regions.empty? region_options += options_for_select(priority_regions, selected) @@ -99,7 +99,7 @@ def region_options_for_select(regions, selected=nil, options={}) end end - main_options = regions.map { |r| [r.name, r.code] } + main_options = regions.map { |r| option_values(r, options['abbreviations'])} main_options.sort!{|a, b| a.first.to_s <=> b.first.to_s} main_options.unshift [options['prompt'], ''] if options['prompt'] @@ -158,6 +158,14 @@ def subregion_select_tag(name, value, parent_region_or_code, options = {}, html_ private + def option_values(region, abbreviations = false) + if abbreviations + [region.code, region.code] + else + [region.name, region.code] + end + end + def determine_parent(parent_region_or_code) case parent_region_or_code when String @@ -177,8 +185,9 @@ def to_region_select_tag(parent_region, options = {}, html_options = {}) html_options = html_options.stringify_keys add_default_name_and_id(html_options) priority_regions = options[:priority] || [] + abbreviations = options[:abbreviations] || false value = options[:selected] ? options[:selected] : value(object) - opts = add_options(region_options_for_select(parent_region.subregions, value, :priority => priority_regions), options, value) + opts = add_options(region_options_for_select(parent_region.subregions, value, :priority => priority_regions, :abbreviations => abbreviations), options, value) content_tag("select", opts, html_options) end end diff --git a/spec/carmen/action_view/helpers/form_helper_spec.rb b/spec/carmen/action_view/helpers/form_helper_spec.rb index 92f015f..e811d49 100644 --- a/spec/carmen/action_view/helpers/form_helper_spec.rb +++ b/spec/carmen/action_view/helpers/form_helper_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require_relative '../../../spec_helper' class CarmenViewHelperTest < MiniTest::Unit::TestCase include ActionView::Helpers::FormOptionsHelper @@ -131,6 +131,19 @@ def test_subregion_select_using_parent_code_array assert_equal_markup(expected, html) end + def test_basic_subregion_select_with_abbreivations + oceania = Carmen::Country.coded('OC') + expected = <<-HTML + + HTML + + html = subregion_select(@object, :subregion_code, oceania, abbreviations: true) + + assert_equal_markup(expected, html) + end + def test_subregion_selected_value @object.subregion_code = 'AO' oceania = Carmen::Country.coded('OC') @@ -154,6 +167,8 @@ def test_html_options_for_selected_value_with_priority_and_selected_options assert_select('.test_html_options') end + + def test_basic_subregion_select_tag oceania = Carmen::Country.coded('OC')