123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Touchable

Relationships & Source Files
Namespace Children
Modules:
Defined in: lib/mongoid/touchable.rb

Overview

Mixin module which is included in Document to add “touch” functionality to update a document’s timestamp(s) atomically.

Constant Summary

Instance Method Summary

Instance Method Details

#define_relation_touch_method(name, association) ⇒ Symbol (private)

This method is for internal use only.

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

Examples:

Define the touch association.

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

Parameters:

Returns:

  • (Symbol)

    The method name.

[ GitHub ]

  
# File 'lib/mongoid/touchable.rb', line 214

def define_relation_touch_method(name, association)
  method_name = "touch_#{name}_after_create_or_destroy"
  association.inverse_class.class_eval do
    define_method(method_name) do
      without_autobuild do
        if !touch_callbacks_suppressed? && relation = __send__(name)
          # This looks up touch_field at runtime, rather than at method definition time.
          # If touch_field is nil, it will only touch the default field (updated_at).
          relation.touch(association.touch_field)
        end
      end
    end
  end
  method_name.to_sym
end

#define_touchable!(association) ⇒ Class

Add the association to the touchable associations if the touch option was provided.

Examples:

Add the touchable.

Model.define_touchable!(assoc)

Parameters:

Returns:

  • (Class)

    The model class.

[ GitHub ]

  
# File 'lib/mongoid/touchable.rb', line 153

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

    # Embedded docs handle touch updates recursively within
    # the #touch method itself
    klass.after_touch method_name unless association.embedded?
  end
end

#suppress_touch_callbacks(name)

This method is for internal use only.

Suppresses touch callbacks for the named class, for the duration of the associated block.

[ GitHub ]

  
# File 'lib/mongoid/touchable.rb', line 170

def suppress_touch_callbacks(name)
  save, touch_callback_statuses[name] = touch_callback_statuses[name], true
  yield
ensure
  touch_callback_statuses[name] = save
end

#touch_callback_statusesHash (private)

Returns a hash to be used to store and query the various touch callback suppression statuses for different classes.

Returns:

  • (Hash)

    The hash that contains touch callback suppression statuses

[ GitHub ]

  
# File 'lib/mongoid/touchable.rb', line 197

def touch_callback_statuses
  Thread.current[SUPPRESS_TOUCH_CALLBACKS_KEY] ||= {}
end

#touch_callbacks_suppressed?(name) ⇒ true | false

This method is for internal use only.

Queries whether touch callbacks are being suppressed for the named class.

Returns:

  • (true | false)

    Whether touch callbacks are suppressed.

[ GitHub ]

  
# File 'lib/mongoid/touchable.rb', line 183

def touch_callbacks_suppressed?(name)
  touch_callback_statuses[name]
end