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
-
SUPPRESS_TOUCH_CALLBACKS_KEY =
The key to use to store the active touch callback suppression statuses
"[mongoid]:suppress-touch-callbacks"
Instance Method Summary
-
#define_touchable!(association) ⇒ Class
Add the association to the touchable associations if the touch option was provided.
-
#suppress_touch_callbacks(name)
Internal use only
Internal use only
Suppresses touch callbacks for the named class, for the duration of the associated block.
-
#touch_callbacks_suppressed?(name) ⇒ true | false
Internal use only
Internal use only
Queries whether touch callbacks are being suppressed for the named class.
-
#define_relation_touch_method(name, association) ⇒ Symbol
private
Internal use only
Internal use only
Define the method that will get called for touching belongs_to associations.
-
#touch_callback_statuses ⇒ Hash
private
Returns a hash to be used to store and query the various touch callback suppression statuses for different classes.
Instance Method Details
#define_relation_touch_method(name, association) ⇒ Symbol (private)
Define the method that will get called for touching belongs_to associations.
# 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.
# 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. end end
#suppress_touch_callbacks(name)
Suppresses touch callbacks for the named class, for the duration of the associated block.
# 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_statuses ⇒ Hash (private)
Returns a hash to be used to store and query the various touch callback suppression statuses for different classes.
# File 'lib/mongoid/touchable.rb', line 197
def touch_callback_statuses Threaded.get(SUPPRESS_TOUCH_CALLBACKS_KEY) { {} } end
#touch_callbacks_suppressed?(name) ⇒ true
| false
Queries whether touch callbacks are being suppressed for the named class.
# File 'lib/mongoid/touchable.rb', line 183
def touch_callbacks_suppressed?(name) touch_callback_statuses[name] end