Skip to content

Commit

Permalink
Makes form_helper use overriden model accessors
Browse files Browse the repository at this point in the history
[#3374 state:committed]
  • Loading branch information
spastorino committed Aug 1, 2010
1 parent f78de68 commit fb0bd8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -1005,9 +1005,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

Expand Down
17 changes: 17 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -4,6 +4,16 @@
class FormHelperTest < ActionView::TestCase
tests ActionView::Helpers::FormHelper

class Developer
def name_before_type_cast
"David"
end

def name
"Santiago"
end
end

def form_for(*)
@output_buffer = super
end
Expand Down Expand Up @@ -233,6 +243,13 @@ def test_text_field_with_custom_type
text_field("user", "email", :type => "email")
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_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" />',
Expand Down

8 comments on commit fb0bd8c

@tilsammans
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thank you.

@spastorino
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome this patch is around since Rails used track, i don't know why nobody did a patch yet

@masterkain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably broke searchlogic, already opened an issue here: http://github.com/binarylogic/searchlogic/issues/issue/111

@spastorino
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@masterkain nice!!!

@jduff
Copy link
Contributor

@jduff jduff commented on fb0bd8c Aug 26, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit causes issue with Time fields in MySQL. Instead of rendering the time (12:30) from the database it is now rendering the casted DateTime (Sat Jan 01 12:30:00 UTC 2000).

@spastorino
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jduff can you provide a test case? also it would be awesome if you can add a ticket on lighthouse and assign to me. Really appreciate your comment ;)

@spastorino
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jduff
Copy link
Contributor

@jduff jduff commented on fb0bd8c Aug 26, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, I've been looking at it with Chris who opened the ticket. It seems like before it was just getting the string from the database and now you get back a time object. I guess the best solution is to pass :value => time.to_s(:short) or whatever formatting you want to the input call. This way you're controlling the formatting and not just displaying whatever happens to be in the database. It was just a little surprising when moving between the RC versions.

Please sign in to comment.