Created
March 8, 2012 09:03
-
-
Save tillsc/1999795 to your computer and use it in GitHub Desktop.
all_errors helper method for DataMapper 1.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DataMapper | |
module MyHelpers | |
def all_errors(seen=[]) | |
return [] if seen.include?(self) | |
seen << self | |
res = self.errors | |
parent_relationships.each do |relationship| | |
association = relationship.get!(self) | |
if association.dirty? | |
err = association.all_errors(seen) | |
res[relationship.name] = err if err.any? | |
end | |
end | |
child_relationships.each do |relationship| | |
coll = relationship.get_collection(self) | |
if coll.loaded? # We don't think there are errors on unloaded relationships | |
coll.each do |association| | |
if association.dirty? | |
err = association.all_errors(seen) | |
(res[relationship.name] ||= []) << err if err.any? | |
end | |
end | |
end | |
end | |
res | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment