# File lib/symetrie_com/acts_as_better_nested_set.rb, line 755 def add_child(child) transaction do self.reload; child.reload # for compatibility with old version # the old version allows records with nil values for lft and rgt unless self[left_col_name] && self[right_col_name] if child[left_col_name] || child[right_col_name] raise ActiveRecord::ActiveRecordError, "If parent lft or rgt are nil, you can't add a child with non-nil lft or rgt" end base_set_class.update_all("#{left_col_name} = CASE \ WHEN id = #{self.id} \ THEN 1 \ WHEN id = #{child.id} \ THEN 3 \ ELSE #{left_col_name} END, \ #{right_col_name} = CASE \ WHEN id = #{self.id} \ THEN 2 \ WHEN id = #{child.id} \ THEN 4 \ ELSE #{right_col_name} END", scope_condition) self.reload; child.reload end unless child[left_col_name] && child[right_col_name] maxright = base_set_class.maximum(right_col_name, :conditions => scope_condition) || 0 base_set_class.update_all("#{left_col_name} = CASE \ WHEN id = #{child.id} \ THEN #{maxright + 1} \ ELSE #{left_col_name} END, \ #{right_col_name} = CASE \ WHEN id = #{child.id} \ THEN #{maxright + 2} \ ELSE #{right_col_name} END", scope_condition) child.reload end child.move_to_child_of(self) # self.reload ## even though move_to calls target.reload, at least one object in the tests was not reloading (near the end of test_common_usage) end # self.reload # child.reload # # if child.root? # raise ActiveRecord::ActiveRecordError, "Adding sub-tree isn\'t currently supported" # else # if ( (self[left_col_name] == nil) || (self[right_col_name] == nil) ) # # Looks like we're now the root node! Woo # self[left_col_name] = 1 # self[right_col_name] = 4 # # # What do to do about validation? # return nil unless self.save # # child[parent_col_name] = self.id # child[left_col_name] = 2 # child[right_col_name]= 3 # return child.save # else # # OK, we need to add and shift everything else to the right # child[parent_col_name] = self.id # right_bound = self[right_col_name] # child[left_col_name] = right_bound # child[right_col_name] = right_bound + 1 # self[right_col_name] += 2 # self.class.transaction { # self.class.update_all( "#{left_col_name} = (#{left_col_name} + 2)", "#{scope_condition} AND #{left_col_name} >= #{right_bound}" ) # self.class.update_all( "#{right_col_name} = (#{right_col_name} + 2)", "#{scope_condition} AND #{right_col_name} >= #{right_bound}" ) # self.save # child.save # } # end # end end