Module: ActiveMongoid::Associations::RecordRelation::AutoSave::ClassMethods

Defined in:
lib/active_mongoid/associations/record_relation/auto_save.rb

Instance Method Summary collapse

Instance Method Details

#autosave_record_id(metadata) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_mongoid/associations/record_relation/auto_save.rb', line 26

def autosave_record_id()
  if .stores_foreign_key?
    save_method = :"autosave_record_id_for_#{.name}"
    define_method(save_method) do
      if relation = instance_variable_get("@#{.name}")
        self.send(.foreign_key_setter, relation.send(.primary_key))
      end
    end
    before_save save_method
  end
end

#autosave_records(metadata) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_mongoid/associations/record_relation/auto_save.rb', line 13

def autosave_records()
  if .autosave?
    save_method = :"autosave_records_for_#{.name}"
    define_method(save_method) do
      if relation = instance_variable_get("@#{.name}")
        Array(relation).each { |d| d.save if record_changed_for_autosave?(d) }
      end
    end
    after_save save_method
  end
  autosave_record_id()
end