Adds a child to this object in the tree. If this object hasn‘t been initialized, it gets set up as a root node. Otherwise, this method will update all of the other elements in the tree and shift them to the right, keeping everything balanced.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 141
141: def add_child( child )
142: self.reload
143: child.reload
144:
145: if child.root?
146: raise "Adding sub-tree isn\'t currently supported"
147: else
148: if ( (self[left_col_name] == nil) || (self[right_col_name] == nil) )
149: # Looks like we're now the root node! Woo
150: self[left_col_name] = 1
151: self[right_col_name] = 4
152:
153: # What do to do about validation?
154: return nil unless self.save
155:
156: child[parent_column] = self.id
157: child[left_col_name] = 2
158: child[right_col_name]= 3
159: return child.save
160: else
161: # OK, we need to add and shift everything else to the right
162: child[parent_column] = self.id
163: right_bound = self[right_col_name]
164: child[left_col_name] = right_bound
165: child[right_col_name] = right_bound + 1
166: self[right_col_name] += 2
167: self.class.base_class.transaction {
168: self.class.base_class.update_all( "#{left_col_name} = (#{left_col_name} + 2)", "#{scope_condition} AND #{left_col_name} >= #{right_bound}" )
169: self.class.base_class.update_all( "#{right_col_name} = (#{right_col_name} + 2)", "#{scope_condition} AND #{right_col_name} >= #{right_bound}" )
170: self.save
171: child.save
172: }
173: end
174: end
175: end
Returns a set of all of its children and nested children
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 188
188: def all_children
189: self.class.base_class.find(:all, :conditions => "#{scope_condition} AND (#{left_col_name} > #{self[left_col_name]}) and (#{right_col_name} < #{self[right_col_name]})" )
190: end
Prunes a branch off of the tree, shifting all of the elements on the right back to the left so the counts still work.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 199
199: def before_destroy
200: return if self[right_col_name].nil? || self[left_col_name].nil?
201: dif = self[right_col_name] - self[left_col_name] + 1
202:
203: self.class.base_class.transaction {
204: self.class.base_class.delete_all( "#{scope_condition} and #{left_col_name} > #{self[left_col_name]} and #{right_col_name} < #{self[right_col_name]}" )
205: self.class.base_class.update_all( "#{left_col_name} = (#{left_col_name} - #{dif})", "#{scope_condition} AND #{left_col_name} >= #{self[right_col_name]}" )
206: self.class.base_class.update_all( "#{right_col_name} = (#{right_col_name} - #{dif} )", "#{scope_condition} AND #{right_col_name} >= #{self[right_col_name]}" )
207: }
208: end
Returns true is this is a child node
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 126
126: def child?
127: parent_id = self[parent_column]
128: !(parent_id == 0 || parent_id.nil?) && (self[left_col_name] > 1) && (self[right_col_name] > self[left_col_name])
129: end
Returns the number of nested children of this object.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 178
178: def children_count
179: return (self[right_col_name] - self[left_col_name] - 1)/2
180: end
Returns a set of only this entry‘s immediate children
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 193
193: def direct_children
194: self.class.base_class.find(:all, :conditions => "#{scope_condition} and #{parent_column} = #{self.id}")
195: end
Returns a set of itself and all of its nested children
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 183
183: def full_set
184: self.class.base_class.find(:all, :conditions => "#{scope_condition} AND (#{left_col_name} BETWEEN #{self[left_col_name]} and #{self[right_col_name]})" )
185: end
Returns true is this is a root node.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 120
120: def root?
121: parent_id = self[parent_column]
122: (parent_id == 0 || parent_id.nil?) && (self[left_col_name] == 1) && (self[right_col_name] > self[left_col_name])
123: end
Returns true if we have no idea what this is
[ show source ]
# File vendor/rails/activerecord/lib/active_record/acts/nested_set.rb, line 132
132: def unknown?
133: !root? && !child?
134: end