Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:jnunemaker/mongomapper

  • Loading branch information...
commit 054c7cede6f7eec1f03c28a9d131660637beb4e7 2 parents 8087b83 + 6e87c79
@jnunemaker jnunemaker authored
Showing with 64 additions and 4 deletions.
  1. +9 −1 lib/mongo_mapper/plugins/keys.rb
  2. +55 −3 test/unit/test_validations.rb
View
10 lib/mongo_mapper/plugins/keys.rb
@@ -122,6 +122,14 @@ def create_validations_for(key)
validates_format_of(attribute, :with => key.options[:format])
end
+ if key.options[:in]
+ 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
@@ -289,4 +297,4 @@ def set(value)
end
end
end
-end
+end
View
58 test/unit/test_validations.rb
@@ -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
@@ -214,6 +227,19 @@ class ValidationsTest < Test::Unit::TestCase
doc.action = 'kick'
doc.should_not have_error_on(:action)
end
+
+ should "work with :in shortcut on key definition" do
+ @document.key :action, String, :in => %w(kick run)
+
+ doc = @document.new
+ doc.should have_error_on(:action, 'is not in the list')
+
+ doc.action = 'fart'
+ doc.should have_error_on(:action, 'is not in the list')
+
+ doc.action = 'kick'
+ doc.should_not have_error_on(:action)
+ end
should "not have error if allow nil is true and value is nil" do
@document.key :action, String
@@ -407,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
@@ -446,7 +485,20 @@ class ValidationsTest < Test::Unit::TestCase
doc.action = 'kick'
doc.should_not have_error_on(:action)
end
-
+
+ should "work with :in shortcut on key definition" do
+ @embedded_doc.key :action, String, :in => %w(kick run)
+
+ doc = @embedded_doc.new
+ doc.should have_error_on(:action, 'is not in the list')
+
+ doc.action = 'fart'
+ doc.should have_error_on(:action, 'is not in the list')
+
+ doc.action = 'kick'
+ doc.should_not have_error_on(:action)
+ end
+
should "not have error if allow nil is true and value is nil" do
@embedded_doc.key :action, String
@embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
@@ -489,4 +541,4 @@ def action_present
doc.should_not have_error_on(:action)
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.