Skip to content

Commit

Permalink
Support the old country_select API for compatibility with various lib…
Browse files Browse the repository at this point in the history
…raries
  • Loading branch information
jim committed Aug 15, 2012
1 parent f8da26f commit 4fabbfe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/carmen/rails/action_view/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,24 @@ def subregion_select(object, method, parent_region_or_code, options={}, html_opt
#
# 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.
def country_select(object, method, options={}, html_options={})
tag = InstanceTag.new(object, method, self, options.delete(:object))
def country_select(object, method, *args)

# These contortions are to provide API-compatibility
priority_countries = args.shift if args.first.is_a?(Array)
options, html_options = args

options ||= {}
options[:priority] = priority_countries if priority_countries

html_options ||= {}

tag = InstanceTag.new(object, method, self)
tag.to_region_select_tag(Carmen::World.instance, options, html_options)
end

Expand Down Expand Up @@ -157,7 +172,7 @@ def determine_parent(parent_region_or_code)
end

class InstanceTag
def to_region_select_tag(parent_region, options, html_options)
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] || []
Expand All @@ -173,19 +188,17 @@ class FormBuilder
# web form.
#
# See `FormOptionsHelper::country_select` for more information.
def country_select(method, options = {}, html_options = {})
@template.country_select(@object_name, method,
options.merge(:object => @object), html_options)
def country_select(method, *args)
@template.country_select(@object_name, method, *args)
end

# Generate select and subregion option tags with the provided name. A
# common use of this would be to allow users to select a state subregion within
# a given country.
#
# See `FormOptionsHelper::subregion_select` for more information.
def subregion_select(method, parent_region_or_code, options={}, html_options={})
@template.subregion_select(@object_name, method, parent_region_or_code,
options.merge(:object => @object), html_options)
def subregion_select(method, parent_region_or_code, *args)
@template.subregion_select(@object_name, method, parent_region_or_code, *args)
end
end

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

def test_priority_country_select_deprecated_api
html = country_select(@object, :country_code, ['ES'], {})
expected = <<-HTML
<select id="object_country_code" name="object[country_code]">
<option value="ES">Eastasia</option>
<option disabled>-------------</option>
<option value="ES">Eastasia</option>
<option value="EU">Eurasia</option>
<option value="OC">Oceania</option>
</select>
HTML

assert_equal_markup(expected, html)
end

def test_basic_subregion_select
oceania = Carmen::Country.coded('OC')
expected = <<-HTML
Expand Down Expand Up @@ -165,6 +180,38 @@ def test_region_options_for_select_with_array_of_regions_and_priority
assert_equal_markup(expected, html)
end

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

html = form.country_select('attribute_name')
expected = <<-HTML
<select id="object_attribute_name" name="object[attribute_name]">
<option value="ES">Eastasia</option>
<option value="EU">Eurasia</option>
<option value="OC">Oceania</option>
</select>
HTML

assert_equal_markup(expected, html)
end

def test_form_builder_country_select_deprecated_api
form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, ->{})

html = form.country_select('attribute_name', ['ES'])
expected = <<-HTML
<select id="object_attribute_name" name="object[attribute_name]">
<option value="ES">Eastasia</option>
<option disabled>-------------</option>
<option value="ES">Eastasia</option>
<option value="EU">Eurasia</option>
<option value="OC">Oceania</option>
</select>
HTML

assert_equal_markup(expected, html)
end

def method_missing(method, *args)
fail "method_missing #{method}"
end
Expand Down

0 comments on commit 4fabbfe

Please sign in to comment.