Module: Mongoid::Association::Referenced::AutoSave
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
| Included In: | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Class Chain: 
          self,
          ActiveSupport::Concern
         | |
| Defined in: | lib/mongoid/association/referenced/auto_save.rb | 
Overview
Mixin module included into ::Mongoid::Document which adds the ability to automatically save opposite-side documents in referenced associations when saving the subject document.
Class Method Summary
- 
    
      .define_autosave!(association)  ⇒ Class 
    
    Define the autosave method on an association’s owning class for an associated object. 
Instance Attribute Summary
- 
    
      #autosaved?  ⇒ true | false 
    
    readonly
    Used to prevent infinite loops in associated autosaves. 
Instance Method Summary
- 
    
      #__autosaving__  
    
    Begin the associated autosave. 
- 
    
      #changed_for_autosave?(doc)  ⇒ Boolean 
    
    Check if there is changes for auto-saving. 
Class Method Details
    .define_autosave!(association)  ⇒ Class 
  
Define the autosave method on an association’s owning class for an associated object.
# File 'lib/mongoid/association/referenced/auto_save.rb', line 53
def self.define_autosave!(association) association.inverse_class.tap do |klass| save_method = :"autosave_documents_for_#{association.name}" klass.send(:define_method, save_method) do if before_callback_halted? self.before_callback_halted = false else __autosaving__ do if assoc_value = ivar(association.name) Array(assoc_value).each do |doc| pc = doc.persistence_context? ? doc.persistence_context : persistence_context.for_child(doc) doc.with(pc) do |d| d.save end end end end end end klass.after_persist_parent save_method, unless: :autosaved? end end
Instance Attribute Details
    #autosaved?  ⇒ true | false  (readonly)
  
Used to prevent infinite loops in associated autosaves.
# File 'lib/mongoid/association/referenced/auto_save.rb', line 20
def autosaved? Threaded.autosaved?(self) end
Instance Method Details
#__autosaving__
Begin the associated autosave.
# File 'lib/mongoid/association/referenced/auto_save.rb', line 28
def __autosaving__ Threaded.begin_autosave(self) yield ensure Threaded.exit_autosave(self) end
#changed_for_autosave?(doc) ⇒ Boolean
Check if there is changes for auto-saving
document.changed_for_autosave?# File 'lib/mongoid/association/referenced/auto_save.rb', line 40
def changed_for_autosave?(doc) doc.new_record? || doc.changed? || doc.marked_for_destruction? end