176: def add_child( child )
177: self.reload
178: child.reload
179:
180: if child.root?
181: raise ActiveRecord::ActiveRecordError, "Adding sub-tree isn\'t currently supported"
182: else
183: if ( (self[acts_as_nested_set_options[:left_column]] == nil) || (self[acts_as_nested_set_options[:right_column]] == nil) )
184:
185: self[acts_as_nested_set_options[:left_column]] = 1
186: self[acts_as_nested_set_options[:right_column]] = 4
187:
188:
189: return nil unless self.save
190:
191: child[acts_as_nested_set_options[:parent_column]] = self.id
192: child[acts_as_nested_set_options[:left_column]] = 2
193: child[acts_as_nested_set_options[:right_column]]= 3
194: return child.save
195: else
196:
197: child[acts_as_nested_set_options[:parent_column]] = self.id
198: right_bound = self[acts_as_nested_set_options[:right_column]]
199: child[acts_as_nested_set_options[:left_column]] = right_bound
200: child[acts_as_nested_set_options[:right_column]] = right_bound + 1
201: self[acts_as_nested_set_options[:right_column]] += 2
202: self.class.transaction {
203: self.class.update_all( "#{acts_as_nested_set_options[:left_column]} = (#{acts_as_nested_set_options[:left_column]} + 2)", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:left_column]} >= #{right_bound}" )
204: self.class.update_all( "#{acts_as_nested_set_options[:right_column]} = (#{acts_as_nested_set_options[:right_column]} + 2)", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:right_column]} >= #{right_bound}" )
205: self.save
206: child.save
207: }
208: end
209: end
210: end