Permalink
Browse files

Use the FormBuilder to generate the inputs, not the template object

Using the template object the collection_check_boxes and
collection_raido_bottons methods do not work with local variables, only
with instance variables.

Closes #474
  • Loading branch information...
1 parent b459b1a commit 8f7fe5ef0ec86e1ab0a073c6b4fd3e5b7a792ec8 @rafaelfranca rafaelfranca committed Feb 26, 2012
Showing with 28 additions and 8 deletions.
  1. +7 −8 lib/simple_form/action_view_extensions/builder.rb
  2. +21 −0 test/action_view_extensions/builder_test.rb
@@ -5,10 +5,9 @@ module ActionViewExtensions
class BuilderBase #:nodoc:
attr_reader :object, :text, :value
- def initialize(template_object, object_name, method_name, object,
- sanitized_attribute_name, text, value, input_html_options)
- @template_object = template_object
- @object_name = object_name
+ def initialize(form_builder, method_name, object, sanitized_attribute_name, text,
+ value, input_html_options)
+ @form_builder = form_builder
@method_name = method_name
@object = object
@sanitized_attribute_name = sanitized_attribute_name
@@ -18,23 +17,23 @@ def initialize(template_object, object_name, method_name, object,
end
def label(label_html_options={}, &block)
- @template_object.label(@object_name, @sanitized_attribute_name, @text, label_html_options, &block)
+ @form_builder.label(@sanitized_attribute_name, @text, label_html_options, &block)
end
end
# Handles generating an instance of radio + label for collection_radio_buttons.
class RadioButtonBuilder < BuilderBase #:nodoc:
def radio_button(extra_html_options={})
html_options = extra_html_options.merge(@input_html_options)
- @template_object.radio_button(@object_name, @method_name, @value, html_options)
+ @form_builder.radio_button(@method_name, @value, html_options)
end
end
# Handles generating an instance of check box + label for collection_check_boxes.
class CheckBoxBuilder < BuilderBase #:nodoc:
def check_box(extra_html_options={})
html_options = extra_html_options.merge(@input_html_options)
- @template_object.check_box(@object_name, @method_name, html_options, @value, nil)
+ @form_builder.check_box(@method_name, html_options, @value, nil)
end
end
@@ -207,7 +206,7 @@ def simple_fields_for(*args, &block)
private
def instantiate_builder(builder_class, attribute, item, value, text, html_options)
- builder_class.new(@template, object_name, attribute, item,
+ builder_class.new(self, attribute, item,
sanitize_attribute_name(attribute, value), text, value, html_options)
end
@@ -48,6 +48,15 @@ def with_collection_check_boxes(object, attribute, collection, value_method, tex
assert_select 'label.collection_radio_buttons[for=user_name_199]', '$1.99'
end
+ test 'collection radio checks the correct value to local variables' do
+ user = User.new
+ user.active = false
+ with_collection_radio_buttons user, :active, [true, false], :to_s, :to_s
+
+ assert_select 'form input[type=radio][value=true]'
+ assert_select 'form input[type=radio][value=false][checked=checked]'
+ end
+
test 'collection radio accepts checked item' do
with_collection_radio_buttons @user, :active, [[1, true], [0, false]], :last, :first, :checked => true
@@ -276,6 +285,18 @@ def with_collection_check_boxes(object, attribute, collection, value_method, tex
assert_select 'label.collection_check_boxes[for=user_name_199]', '$1.99'
end
+ test 'collection check box checks the correct value to local variables' do
+ user = User.new
+ user.tag_ids = [1, 3]
+ collection = (1..3).map{|i| [i, "Tag #{i}"] }
+ with_collection_check_boxes user, :tag_ids, collection, :first, :last
+
+ assert_select 'form input[type=checkbox][value=1][checked=checked]'
+ assert_select 'form input[type=checkbox][value=3][checked=checked]'
+ assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
+ end
+
+
test 'collection check box accepts selected values as :checked option' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :checked => [1, 3]

0 comments on commit 8f7fe5e

Please sign in to comment.