Module: ActiveRecord::Adapters::Mongoid::Associations::InstanceMethods

Defined in:
lib/active_record/adapters/mongoid/associations.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/active_record/adapters/mongoid/associations.rb', line 37

def method_missing(meth, *args, &block)
  case meth
  when /build_(.+)$/
    self.send("#{$1}=".to_sym, ($1.classify.constantize).new(*args, &block)) rescue super(meth, *args, &block)
  when /create_(.+)$/          
    self.send("#{$1}=".to_sym, ($1.classify.constantize).create(*args, &block)) rescue super(meth, *args, &block)
  else
    super(meth, *args, &block)
  end
end

Instance Method Details

#save_document(document) ⇒ Object



21
22
23
24
# File 'lib/active_record/adapters/mongoid/associations.rb', line 21

def save_document(document)
  send(document).save if send(document) && (send(document).changed? || send(document).new_record?)
  instance_variable_get("@old_#{document}".to_sym).save if instance_variable_get("@old_#{document}".to_sym)
end

#save_documents(documents) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/active_record/adapters/mongoid/associations.rb', line 26

def save_documents(documents)
  send(documents).each do |document|
    document.save if document && (document.changed? || document.new_record?)
  end
  if instance_variable_get("@old_#{documents}".to_sym)
    instance_variable_get("@old_#{documents}".to_sym).each do |old_document| 
      old_document.save
    end
  end
end