public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
error_message_on takes an options hash instead of ordered parameters [#704 
state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
clemens (author)
Sun Jul 27 14:34:20 -0700 2008
josh (committer)
Sun Jul 27 14:34:20 -0700 2008
commit  f7abf0c9db61621b3f27061debd7983075cdca61
tree    8bf3fb1f3c977e9cb5fc8afce03a0eaf79042ae7
parent  490178c93008c6fca20e3e2d6302a28d86aab94d
...
25
26
27
28
 
29
30
31
...
90
91
92
93
94
95
 
 
 
 
 
96
97
98
99
100
 
101
102
103
104
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
107
108
109
 
 
 
 
 
110
111
112
...
133
134
135
136
 
137
138
139
...
157
158
159
160
 
161
162
163
...
174
175
176
177
 
178
179
180
...
193
194
195
196
 
197
198
199
...
25
26
27
 
28
29
30
31
...
90
91
92
 
 
 
93
94
95
96
97
98
99
100
101
 
102
103
104
 
 
 
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 
 
123
124
125
126
127
128
129
130
...
151
152
153
 
154
155
156
157
...
175
176
177
 
178
179
180
181
...
192
193
194
 
195
196
197
198
...
211
212
213
 
214
215
216
217
0
@@ -25,7 +25,7 @@ module ActionView
0
       # Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt>
0
       # has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then
0
       #
0
-      #   form("post") 
0
+      #   form("post")
0
       #
0
       # would yield a form like the following (modulus formatting):
0
       #
0
@@ -90,23 +90,41 @@ module ActionView
0
       end
0
 
0
       # Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
0
-      # This error message is wrapped in a <tt>DIV</tt> tag, which can be extended to include a +prepend_text+ and/or +append_text+
0
-      # (to properly explain the error), and a +css_class+ to style it accordingly. +object+ should either be the name of an instance variable or
0
-      # the actual object. As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
0
+      # This error message is wrapped in a <tt>DIV</tt> tag, which can be extended to include a <tt>:prepend_text</tt>
0
+      # and/or <tt>:append_text</tt> (to properly explain the error), and a <tt>:css_class</tt> to style it
0
+      # accordingly. +object+ should either be the name of an instance variable or the actual object. The method can be
0
+      # passed in either as a string or a symbol.
0
+      # As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
0
       #
0
       #   <%= error_message_on "post", "title" %>
0
       #   # => <div class="formError">can't be empty</div>
0
       #
0
-      #   <%= error_message_on @post, "title" %>
0
+      #   <%= error_message_on @post, :title %>
0
       #   # => <div class="formError">can't be empty</div>
0
       #
0
-      #   <%= error_message_on "post", "title", "Title simply ", " (or it won't work).", "inputError" %>
0
-      #   # => <div class="inputError">Title simply can't be empty (or it won't work).</div>
0
-      def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
0
+      #   <%= error_message_on "post", "title",
0
+      #       :prepend_text => "Title simply ",
0
+      #       :append_text => " (or it won't work).",
0
+      #       :css_class => "inputError" %>
0
+      def error_message_on(object, method, *args)
0
+        options = args.extract_options!
0
+        unless args.empty?
0
+          ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate' +
0
+            'prepend_text, append_text, and css_class arguments', caller)
0
+
0
+          options[:prepend_text] = args[0] || ''
0
+          options[:append_text] = args[1] || ''
0
+          options[:css_class] = args[2] || 'formError'
0
+        end
0
+        options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError')
0
+
0
         if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
0
           (errors = obj.errors.on(method))
0
-          content_tag("div", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class)
0
-        else 
0
+          content_tag("div",
0
+            "#{options[:prepend_text]}#{errors.is_a?(Array) ? errors.first : errors}#{options[:append_text]}",
0
+            :class => options[:css_class]
0
+          )
0
+        else
0
           ''
0
         end
0
       end
0
@@ -133,7 +151,7 @@ module ActionView
0
       #
0
       # To specify the display for one object, you simply provide its name as a parameter.
0
       # For example, for the <tt>@user</tt> model:
0
-      # 
0
+      #
0
       #   error_messages_for 'user'
0
       #
0
       # To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
0
@@ -157,7 +175,7 @@ module ActionView
0
         else
0
           objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
0
         end
0
-        
0
+
0
         count  = objects.inject(0) {|sum, object| sum + object.errors.count }
0
         unless count.zero?
0
           html = {}
0
@@ -174,7 +192,7 @@ module ActionView
0
           I18n.with_options :locale => options[:locale], :scope => [:active_record, :error] do |locale|
0
             header_message = if options.include?(:header_message)
0
               options[:header_message]
0
-            else 
0
+            else
0
               object_name = options[:object_name].to_s.gsub('_', ' ')
0
               object_name = I18n.t(object_name, :default => object_name)
0
               locale.t :header_message, :count => count, :object_name => object_name
0
@@ -193,7 +211,7 @@ module ActionView
0
           ''
0
         end
0
       end
0
-      
0
+
0
       private
0
         def all_input_tags(record, record_name, options)
0
           input_block = options[:input_block] || default_input_block
...
782
783
784
785
786
 
 
787
788
789
...
782
783
784
 
 
785
786
787
788
789
0
@@ -782,8 +782,8 @@ module ActionView
0
         @template.radio_button(@object_name, method, tag_value, objectify_options(options))
0
       end
0
 
0
-      def error_message_on(method, prepend_text = "", append_text = "", css_class = "formError")
0
-        @template.error_message_on(@object, method, prepend_text, append_text, css_class)
0
+      def error_message_on(method, *args)
0
+        @template.error_message_on(@object, method, *args)
0
       end
0
 
0
       def error_messages(options = {})
...
10
11
12
13
 
