Module: DataMapper::Resource

Defined in:
lib/dm-accepts_nested_attributes/resource.rb

Instance Method Summary collapse

Instance Method Details

#save(context = :default) ⇒ Object

basic extract method refactorings to work around a bug in extlib see sick.snusnu.info/2009/04/29/extlibhook-breaks-if-hooked-method-is-redefined/ maybe they are worth considering even when the bug in extlib (hopefully) gets fixed



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dm-accepts_nested_attributes/resource.rb', line 8

def save(context = :default)
  
  associations_saved = false
  associations_saved = save_child_associations(associations_saved, context)
  
  saved = save_self
  
  if saved
    original_values.clear
  end
  
  associations_saved = save_parent_associations(associations_saved, context)
  
  # We should return true if the model (or any of its associations) were saved.
  (saved | associations_saved) == true
  
end

#save_child_associations(saved, context) ⇒ Object



27
28
29
30
# File 'lib/dm-accepts_nested_attributes/resource.rb', line 27

def save_child_associations(saved, context)
  child_associations.each { |a| saved |= a.save }
  saved
end

#save_parent_associations(saved, context) ⇒ Object



36
37
38
39
# File 'lib/dm-accepts_nested_attributes/resource.rb', line 36

def save_parent_associations(saved, context)
  parent_associations.each { |a| saved |= a.save }
  saved
end

#save_selfObject



32
33
34
# File 'lib/dm-accepts_nested_attributes/resource.rb', line 32

def save_self
  new_record? ? create : update
end