Created
November 30, 2010 09:39
-
-
Save airblade/721414 to your computer and use it in GitHub Desktop.
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
| def reify_has_manys(model, lookback) | |
| model.class.reflect_on_all_associations(:has_many).each do |assoc| | |
| # Get live children. | |
| children = model.send assoc.name | |
| # Ensure child models are using PaperTrail. | |
| if children.first.respond_to? :version_at | |
| # Handle updated children, and children created since the parent's version. | |
| children_then = [] | |
| children.each do |child| | |
| if (child_as_it_was = child.version_at(created_at - lookback.seconds)) | |
| child_as_it_was.attributes.each do |k,v| | |
| child.send :write_attribute, k.to_sym, v rescue nil | |
| end | |
| children_then << child | |
| end | |
| end | |
| children = children_then | |
| # Now handle children destroyed since the parent's version. | |
| destroyed_children = Version.all :conditions => [ | |
| "item_type = ? AND event = 'destroy' AND created_at > ?", | |
| assoc.name.to_s.classify, (created_at - lookback.seconds)] | |
| #children << destroyed_children.map(&:reify).select{ |child| child.send(foreign_key(model.class.name)) == model.id } | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment