1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added options to tailor header tag, div id, and div class on ActiveRecordHelper#error_messages_for [josh]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@41 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2004-12-01 13:59:16 +00:00
parent 0daa29ece2
commit 005371e16c
3 changed files with 37 additions and 8 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Added options to tailor header tag, div id, and div class on ActiveRecordHelper#error_messages_for [josh]
* Added graceful handling of non-alphanumeric names and misplaced brackets in input parameters [bitsweat] * Added graceful handling of non-alphanumeric names and misplaced brackets in input parameters [bitsweat]
* Added a new container for cookies that makes them more intuative to use. The old methods of cookie and @cookies have been deprecated. * Added a new container for cookies that makes them more intuative to use. The old methods of cookie and @cookies have been deprecated.

View file

@ -76,14 +76,24 @@ module ActionView
end end
end end
def error_messages_for(object_name) # Returns a string with a div containing all the error messages for the object located as an instance variable by the name
object = instance_eval("@#{object_name}") # of <tt>object_name</tt>. This div can be tailored by the following options:
#
# ::header_tag: Used for the header of the error div (default: h2)
# ::id: The id of the error div (default: errorExplanation)
# ::class: The class of the error div (default: errorExplanation)
def error_messages_for(object_name, options={})
object = instance_eval "@#{object_name}"
unless object.errors.empty? unless object.errors.empty?
"<div id=\"errorExplanation\">" + content_tag("div",
"<h2>#{object.errors.count} error#{"s" unless object.errors.count == 1} prohibited this #{object_name.gsub("_", " ")} from being saved</h2>" + content_tag(
"<p>There were problems with the following fields (marked in red below):</p>" + options[:header_tag] || "h2",
"<ul>#{object.errors.full_messages.collect { |msg| "<li>#{msg}</li>"}}</ul>" + "#{pluralize(object.errors.count, "error")} prohibited this #{object_name.gsub("_", " ")} from being saved"
"</div>" ) +
content_tag("p", "There were problems with the following fields:") +
content_tag("ul", object.errors.full_messages.collect { |msg| content_tag("li", msg) }),
"id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
)
end end
end end

View file

@ -1,18 +1,30 @@
require 'test/unit' require 'test/unit'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
# require File.dirname(__FILE__) + '/../../lib/action_view/helpers/active_record_helper' # require File.dirname(__FILE__) + '/../../lib/action_view/helpers/active_record_helper'
class ActiveRecordHelperTest < Test::Unit::TestCase class ActiveRecordHelperTest < Test::Unit::TestCase
include ActionView::Helpers::FormHelper include ActionView::Helpers::FormHelper
include ActionView::Helpers::ActiveRecordHelper include ActionView::Helpers::ActiveRecordHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::TagHelper
Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on)
Column = Struct.new("Column", :type, :name, :human_name) Column = Struct.new("Column", :type, :name, :human_name)
def setup def setup
@post = Post.new @post = Post.new
def @post.errors() Class.new{ def on(field) field == "author_name" || field == "body" end }.new end def @post.errors
Class.new {
def on(field) field == "author_name" || field == "body" end
def empty?() false end
def count() 1 end
def full_messages() [ "Author name can't be empty" ] end
}.new
end
def @post.new_record?() true end def @post.new_record?() true end
def @post.column_for_attribute(attr_name) def @post.column_for_attribute(attr_name)
@ -73,4 +85,9 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
form("post") form("post")
) )
end end
def test_error_for_block
assert_equal "<div class=\"errorExplanation\" id=\"errorExplanation\"><h2>1 error 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></ul></div>", error_messages_for("post")
assert_equal "<div class=\"errorDeathByClass\" id=\"errorDeathById\"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>", error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1")
end
end end