aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-22 13:27:20 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-22 13:27:20 +0000
commit91eaeb280385d8423d8ec6f7097c9e1a19deb131 (patch)
treef0d480f04f6c49e5be285e6fb21ead12205b9df1
parent78c6de9daf7970ae40d50899ee0ceb67c0297a94 (diff)
downloadrails-91eaeb280385d8423d8ec6f7097c9e1a19deb131.tar.gz
rails-91eaeb280385d8423d8ec6f7097c9e1a19deb131.tar.bz2
rails-91eaeb280385d8423d8ec6f7097c9e1a19deb131.zip
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
-rw-r--r--activerecord/CHANGELOG3
-rwxr-xr-xactiverecord/lib/active_record/validations.rb13
2 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 5f0b4242d8..6c8d3c5211 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,8 @@
*SVN*
+* 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]
+
* Fixed that Base.table_name would expect a parameter when used in has_and_belongs_to_many joins [Anna Lissa Cruz]
* Fixed that nested transactions now work by letting the outer most transaction have the responsibilty of starting and rolling back the transaction.
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 1f23909676..dff2d8aeb2 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -395,19 +395,24 @@ module ActiveRecord
# Will add an error message to each of the attributes in +attributes+ that is empty (defined by <tt>attribute_present?</tt>).
def add_on_empty(attributes, msg = @@default_error_messages[:empty])
- [attributes].flatten.each { |attr| add(attr, msg) unless @base.attribute_present?(attr.to_s) }
+ for attr in [attributes].flatten
+ value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
+ is_empty = value.respond_to?("empty?") ? value.empty? : false
+ add(attr, msg) unless !value.nil? && !is_empty
+ end
end
# Will add an error message to each of the attributes in +attributes+ that has a length outside of the passed boundary +range+.
# If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.
def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
for attr in [attributes].flatten
- add(attr, too_short_msg % range.begin) if @base[attr.to_s] && @base.send(attr.to_s).length < range.begin
- add(attr, too_long_msg % range.end) if @base[attr.to_s] && @base.send(attr.to_s).length > range.end
+ value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
+ add(attr, too_short_msg % range.begin) if value && value.length < range.begin
+ add(attr, too_long_msg % range.end) if value && value.length > range.end
end
end
- alias :add_on_boundary_breaking :add_on_boundary_breaking
+ alias :add_on_boundry_breaking :add_on_boundary_breaking
# Returns true if the specified +attribute+ has errors associated with it.
def invalid?(attribute)