Skip to content

Commit

Permalink
Included embedded documents validations
Browse files Browse the repository at this point in the history
  • Loading branch information
warlley committed Mar 25, 2010
1 parent 17b97db commit 0ce426b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/mongo_mapper/document.rb
Expand Up @@ -26,10 +26,12 @@ def self.included(model)
plugin Plugins::Userstamps
plugin Plugins::Validations
plugin Plugins::Callbacks # for now callbacks needs to be after validations

plugin Plugins::NestedAttributes

extend Plugins::Validations::DocumentMacros

validate :embedded_document_validity
end

super
Expand Down Expand Up @@ -356,6 +358,20 @@ def save_to_collection(options={})
@new = false
collection.save(to_mongo, :safe => safe)
end

def embedded_document_validity
all_embedded_documents.each do |doc|
unless doc.valid?
class_name = doc.class.name.humanize
doc.errors.errors.each do |attribute, error_messages|
error_messages.each do |error|
errors.add("#{class_name} #{attribute}", error)
end
end
end
end
end

end
end # Document
end # MongoMapper

2 comments on commit 0ce426b

@jnunemaker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use validates_associated with association name to include embedded in validations. validates_associated :fields for example.

@warlley
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, i will remove this.

Please sign in to comment.