From 1e6534678ffdd3f36388b3afffa9d6b2494efc0a Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Fri, 25 Jul 2008 16:37:47 +0200 Subject: [PATCH] Made error_message_on accept an options hash instead of ordered parameters. Also improved documentation a bit. --- .../action_view/helpers/active_record_helper.rb | 27 +++++++++++++++---- .../test/template/active_record_helper_test.rb | 4 +++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index 959d8a8..b795bfd 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -90,22 +90,37 @@ module ActionView end # Returns a string containing the error message attached to the +method+ on the +object+ if one exists. - # This error message is wrapped in a DIV tag, which can be extended to include a +prepend_text+ and/or +append_text+ - # (to properly explain the error), and a +css_class+ to style it accordingly. +object+ should either be the name of an instance variable or - # the actual object. As an example, let's say you have a model @post that has an error message on the +title+ attribute: + # This error message is wrapped in a DIV tag, which can be extended to include a :prepend_text and/or :append_text + # (to properly explain the error), and a :css_class to style it accordingly. +object+ should either be the name of an instance variable or + # the actual object. The method can be passed in either as a string or a symbol. + # As an example, let's say you have a model @post that has an error message on the +title+ attribute: # # <%= error_message_on "post", "title" %> # # =>
can't be empty
# - # <%= error_message_on @post, "title" %> + # <%= error_message_on @post, :title %> # # =>
can't be empty
# + # <%= error_message_on "post", "title", :prepend_text => "Title simply ", :append_text => " (or it won't work).", :css_class => "inputError" %> + # + # You can still use error_message_on with the old API that accepts the + # +prepend_text+ as its optional second, +append_text+ as its optional third and +css_class+ as + # its optional forth parameter: # <%= error_message_on "post", "title", "Title simply ", " (or it won't work).", "inputError" %> # # =>
Title simply can't be empty (or it won't work).
- def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError") + def error_message_on(object, method, *args) + options = args.extract_options! + unless args.empty? + options[:prepend_text] = args[0] || '' + options[:append_text] = args[1] || '' + options[:css_class] = args[2] || 'formError' + end + options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError') + if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && (errors = obj.errors.on(method)) - content_tag("div", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class) + content_tag("div", "#{options[:prepend_text]}#{errors.is_a?(Array) ? errors.first : errors}#{options[:append_text]}", + :class => options[:css_class]) else '' end diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb index dfc30e6..ff9c6eb 100644 --- a/actionpack/test/template/active_record_helper_test.rb +++ b/actionpack/test/template/active_record_helper_test.rb @@ -215,6 +215,10 @@ class ActiveRecordHelperTest < ActionView::TestCase def test_error_message_on_should_use_options assert_dom_equal "
beforecan't be emptyafter
", error_message_on(:post, :author_name, "before", "after", "differentError") end + + def test_error_message_on_with_options_hash + assert_dom_equal "
beforecan't be emptyafter
", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after') + end def test_error_messages_for_many_objects assert_dom_equal %(

2 errors prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", "user") -- 1.5.2.4