module Mongoid::Touchable
Public Instance Methods
define_touchable!(association)
click to toggle source
Add the association to the touchable associations if the touch option was provided.
@example Add the touchable.
Model.define_touchable!(assoc)
@param [ Association
] association The association metadata.
@return [ Class ] The model class.
@since 3.0.0
# File lib/mongoid/touchable.rb, line 85 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
Private Instance Methods
define_relation_touch_method(name, association)
click to toggle source
Define the method that will get called for touching belongs_to associations.
@api private
@example Define the touch association.
Model.define_relation_touch_method(:band) Model.define_relation_touch_method(:band, :band_updated_at)
@param [ Symbol ] name The name of the association. @param [ Association
] association The association metadata.
@since 3.1.0
@return [ Symbol ] The method name.
# File lib/mongoid/touchable.rb, line 112 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