Module: Couchbase::Model::Relationship::Parent

Extended by:
ActiveSupport::Concern
Defined in:
lib/couchbase/model/relationship/parent.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#changed_childrenObject



83
84
85
86
87
# File 'lib/couchbase/model/relationship/parent.rb', line 83

def changed_children
  loaded_children.select do |child|
    child.changed? || (child.respond_to?(:children_changed?) ? child.children_changed? : nil)
  end
end

#childrenObject



71
72
73
74
75
# File 'lib/couchbase/model/relationship/parent.rb', line 71

def children
  self.class.child_associations.map do |association|
    association.fetch self
  end.compact
end

#children_changed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/couchbase/model/relationship/parent.rb', line 40

def children_changed?
  changed_children.present?
end

#delete_with_autodelete_children(options = {}) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/couchbase/model/relationship/parent.rb', line 51

def delete_with_autodelete_children(options = {})
  self.class.child_associations.select(&:auto_delete).each do |association|
    association.fetch(self).try :delete, options
  end

  delete_without_autodelete_children(options)
end

#delete_with_children(options = {}) ⇒ Object

FIXME #changed? should include children if any are autosave



61
62
63
64
65
66
67
68
69
# File 'lib/couchbase/model/relationship/parent.rb', line 61

def delete_with_children(options = {})
  children.each do |child| 
    if child.persisted?
      child.delete options
    end
  end

  delete(options)
end

#loaded_childrenObject



77
78
79
80
81
# File 'lib/couchbase/model/relationship/parent.rb', line 77

def loaded_children
  self.class.child_associations.map do |association|
    association.fetch(self) if association.loaded?(self)
  end.compact
end

#reload_allObject



89
90
91
92
# File 'lib/couchbase/model/relationship/parent.rb', line 89

def reload_all
  children.each(&:reload)
  reload
end

#save_with_autosave_children(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/couchbase/model/relationship/parent.rb', line 29

def save_with_autosave_children(options = {})
  # Don't save if we failed
  save_without_autosave_children(options).tap do |result|
    if result
      self.class.child_associations.select(&:auto_save).each do |association|
        association.fetch(self).try :save_if_changed, options
      end
    end
  end
end

#save_with_changed_childrenObject



44
45
46
47
48
49
# File 'lib/couchbase/model/relationship/parent.rb', line 44

def save_with_changed_children
  save_if_changed
  changed_children.each do |child|
    child.respond_to?(:save_with_changed_children) ? child.save_with_changed_children : child.save_if_changed
  end
end

#save_with_children(options = {}) ⇒ Object

TODO How to handle failures saving children?



19
20
21
22
23
24
25
26
27
# File 'lib/couchbase/model/relationship/parent.rb', line 19

def save_with_children(options = {})
  save(options).tap do |result|
    if result
      children.each do |child|
        child.save_if_changed(options)
      end
    end
  end
end