Permalink
Browse files

Add key validation shortcut :not_in.

  • Loading branch information...
1 parent 15b01ad commit 6e87c793d982437527fc69ef665db682ab57acde @chanks chanks committed with jnunemaker Jan 23, 2010
Showing with 31 additions and 1 deletion.
  1. +4 −0 lib/mongo_mapper/plugins/keys.rb
  2. +27 −1 test/unit/test_validations.rb
@@ -126,6 +126,10 @@ def create_validations_for(key)
validates_inclusion_of(attribute, :within => key.options[:in])
end
+ if key.options[:not_in]
+ validates_exclusion_of(attribute, :within => key.options[:not_in])
+ end
+
if key.options[:length]
length_options = case key.options[:length]
when Integer
@@ -175,7 +175,20 @@ class ValidationsTest < Test::Unit::TestCase
doc.action = 'kick'
doc.should have_error_on(:action, 'is reserved')
end
-
+
+ should "work with :not_in shortcut on key definition" do
+ @document.key :action, String, :not_in => %w(kick run)
+
+ doc = @document.new
+ doc.should_not have_error_on(:action)
+
+ doc.action = 'fart'
+ doc.should_not have_error_on(:action)
+
+ doc.action = 'kick'
+ doc.should have_error_on(:action, 'is reserved')
+ end
+
should "not have error if allow nil is true and value is nil" do
@document.key :action, String
@document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
@@ -420,6 +433,19 @@ class ValidationsTest < Test::Unit::TestCase
doc.action = 'kick'
doc.should have_error_on(:action, 'is reserved')
end
+
+ should "work with :not_in shortcut on key definition" do
+ @embedded_doc.key :action, String, :not_in => %w(kick run)
+
+ doc = @embedded_doc.new
+ doc.should_not have_error_on(:action)
+
+ doc.action = 'fart'
+ doc.should_not have_error_on(:action)
+
+ doc.action = 'kick'
+ doc.should have_error_on(:action, 'is reserved')
+ end
should "not have error if allow nil is true and value is nil" do
@embedded_doc.key :action, String

0 comments on commit 6e87c79

Please sign in to comment.