Skip to content

Instantly share code, notes, and snippets.

@jkarmel
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkarmel/af87d384014a637ac4dd to your computer and use it in GitHub Desktop.
Save jkarmel/af87d384014a637ac4dd to your computer and use it in GitHub Desktop.
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