module Mongoid::Relations::Touchable::ClassMethods

Public Instance Methods

touchable(metadata) click to toggle source

Add the metadata to the touchable relations if the touch option was provided.

@example Add the touchable.

Model.touchable(meta)

@param [ Metadata ] metadata The relation metadata.

@return [ Class ] The model class.

@since 3.0.0

# File lib/mongoid/relations/touchable.rb, line 52
def touchable(metadata)
  if metadata.touchable?
    name = metadata.name
    field = metadata[:touch].is_a?(Symbol) ? metadata[:touch] : nil
    method_name = define_relation_touch_method(name, field)
    after_save method_name
    after_destroy method_name
    after_touch method_name
  end
  self
end

Private Instance Methods

define_relation_touch_method(name, extra_field = nil) click to toggle source

Define the method that will get called for touching belongs_to relations.

@api private

@example Define the touch relation.

Model.define_relation_touch_method(:band)
Model.define_relation_touch_method(:band, :band_updated_at)

@param [ Symbol ] name The name of the relation. @param [ Symbol ] extra_field Additional timestamp field to update.

@since 3.1.0

@return [ Symbol ] The method name.

# File lib/mongoid/relations/touchable.rb, line 81
        def define_relation_touch_method(name, extra_field = nil)
          method_name = "touch_#{name}_after_create_or_destroy"
          class_eval "            def #{method_name}
              without_autobuild do
                relation = __send__(:#{name})
                relation.touch #{":#{extra_field}" if extra_field} if relation
              end
            end
", __FILE__, __LINE__ + 1
          method_name.to_sym
        end