Skip to content
This repository
Browse code

Fixed that overriding an attribute's accessor would be disregarded by…

… add_on_empty and add_on_boundary_breaking because they simply used the attributes[] hash instead of checking for @base.respond_to?(attr.to_s). [Marten]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@247 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 91eaeb280385d8423d8ec6f7097c9e1a19deb131 1 parent 78c6de9
David Heinemeier Hansson authored December 22, 2004
3  activerecord/CHANGELOG
... ...
@@ -1,5 +1,8 @@
1 1
 *SVN*
2 2
 
  3
+* Fixed that overriding an attribute's accessor would be disregarded by add_on_empty and add_on_boundary_breaking because they simply used 
  4
+  the attributes[] hash instead of checking for @base.respond_to?(attr.to_s). [Marten]
  5
+
3 6
 * Fixed that Base.table_name would expect a parameter when used in has_and_belongs_to_many joins [Anna Lissa Cruz]
4 7
 
5 8
 * Fixed that nested transactions now work by letting the outer most transaction have the responsibilty of starting and rolling back the transaction.
13  activerecord/lib/active_record/validations.rb
@@ -395,19 +395,24 @@ def add(attribute, msg = @@default_error_messages[:invalid])
395 395
 
396 396
     # Will add an error message to each of the attributes in +attributes+ that is empty (defined by <tt>attribute_present?</tt>).
397 397
     def add_on_empty(attributes, msg = @@default_error_messages[:empty])
398  
-      [attributes].flatten.each { |attr| add(attr, msg) unless @base.attribute_present?(attr.to_s) }
  398
+      for attr in [attributes].flatten
  399
+        value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
  400
+        is_empty = value.respond_to?("empty?") ? value.empty? : false
  401
+        add(attr, msg) unless !value.nil? && !is_empty
  402
+      end
399 403
     end
400 404
 
401 405
     # Will add an error message to each of the attributes in +attributes+ that has a length outside of the passed boundary +range+. 
402 406
     # If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.
403 407
     def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
404 408
       for attr in [attributes].flatten
405  
-        add(attr, too_short_msg % range.begin) if @base[attr.to_s] && @base.send(attr.to_s).length < range.begin
406  
-        add(attr, too_long_msg % range.end) if @base[attr.to_s] && @base.send(attr.to_s).length > range.end
  409
+        value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
  410
+        add(attr, too_short_msg % range.begin) if value && value.length < range.begin
  411
+        add(attr, too_long_msg % range.end) if value && value.length > range.end
407 412
       end
408 413
     end
409 414
 
410  
-    alias :add_on_boundary_breaking :add_on_boundary_breaking
  415
+    alias :add_on_boundry_breaking :add_on_boundary_breaking
411 416
 
412 417
     # Returns true if the specified +attribute+ has errors associated with it.
413 418
     def invalid?(attribute)

0 notes on commit 91eaeb2

Please sign in to comment.
Something went wrong with that request. Please try again.