Permalink
Browse files

Merge pull request #363 from statianzo/validate-association-context

validates_associated context limitations
  • Loading branch information...
2 parents ec6fb52 + d6b10f9 commit 15067e539e2b2330b3094a45cf105986fb3e4346 @bkeepers bkeepers committed Dec 12, 2011
Showing with 34 additions and 1 deletion.
  1. +1 −1 lib/mongo_mapper/plugins/validations.rb
  2. +33 −0 test/functional/test_validations.rb
@@ -68,7 +68,7 @@ def scope_conditions(instance)
class AssociatedValidator < ::ActiveModel::EachValidator
def validate_each(record, attribute, value)
- if !Array.wrap(value).all? { |c| c.nil? || c.valid? }
+ if !Array.wrap(value).all? { |c| c.nil? || c.valid?(options[:context]) }
record.errors.add(attribute, :invalid, :message => options[:message], :value => value)
end
end
@@ -350,6 +350,39 @@ def action_present
doc.children.build
doc.should have_error_on(:children, 'are invalid')
end
+
+ end
+
+ context "validating associated docs with custom context" do
+ setup do
+ @child_class = EDoc do
+ key :name
+
+ validates_length_of :name, :minimum => 5, :on => :custom_context
+ end
+
+ @root_class = Doc { }
+ @root_class.many :children, :class => @child_class
+ @root_class.validates_associated :children, :context => :custom_context
+ end
+
+ should "pass if there are no associated docs" do
+ doc = @root_class.new
+ doc.valid?(:custom_context).should be_true
+ end
+
+ should "pass if the associated doc is valid" do
+ doc = @root_class.new
+ doc.children.build(:name => 'George')
+ doc.valid?(:custom_context).should be_true
+ end
+
+ should "fail if the associated doc is invalid" do
+ doc = @root_class.new
+ doc.children.build(:name => 'Bob')
+ doc.valid?(:custom_context).should_not be_true
+ end
+
end
# context "validates uniqueness of with :unique shortcut" do
# should "work" do

0 comments on commit 15067e5

Please sign in to comment.