public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
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
dhh (author)
Wed Dec 22 15:25:45 -0800 2004
commit  d834b65b540665db81cca51fdc828d0c91e314a6
tree    2088904083b4ee6297947a911d5ef61349f65778
parent  2ec81dcd28ece609bf837b7869696bba0af5507b
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Added allow_nil options to validates_inclusion_of so that validation is only triggered if the attribute is not nil [what-a-day]
0
+
0
 * Added work-around for PostgreSQL and the problem of getting fixtures to be created from id 1 on each test case.
0
   This only works for auto-incrementing primary keys called "id" for now #359 [Scott Baron]
0
 
...
249
250
251
 
252
253
254
255
 
256
257
258
259
260
 
 
 
 
 
261
262
263
...
249
250
251
252
253
254
255
256
257
258
259
260
261
 
262
263
264
265
266
267
268
269
0
@@ -249,15 +249,21 @@
0
       # Configuration options:
0
       # * <tt>in</tt> - An enumerable object of available items
0
       # * <tt>message</tt> - Specifieds a customer error message (default is: "is not included in the list")
0
+ # * <tt>allows_nil</tt> - If set to true, skips this validation if the attribute is null (default is: false)
0
       def validates_inclusion_of(*attr_names)
0
         configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save }
0
         configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
0
         enum = configuration[:in] || configuration[:within]
0
+ allow_nil = configuration[:allow_nil]
0
 
0
         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?")
0
 
0
         for attr_name in attr_names
0
- class_eval(%(#{validation_method(configuration[:on])} %{errors.add("#{attr_name}", "#{configuration[:message]}") unless (#{enum.inspect}).include?(#{attr_name}) }))
0
+ if allow_nil
0
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add("#{attr_name}", "#{configuration[:message]}") unless #{attr_name}.nil? or (#{enum.inspect}).include?(#{attr_name}) }))
0
+ else
0
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add("#{attr_name}", "#{configuration[:message]}") unless (#{enum.inspect}).include?(#{attr_name}) }))
0
+ end
0
         end
0
       end
0
       
...
244
245
246
 
 
 
 
 
 
 
 
247
248
249
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
0
@@ -244,6 +244,14 @@
0
     assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) }
0
   end
0
 
0
+ def test_validates_inclusion_of_with_allow_nil
0
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :allow_nil=>true )
0
+
0
+ assert !Topic.create("title" => "a!", "content" => "abc").valid?
0
+ assert !Topic.create("title" => "", "content" => "abc").valid?
0
+ assert Topic.create("title" => nil, "content" => "abc").valid?
0
+ end
0
+
0
   def test_validates_length_of_using_minimum
0
     Topic.validates_length_of( :title, :minimum=>5 )
0
     t = Topic.create("title" => "valid", "content" => "whatever")

Comments

    No one has commented yet.