aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-10 16:02:11 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-10 16:02:11 +0000
commitaaf9a45ca9ce5ceb67b7d4ae0dff350389b72c69 (patch)
treec2c443eae085e4fe528cd1223ed15147706dc7c7 /activerecord/lib/active_record/validations.rb
parent0b92b7de2f752bee6c4c950ac9090e5bce3b63bf (diff)
downloadrails-aaf9a45ca9ce5ceb67b7d4ae0dff350389b72c69.tar.gz
rails-aaf9a45ca9ce5ceb67b7d4ae0dff350389b72c69.tar.bz2
rails-aaf9a45ca9ce5ceb67b7d4ae0dff350389b72c69.zip
Added Base.validate_uniqueness thatv alidates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user can be named "davidhh".
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@108 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rwxr-xr-xactiverecord/lib/active_record/validations.rb24
1 files changed, 21 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 17810318b7..b2e7b1e99b 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -74,7 +74,6 @@ module ActiveRecord
# situations.
def validate_confirmation(*attr_names)
error_message = attr_names.last.is_a?(String) ? attr_names.pop : "doesn't match confirmation"
-
validation_method = block_given? ? yield : "validate"
for attr_name in attr_names
@@ -111,7 +110,6 @@ module ActiveRecord
# NOTE: The agreement is considered valid if it's set to the string "1". This makes it easy to relate it to an HTML checkbox.
def validate_acceptance(*attr_names)
error_message = attr_names.last.is_a?(String) ? attr_names.pop : "must be accepted"
-
validation_method = block_given? ? yield : "validate"
for attr_name in attr_names
@@ -132,7 +130,6 @@ module ActiveRecord
def validate_presence(*attr_names)
error_message = attr_names.last.is_a?(String) ? attr_names.pop : "can't be empty"
-
validation_method = block_given? ? yield : "validate"
for attr_name in attr_names
@@ -149,6 +146,27 @@ module ActiveRecord
def validate_presence_on_update(*attr_names)
validate_presence(*attr_names) { "validate_on_update" }
end
+
+ # Validates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user
+ # can be named "davidhh".
+ #
+ # Model:
+ # class Person < ActiveRecord::Base
+ # validate_uniqueness :user_name
+ # end
+ #
+ # View:
+ # <%= text_field "person", "user_name" %>
+ #
+ # When the record is created, a check is performed to make sure that no record exist in the database with the given value for the specified
+ # attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.
+ def validate_uniqueness(*attr_names)
+ error_message = attr_names.last.is_a?(String) ? attr_names.pop : "has already been taken"
+
+ for attr_name in attr_names
+ class_eval(%(validate %{errors.add("#{attr_name}", "#{error_message}") if self.class.find_first(new_record? ? ["#{attr_name} = ?", #{attr_name}] : ["#{attr_name} = ? AND id <> ?", #{attr_name}, id])}))
+ end
+ end
end
# The validation process on save can be skipped by passing false. The regular Base#save method is