aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/active_record_helper_test.rb
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2006-04-26 23:09:08 +0000
committerMarcel Molina <marcel@vernix.org>2006-04-26 23:09:08 +0000
commit43ee8ab6e2aa1efeb848885ed51f14709990cc08 (patch)
tree1cff111083576260cbdecb19c2b84eb9a3dffeed /actionpack/test/template/active_record_helper_test.rb
parent8ee378f3c46c164cf8d7559bbb6a65ce1bb786fb (diff)
downloadrails-43ee8ab6e2aa1efeb848885ed51f14709990cc08.tar.gz
rails-43ee8ab6e2aa1efeb848885ed51f14709990cc08.tar.bz2
rails-43ee8ab6e2aa1efeb848885ed51f14709990cc08.zip
Allow error_messages_for to report errors for multiple objects, as well as support for customizing the name of the object in the error summary header. Closes #4186. [andrew@redlinesoftware.com, Marcel Molina Jr.]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4287 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test/template/active_record_helper_test.rb')
-rw-r--r--actionpack/test/template/active_record_helper_test.rb52
1 files changed, 51 insertions, 1 deletions
diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb
index e153e630ab..ffaae99e28 100644
--- a/actionpack/test/template/active_record_helper_test.rb
+++ b/actionpack/test/template/active_record_helper_test.rb
@@ -22,10 +22,16 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
end
+
+ User = Struct.new("User", :email)
+ User.class_eval do
+ alias_method :email_before_type_cast, :email unless respond_to?(:email_before_type_cast)
+ end
+
Column = Struct.new("Column", :type, :name, :human_name)
end
- def setup
+ def setup_post
@post = Post.new
def @post.errors
Class.new {
@@ -50,6 +56,34 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
@post.body = "Back to the hill and over it again!"
@post.secret = 1
@post.written_on = Date.new(2004, 6, 15)
+ end
+
+ def setup_user
+ @user = User.new
+ def @user.errors
+ Class.new {
+ def on(field) field == "email" end
+ def empty?() false end
+ def count() 1 end
+ def full_messages() [ "User email can't be empty" ] end
+ }.new
+ end
+
+ def @user.new_record?() true end
+ def @user.to_param() nil end
+
+ def @user.column_for_attribute(attr_name)
+ User.content_columns.select { |column| column.name == attr_name }.first
+ end
+
+ def User.content_columns() [ Column.new(:string, "email", "Email") ] end
+
+ @user.email = ""
+ end
+
+ def setup
+ setup_post
+ setup_user
@controller = Object.new
def @controller.url_for(options, *parameters_for_method_reference)
@@ -124,6 +158,22 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
assert_equal "", error_messages_for("notthere")
end
+ def test_error_messages_for_many_objects
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user")
+
+ # reverse the order, error order changes and so does the title
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post")
+
+ # add the default to put post back in the title
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post", :object_name => "post")
+
+ # symbols work as well
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => :post)
+
+ # any default works too
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this monkey from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "monkey")
+ end
+
def test_form_with_string_multipart
assert_dom_equal(
%(<form action="create" enctype="multipart/form-data" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),