aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-04-24 16:25:57 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-04-24 16:25:57 +0000
commitd8fc381ebc89e3934f266bfb0c304ca241d5ad93 (patch)
tree1edc8d19a488bf83a74ff5630eb6f0e860962e75
parent56412f4441f0954d3181eafbfb7b60b361e05d11 (diff)
downloadrails-d8fc381ebc89e3934f266bfb0c304ca241d5ad93.tar.gz
rails-d8fc381ebc89e3934f266bfb0c304ca241d5ad93.tar.bz2
rails-d8fc381ebc89e3934f266bfb0c304ca241d5ad93.zip
Added validates_exclusion_of as a negative of validates_inclusion_of
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1236 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG7
-rwxr-xr-xactiverecord/lib/active_record/validations.rb25
-rwxr-xr-xactiverecord/test/validations_test.rb7
3 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 0194feb3fb..b9b470d30a 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,3 +1,10 @@
+*SVN*
+
+* Added validates_exclusion_of as a negative of validates_inclusion_of
+
+* Optimized counting of has_many associations by setting the association to empty if the count is 0 so repeated calls doesn't trigger database calls
+
+
*1.10.1* (20th April, 2005)
* Fixed frivilous database queries being triggered with eager loading on empty associations and other things
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index fb8879ef2a..9af4010a51 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -11,6 +11,7 @@ module ActiveRecord
@@default_error_messages = {
:inclusion => "is not included in the list",
+ :exclusion => "is reserved",
:invalid => "is invalid",
:confirmation => "doesn't match confirmation",
:accepted => "must be accepted",
@@ -470,6 +471,30 @@ module ActiveRecord
end
end
+ # Validates that the value of the specified attribute is not in a particular enumerable object.
+ #
+ # class Person < ActiveRecord::Base
+ # validates_exclusion_of :username, :in => %w( admin superuser ), :message => "You don't belong here"
+ # validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60"
+ # end
+ #
+ # Configuration options:
+ # * <tt>in</tt> - An enumerable object of items that the value shouldn't be part of
+ # * <tt>message</tt> - Specifies a customer error message (default is: "is reserved")
+ # * <tt>allow_nil</tt> - If set to true, skips this validation if the attribute is null (default is: false)
+ def validates_exclusion_of(*attr_names)
+ configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], :on => :save }
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
+
+ enum = configuration[:in] || configuration[:within]
+
+ raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")
+
+ validates_each(attr_names, configuration) do |record, attr_name, value|
+ record.errors.add(attr_name, configuration[:message]) if enum.include?(value)
+ end
+ end
+
# Validates whether the associated object or objects are all themselves valid. Works with any kind of association.
#
# class Book < ActiveRecord::Base
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 585cad39bd..89df29375c 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -303,6 +303,13 @@ class ValidationsTest < Test::Unit::TestCase
assert Topic.create("title" => nil, "content" => "abc").valid?
end
+ def test_validates_exclusion_of
+ Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) )
+
+ assert Topic.create("title" => "something", "content" => "abc").valid?
+ assert !Topic.create("title" => "monkey", "content" => "abc").valid?
+ end
+
def test_validates_length_of_using_minimum
Topic.validates_length_of :title, :minimum => 5