aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations.rb
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2006-04-15 23:22:57 +0000
committerMarcel Molina <marcel@vernix.org>2006-04-15 23:22:57 +0000
commit37758cde13b2d15d11b2a9e407d9c269cd24715f (patch)
treebebe1a39f28fe38774e4fba1b7a9942dd69c905e /activerecord/lib/active_record/validations.rb
parent32f66f09dbcd44f2edbbacbd23d0a9d0aa60b917 (diff)
downloadrails-37758cde13b2d15d11b2a9e407d9c269cd24715f.tar.gz
rails-37758cde13b2d15d11b2a9e407d9c269cd24715f.tar.bz2
rails-37758cde13b2d15d11b2a9e407d9c269cd24715f.zip
DRY up and tweak style of the validation error object.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4213 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rwxr-xr-xactiverecord/lib/active_record/validations.rb21
1 files changed, 7 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 648b7f8f5a..d31b53b941 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -97,13 +97,9 @@ module ActiveRecord
# * Returns the error message, if one error is associated with the specified +attribute+.
# * Returns an array of error messages, if more than one error is associated with the specified +attribute+.
def on(attribute)
- if @errors[attribute.to_s].nil?
- nil
- elsif @errors[attribute.to_s].length == 1
- @errors[attribute.to_s].first
- else
- @errors[attribute.to_s]
- end
+ errors = @errors[attribute.to_s]
+ return nil if errors.nil?
+ errors.size == 1 ? errors.first : errors
end
alias :[] :on
@@ -139,13 +135,12 @@ module ActiveRecord
end
end
end
-
- return full_messages
+ full_messages
end
# Returns true if no errors have been added.
def empty?
- return @errors.empty?
+ @errors.empty?
end
# Removes all the errors that have been added.
@@ -156,9 +151,7 @@ module ActiveRecord
# Returns the total number of errors added. Two errors added to the same attribute will be counted as such
# with this as well.
def size
- error_count = 0
- @errors.each_value { |attribute| error_count += attribute.length }
- error_count
+ @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
end
alias_method :count, :size
@@ -290,7 +283,7 @@ module ActiveRecord
# method, proc or string should return or evaluate to a true or false value.
def validates_each(*attrs)
options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {}
- attrs = attrs.flatten
+ attrs = attrs.flatten
# Declare the validation.
send(validation_method(options[:on] || :save)) do |record|