Module: Mongoid::Relations::AutoSave::ClassMethods
- Defined in:
- lib/mongoid/relations/auto_save.rb
Instance Method Summary collapse
-
#autosavable?(metadata) ⇒ true, false
Can the autosave be added?.
-
#autosave(metadata) ⇒ Object
Set up the autosave behaviour for references many and references one relations.
Instance Method Details
#autosavable?(metadata) ⇒ true, false
Can the autosave be added?
90 91 92 |
# File 'lib/mongoid/relations/auto_save.rb', line 90 def autosavable?() !autosaved_relations.include?(.name) && !. end |
#autosave(metadata) ⇒ Object
Set up the autosave behaviour for references many and references one relations. When the option is set to true, these relations will get saved automatically when the parent saved, if they are dirty.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mongoid/relations/auto_save.rb', line 59 def autosave() if .autosave? && autosavable?() autosaved_relations.push(.name) set_callback :save, :after, unless: :autosaved? do |document| # @todo: Durran: Remove with Rails 4 after callback termination. if before_callback_halted? self.before_callback_halted = false else begin_autosave relation = document.send(.name) if relation (relation.do_or_do_not(:in_memory) || Array.wrap(relation)).each do |doc| doc.save end end exit_autosave end end end end |