Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows to set multiple groups of priority regions #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/carmen/rails/action_view/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ def region_options_for_select(regions, selected=nil, options={})
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>"
priority_region_codes = [priority_region_codes] unless priority_region_codes.first.is_a?(Array)
priority_region_codes.each do |prc|
priority_regions = prc.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
end

Expand Down
15 changes: 15 additions & 0 deletions spec/carmen/action_view/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ def test_region_options_for_select_with_array_of_regions_and_priority
assert_equal_markup(expected, html)
end

def test_region_options_for_select_with_multiple_groups_of_priority
html = region_options_for_select(Carmen::Country.all, nil, priority: [['ES'], ['EU']])
expected = <<-HTML
<option value="ES">Eastasia</option>
<option disabled>-------------</option>
<option value="EU">Eurasia</option>
<option disabled>-------------</option>
<option value="ES">Eastasia</option>
<option value="EU">Eurasia</option>
<option value="OC">Oceania</option>
HTML

assert_equal_markup(expected, html)
end

def test_form_builder_country_select
form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{})

Expand Down