Skip to content

Commit

Permalink
Merge pull request mongomapper#363 from statianzo/validate-associatio…
Browse files Browse the repository at this point in the history
…n-context

validates_associated context limitations
  • Loading branch information
bkeepers committed Dec 12, 2011
2 parents ec6fb52 + d6b10f9 commit 15067e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/validations.rb
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/functional/test_validations.rb
Expand Up @@ -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
Expand Down

0 comments on commit 15067e5

Please sign in to comment.