module Mongoid::Touchable
def define_relation_touch_method(name, association)
-
(Symbol)- The method name.
Parameters:
-
association(Association) -- The association metadata. -
name(Symbol) -- The name of the association.
Other tags:
- Example: Define the touch association. -
Other tags:
- Api: - private
def define_relation_touch_method(name, association) relation_classes = if association.polymorphic? association.send(:inverse_association_classes) else [ association.relation_class ] end relation_classes.each { |c| c.send(:include, InstanceMethods) } method_name = "touch_#{name}_after_create_or_destroy" association.inverse_class.class_eval do define_method(method_name) do without_autobuild do if relation = __send__(name) if association.touch_field # Note that this looks up touch_field at runtime, rather than # at method definition time. relation.touch association.touch_field else relation.touch end end end end end method_name.to_sym end
def define_touchable!(association)
-
(Class)- The model class.
Parameters:
-
association(Association) -- The association metadata.
Other tags:
- Example: Add the touchable. -
def define_touchable!(association) name = association.name method_name = define_relation_touch_method(name, association) association.inverse_class.tap do |klass| klass.after_save method_name klass.after_destroy method_name klass.after_touch method_name end end