Browse files

Makes form_helper use overriden model accessors backport

  • Loading branch information...
1 parent 27651c1 commit 8141f0894ecf869ee11f077e5b49e06669790632 @spastorino spastorino committed Aug 1, 2010
Showing with 20 additions and 3 deletions.
  1. +3 −3 actionpack/lib/action_view/helpers/form_helper.rb
  2. +17 −0 actionpack/test/template/form_helper_test.rb
View
6 actionpack/lib/action_view/helpers/form_helper.rb
@@ -877,9 +877,9 @@ def value(object, method_name)
def value_before_type_cast(object, method_name)
unless object.nil?
- object.respond_to?(method_name + "_before_type_cast") ?
- object.send(method_name + "_before_type_cast") :
- object.send(method_name)
+ object.respond_to?(method_name) ?
+ object.send(method_name) :
+ object.send(method_name + "_before_type_cast")
end
end
View
17 actionpack/test/template/form_helper_test.rb
@@ -91,6 +91,16 @@ def post_attributes=(attributes); end
class FormHelperTest < ActionView::TestCase
tests ActionView::Helpers::FormHelper
+ class Developer
+ def name_before_type_cast
+ "David"
+ end
+
+ def name
+ "Santiago"
+ end
+ end
+
def setup
super
@@ -256,6 +266,13 @@ def test_text_field_doesnt_change_param_values
assert_equal object_name, "post[]"
end
+ def test_text_field_from_a_user_defined_method
+ @developer = Developer.new
+ assert_dom_equal(
+ '<input id="developer_name" name="developer[name]" size="30" type="text" value="Santiago" />', text_field("developer", "name")
+ )
+ end
+
def test_hidden_field
assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
hidden_field("post", "title")

8 comments on commit 8141f08

@feldpost

It's fine to add this to Rails 3, but I don't think it's a good idea to backport this to Rails 2.3. It's a major change of behavior. In the past I have taken full advantage of the fact that facade methods are not used to generate form values. This change causes breakages all over, and I have a feeling it may affect a lot of people. After all, this was not a bug -- this particular behavior has been documented in books, blog posts etc. Can you please re-consider this change for 2.3?

@ralph

I agree with feldpost on this one. In addition to this, the method is called "value_BEFORE_type_cast", but it does deliver the value AFTER the type cast. This is misleading at best.

@sjain

+1.
When validation errors happen on form, the form needs to display the invalid value. This code change prevents us from showing user entered invalid value. A case in point is any invalid numeric value gets turned into 0.0 (after the type cast).

@NZKoz
Ruby on Rails member

This has now been reverted in 2-3-stable

@feldpost

Thanks, Michael.

@ralph

Cool, thank you!

@tomstuart

Why hasn't this been reverted in Rails 3 too? Is the failed-validation behaviour not a problem there?

@spastorino
Ruby on Rails member

After discussing this we agreed to revert this behavior everywhere. So i'm reverting this on Rails 3 too

Please sign in to comment.