Class: Mongoid::Acts::Tree::Children
- Inherits:
-
Array
- Object
- Array
- Mongoid::Acts::Tree::Children
- Defined in:
- lib/mongoid_acts_as_tree.rb
Overview
proxy class
Instance Method Summary collapse
-
#<<(object, will_save = true) ⇒ Object
(also: #push)
Add new child to list of object children.
- #build(attributes) ⇒ Object (also: #create)
-
#clear ⇒ Object
Clear children list.
-
#delete(object_or_id) ⇒ Object
Deletes object only from children list.
-
#initialize(owner) ⇒ Children
constructor
TODO: improve accessors to options to eliminate object.
Constructor Details
Instance Method Details
#<<(object, will_save = true) ⇒ Object Also known as: push
Add new child to list of object children
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/mongoid_acts_as_tree.rb', line 194 def <<(object, will_save=true) if object.descendants.include? @parent object.instance_variable_set :@_cyclic, true else object.write_attribute object.parent_id_field, @parent._id object[object.path_field] = @parent[@parent.path_field] + [@parent._id] object[object.depth_field] = @parent[@parent.depth_field] + 1 object.instance_variable_set :@_will_move, true object.save if will_save end super(object) end |
#build(attributes) ⇒ Object Also known as: create
208 209 210 211 212 |
# File 'lib/mongoid_acts_as_tree.rb', line 208 def build(attributes) child = @parent.class.new(attributes) self.push child child end |
#clear ⇒ Object
Clear children list
237 238 239 240 241 |
# File 'lib/mongoid_acts_as_tree.rb', line 237 def clear self.each do | child | @parent.children.delete child end end |
#delete(object_or_id) ⇒ Object
Deletes object only from children list. To delete object use object.destroy
.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/mongoid_acts_as_tree.rb', line 220 def delete(object_or_id) object = case object_or_id when String, BSON::ObjectId @parent.class.find object_or_id else object_or_id end object.write_attribute object.parent_id_field, nil object[object.path_field] = [] object[object.depth_field] = 0 object.save super(object) end |