2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-10-17 12:03:28 -04:00
|
|
|
class ActiveModelHelperTest < ActionView::TestCase
|
2009-07-19 12:28:15 -04:00
|
|
|
tests ActionView::Helpers::ActiveModelHelper
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-07-04 19:09:58 -04:00
|
|
|
silence_warnings do
|
2010-04-10 04:53:05 -04:00
|
|
|
class Post < Struct.new(:author_name, :body)
|
2009-07-21 01:51:57 -04:00
|
|
|
include ActiveModel::Conversion
|
2010-04-10 04:53:05 -04:00
|
|
|
include ActiveModel::Validations
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-04-10 04:53:05 -04:00
|
|
|
def persisted?
|
2009-03-07 13:55:12 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-04-26 19:09:08 -04:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2005-06-12 01:33:23 -04:00
|
|
|
|
2010-04-10 04:53:05 -04:00
|
|
|
@post = Post.new
|
|
|
|
@post.errors[:author_name] << "can't be empty"
|
|
|
|
@post.errors[:body] << "foo"
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-04-10 04:53:05 -04:00
|
|
|
@post.author_name = ""
|
|
|
|
@post.body = "Back to the hill and over it again!"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-06-12 21:34:16 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_text_area_with_errors
|
2005-09-20 03:54:55 -04:00
|
|
|
assert_dom_equal(
|
2010-04-24 17:57:49 -04:00
|
|
|
%(<div class="field_with_errors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div>),
|
2004-11-23 20:04:44 -05:00
|
|
|
text_area("post", "body")
|
2008-07-27 17:34:20 -04:00
|
|
|
)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_text_field_with_errors
|
2005-09-20 03:54:55 -04:00
|
|
|
assert_dom_equal(
|
2010-04-24 17:57:49 -04:00
|
|
|
%(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /></div>),
|
2004-11-23 20:04:44 -05:00
|
|
|
text_field("post", "author_name")
|
|
|
|
)
|
|
|
|
end
|
2007-06-12 21:34:16 -04:00
|
|
|
|
2010-06-24 16:36:54 -04:00
|
|
|
def test_hidden_field_does_not_render_errors
|
|
|
|
assert_dom_equal(
|
|
|
|
%(<input id="post_author_name" name="post[author_name]" type="hidden" value="" />),
|
|
|
|
hidden_field("post", "author_name")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-03-15 14:11:46 -04:00
|
|
|
def test_field_error_proc
|
|
|
|
old_proc = ActionView::Base.field_error_proc
|
|
|
|
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
|
2010-04-24 17:57:49 -04:00
|
|
|
%(<div class=\"field_with_errors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe
|
2010-03-15 14:11:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal(
|
2010-04-24 17:57:49 -04:00
|
|
|
%(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /> <span class="error">can't be empty</span></div>),
|
2010-03-15 14:11:46 -04:00
|
|
|
text_field("post", "author_name")
|
|
|
|
)
|
|
|
|
ensure
|
|
|
|
ActionView::Base.field_error_proc = old_proc if old_proc
|
|
|
|
end
|
2004-12-12 06:17:25 -05:00
|
|
|
end
|