Skip to content

Commit

Permalink
Allow users to pass an abbreviations option to subregion_select to di…
Browse files Browse the repository at this point in the history
…splay region abbreviations as the visible element in the option
  • Loading branch information
Dan Melnick committed Jul 17, 2013
1 parent 2923ea7 commit b34eab1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions lib/carmen/rails/action_view/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ 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)
region_options += "<option disabled>-------------</option>"
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']

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion spec/carmen/action_view/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'spec_helper'
require_relative '../../../spec_helper'

class CarmenViewHelperTest < MiniTest::Unit::TestCase
include ActionView::Helpers::FormOptionsHelper
Expand Down Expand Up @@ -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
<select id="object_subregion_code" name="object[subregion_code]">
<option value="AO">AO</option>
</select>
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')
Expand All @@ -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')
Expand Down

0 comments on commit b34eab1

Please sign in to comment.