14
15
16
17
18
 
19
20
21
22
23
 
24
25
26
...
33
34
35
36
37
 
 
38
39
40
41
 
42
43
44
...
58
59
60
61
 
62
63
64
65
66
 
 
67
68
69
70
 
71
72
73
...
81
82
83
84
 
85
86
87
...
92
93
94
95
 
96
97
98
...
111
112
113
114
 
115
116
117
...
140
141
142
143
 
144
145
146
...
150
151
152
153
 
154
155
156
...
211
212
213
214
215
216
 
 
 
217
218
219
...
224
225
226
227
 
228
229
230
 
231
232
233
...
242
243
244
245
 
246
247
248
...
251
252
253
254
 
255
256
257
 
258
259
260
261
 
262
263
264
...
10
11
12
 
13
14
15
16
17
 
18
19
20
21
22
 
23
24
25
26
...
33
34
35
 
 
36
37
38
39
40
 
41
42
43
44
...
58
59
60
 
61
62
63
64
 
 
65
66
67
68
69
 
70
71
72
73
...
81
82
83
 
84
85
86
87
...
92
93
94
 
95
96
97
98
...
111
112
113
 
114
115
116
117
...
140
141
142
 
143
144
145
146
...
150
151
152
 
153
154
155
156
...
211
212
213
 
 
 
214
215
216
217
218
219
...
224
225
226
 
227
228
229
 
230
231
232
233
...
242
243
244
 
245
246
247
248
...
251
252
253
 
254
255
256
 
257
258
259
260
 
261
262
263
264
0
@@ -10,17 +10,17 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
       alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
0
       alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
0
     end
0
-    
0
+
0
     User = Struct.new("User", :email)
0
     User.class_eval do
0
       alias_method :email_before_type_cast, :email unless respond_to?(:email_before_type_cast)
0
     end
0
-    
0
+
0
     Column = Struct.new("Column", :type, :name, :human_name)
0
   end
0
 
0
   def setup_post
0
-    @post = Post.new    
0
+    @post = Post.new
0
     def @post.errors
0
       Class.new {
0
         def on(field)
0
@@ -33,12 +33,12 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
             false
0
           end
0
         end
0
-        def empty?() false end 
0
-        def count() 1 end 
0
+        def empty?() false end
0
+        def count() 1 end
0
         def full_messages() [ "Author name can't be empty" ] end
0
       }.new
0
     end
0
-    
0
+
0
     def @post.new_record?() true end
0
     def @post.to_param() nil end
0
 
0
@@ -58,16 +58,16 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
   end
0
 
0
   def setup_user
0
-    @user = User.new    
0
+    @user = User.new
0
     def @user.errors
0
       Class.new {
0
         def on(field) field == "email" end
0
-        def empty?() false end 
0
-        def count() 1 end 
0
+        def empty?() false end
0
+        def count() 1 end
0
         def full_messages() [ "User email can't be empty" ] end
0
       }.new
0
     end
0
-    
0
+
0
     def @user.new_record?() true end
0
     def @user.to_param() nil end
0
 
0
@@ -81,7 +81,7 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
 
0
     @user.email = ""
0
   end
0
-  
0
+
0
   def protect_against_forgery?
0
     @protect_against_forgery ? true : false
0
   end
0
@@ -92,7 +92,7 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
     setup_user
0
 
0
     @response = ActionController::TestResponse.new
0
-    
0
+
0
     @controller = Object.new
0
     def @controller.url_for(options)
0
       options = options.symbolize_keys
0
@@ -111,7 +111,7 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
     assert_dom_equal(
0
       %(<div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div>),
0
       text_area("post", "body")
0
-    )    
0
+    )
0
   end
0
 
0
   def test_text_field_with_errors
0
@@ -140,7 +140,7 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
       form("post")
0
     )
0
   end
0
-  
0
+
0
   def test_form_with_protect_against_forgery
0
     @protect_against_forgery = true
0
     @request_forgery_protection_token = 'authenticity_token'
0
@@ -150,7 +150,7 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
       form("post")
0
     )
0
   end
0
-  
0
+
0
   def test_form_with_method_option
0
     assert_dom_equal(
0
       %(<form action="create" method="get"><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>),
0
@@ -211,9 +211,9 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
     other_post = @post
0
     assert_dom_equal "<div class=\"formError\">can't be empty</div>", error_message_on(other_post, :author_name)
0
   end
0
-  
0
-  def test_error_message_on_should_use_options
0
-    assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, "before", "after", "differentError")
0
+
0
+  def test_error_message_on_with_options_hash
0
+    assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
0
   end
0
 
0
   def test_error_messages_for_many_objects
0
@@ -224,10 +224,10 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
 
0
     # add the default to put post back in the title
0
     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")
0
-    
0
+
0
     # symbols work as well
0
     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)
0
-    
0
+
0
     # any default works too
0
     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")
0
 
0
@@ -242,7 +242,7 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
     message = "Please fix the following fields and resubmit:"
0
     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>#{header_message}</h2><p>#{message}</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, :header_message => header_message, :message => message)
0
   end
0
-  
0
+
0
   def test_error_messages_for_non_instance_variable
0
     actual_user = @user
0
     actual_post = @post
0
@@ -251,14 +251,14 @@ class ActiveRecordHelperTest < ActionView::TestCase
0
 
0
   #explicitly set object
0
     assert_dom_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", :object => actual_post)
0
-      
0
+
0
   #multiple objects
0
     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", :object => [actual_user, actual_post])
0
-    
0
+
0
   #nil object
0
     assert_equal '', error_messages_for('user', :object => nil)
0
   end
0
-  
0
+
0
   def test_form_with_string_multipart
0
     assert_dom_equal(
0
       %(<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>),

Comments