aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-22 23:25:45 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-22 23:25:45 +0000
commitd834b65b540665db81cca51fdc828d0c91e314a6 (patch)
tree2088904083b4ee6297947a911d5ef61349f65778 /activerecord
parent2ec81dcd28ece609bf837b7869696bba0af5507b (diff)
downloadrails-d834b65b540665db81cca51fdc828d0c91e314a6.tar.gz
rails-d834b65b540665db81cca51fdc828d0c91e314a6.tar.bz2
rails-d834b65b540665db81cca51fdc828d0c91e314a6.zip
Added allow_nil options to validates_inclusion_of so that validation is only triggered if the attribute is not nil [what-a-day]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@258 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/validations.rb8
-rwxr-xr-xactiverecord/test/validations_test.rb8
3 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 1de73028d2..c85ea7b8bf 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added allow_nil options to validates_inclusion_of so that validation is only triggered if the attribute is not nil [what-a-day]
+
* Added work-around for PostgreSQL and the problem of getting fixtures to be created from id 1 on each test case.
This only works for auto-incrementing primary keys called "id" for now #359 [Scott Baron]
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index dff2d8aeb2..1a4d6c190f 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -249,15 +249,21 @@ module ActiveRecord
# Configuration options:
# * <tt>in</tt> - An enumerable object of available items
# * <tt>message</tt> - Specifieds a customer error message (default is: "is not included in the list")
+ # * <tt>allows_nil</tt> - If set to true, skips this validation if the attribute is null (default is: false)
def validates_inclusion_of(*attr_names)
configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
enum = configuration[:in] || configuration[:within]
+ allow_nil = configuration[:allow_nil]
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?")
for attr_name in attr_names
- class_eval(%(#{validation_method(configuration[:on])} %{errors.add("#{attr_name}", "#{configuration[:message]}") unless (#{enum.inspect}).include?(#{attr_name}) }))
+ if allow_nil
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add("#{attr_name}", "#{configuration[:message]}") unless #{attr_name}.nil? or (#{enum.inspect}).include?(#{attr_name}) }))
+ else
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add("#{attr_name}", "#{configuration[:message]}") unless (#{enum.inspect}).include?(#{attr_name}) }))
+ end
end
end
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 867be0376d..f5735de59d 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -244,6 +244,14 @@ class ValidationsTest < Test::Unit::TestCase
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) }
end
+ def test_validates_inclusion_of_with_allow_nil
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :allow_nil=>true )
+
+ assert !Topic.create("title" => "a!", "content" => "abc").valid?
+ assert !Topic.create("title" => "", "content" => "abc").valid?
+ assert Topic.create("title" => nil, "content" => "abc").valid?
+ end
+
def test_validates_length_of_using_minimum
Topic.validates_length_of( :title, :minimum=>5 )
t = Topic.create("title" => "valid", "content" => "whatever")