Last active
August 29, 2015 14:03
-
-
Save jkarmel/af87d384014a637ac4dd to your computer and use it in GitHub Desktop.
This file contains 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
class ActiveRecord::Base | |
descendants.each do |klass| | |
klass.singleton_class.instance_eval do | |
attr_accessor :touches, :touched_by | |
end | |
klass.touches = [] | |
klass.touched_by = [] | |
klass.reflections.values.each do |reflection| | |
if reflection.options && reflection.options[:touch] | |
toucher = klass | |
touched = Kernel.const_get(reflection.class_name) | |
toucher.touches << touched | |
touched.touched_by << toucher | |
end | |
end | |
klass.instance_eval do | |
def updates | |
unless self.touches | |
[] | |
else | |
direct = self.touches | |
indirect = direct.map &:updates | |
(direct + indirect).flatten.uniq | |
end | |
end | |
def updated_by | |
unless self.touched_by | |
[] | |
else | |
direct = self.touched_by | |
indirect = direct.map &:updated_by | |
(direct + indirect).flatten.uniq | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment