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
- #changed_children ⇒ Object
- #children ⇒ Object
- #children_changed? ⇒ Boolean
- #delete_with_autodelete_children(options = {}) ⇒ Object
-
#delete_with_children(options = {}) ⇒ Object
FIXME #changed? should include children if any are autosave.
- #loaded_children ⇒ Object
- #reload_all ⇒ Object
- #save_with_autosave_children(options = {}) ⇒ Object
- #save_with_changed_children ⇒ Object
-
#save_with_children(options = {}) ⇒ Object
TODO How to handle failures saving children?.
Instance Method Details
#changed_children ⇒ Object
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 |
#children ⇒ Object
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
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( = {}) self.class.child_associations.select(&:auto_delete).each do |association| association.fetch(self).try :delete, end delete_without_autodelete_children() 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( = {}) children.each do |child| if child.persisted? child.delete end end delete() end |
#loaded_children ⇒ Object
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_all ⇒ Object
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( = {}) # Don't save if we failed save_without_autosave_children().tap do |result| if result self.class.child_associations.select(&:auto_save).each do |association| association.fetch(self).try :save_if_changed, end end end end |
#save_with_changed_children ⇒ Object
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( = {}) save().tap do |result| if result children.each do |child| child.save_if_changed() end end end end |