From f7abf0c9db61621b3f27061debd7983075cdca61 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Sun, 27 Jul 2008 16:34:20 -0500 Subject: error_message_on takes an options hash instead of ordered parameters [#704 state:resolved] Signed-off-by: Joshua Peek --- .../action_view/helpers/active_record_helper.rb | 46 +++++++++++++++------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'actionpack/lib/action_view/helpers/active_record_helper.rb') diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index 959d8a8563..ff70ce9aae 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -25,7 +25,7 @@ module ActionView # Returns an entire form with all needed input tags for a specified Active Record object. For example, if @post # has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then # - # form("post") + # form("post") # # would yield a form like the following (modulus formatting): # @@ -90,23 +90,41 @@ 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", "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") + # <%= error_message_on "post", "title", + # :prepend_text => "Title simply ", + # :append_text => " (or it won't work).", + # :css_class => "inputError" %> + def error_message_on(object, method, *args) + options = args.extract_options! + unless args.empty? + ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate' + + 'prepend_text, append_text, and css_class arguments', caller) + + 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) - else + content_tag("div", + "#{options[:prepend_text]}#{errors.is_a?(Array) ? errors.first : errors}#{options[:append_text]}", + :class => options[:css_class] + ) + else '' end end @@ -133,7 +151,7 @@ module ActionView # # To specify the display for one object, you simply provide its name as a parameter. # For example, for the @user model: - # + # # error_messages_for 'user' # # To specify more than one object, you simply list them; optionally, you can add an extra :object_name parameter, which @@ -157,7 +175,7 @@ module ActionView else objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact end - + count = objects.inject(0) {|sum, object| sum + object.errors.count } unless count.zero? html = {} @@ -174,7 +192,7 @@ module ActionView I18n.with_options :locale => options[:locale], :scope => [:active_record, :error] do |locale| header_message = if options.include?(:header_message) options[:header_message] - else + else object_name = options[:object_name].to_s.gsub('_', ' ') object_name = I18n.t(object_name, :default => object_name) locale.t :header_message, :count => count, :object_name => object_name @@ -193,7 +211,7 @@ module ActionView '' end end - + private def all_input_tags(record, record_name, options) input_block = options[:input_block] || default_input_block -- cgit v1.2.3