Permalink
Browse files

Make InstanceTagMethods#value_before_type_cast raise if the model don…

…'t respond to attr_before_type_cast or attr method

[#3374] [#5471 state:committed]
  • Loading branch information...
1 parent f98694b commit f95ba5c262bfd713f0a2fc656f8e645d3eea60f2 @spastorino spastorino committed Aug 27, 2010
Showing with 21 additions and 3 deletions.
  1. +8 −3 actionpack/lib/action_view/helpers/form_helper.rb
  2. +13 −0 actionpack/test/template/form_helper_test.rb
@@ -1006,9 +1006,14 @@ def value(object, method_name)
def value_before_type_cast(object, method_name)
unless object.nil?
- object.respond_to?(method_name) ?
- object.send(method_name) :
- object.send(method_name + "_before_type_cast")
+ if object.respond_to?(method_name)
+ object.send(method_name)
+ # FIXME: this is AR dependent
+ elsif object.respond_to?(method_name + "_before_type_cast")
+ object.send(method_name + "_before_type_cast")
+ else
+ raise NoMethodError, "Model #{object.class} does not respond to #{method_name}"
+ end
end
end
@@ -12,6 +12,8 @@ def name_before_type_cast
def name
"Santiago"
end
+
+ attr_writer :language
end
def form_for(*)
@@ -257,6 +259,17 @@ def test_text_field_from_a_user_defined_method
)
end
+ def test_text_field_on_a_model_with_undefined_attr_reader
+ @developer = Developer.new
+ @developer.language = 'ruby'
+ begin
+ text_field("developer", "language")
+ rescue NoMethodError => error
+ message = error.message
+ end
+ assert_equal "Model #{Developer} does not respond to language", message
+ end
+
def test_check_box
assert_dom_equal(
'<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',

0 comments on commit f95ba5c

Please sign in to comment.