From a2fba43ebc8e916213b0110d21aff44572b15edb Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 18 Mar 2006 03:22:45 +0000 Subject: The style police makes an expection on acts_as_tree (closes #4168) [coffee2code] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3906 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/acts/tree.rb | 54 ++++++++++++++++------------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/acts/tree.rb b/activerecord/lib/active_record/acts/tree.rb index e4ab8cd708..c5aa4cd22d 100644 --- a/activerecord/lib/active_record/acts/tree.rb +++ b/activerecord/lib/active_record/acts/tree.rb @@ -45,40 +45,44 @@ module ActiveRecord configuration.update(options) if options.is_a?(Hash) belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache] - has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => true + has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy - module_eval <<-END + class_eval <<-EOV + include ActiveRecord::Acts::Tree::InstanceMethods + def self.roots - self.find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) + find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) end + def self.root - self.find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) + find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) end - END - - # Returns list of ancestors, starting from parent until root. - # - # subchild1.ancestors # => [child1, root] - define_method(:ancestors) do - node, nodes = self, [] - nodes << node = node.parent until not node.has_parent? - nodes - end + EOV + end + end - define_method(:root) do - node = self - node = node.parent until not node.has_parent? - node - end + module InstanceMethods + # Returns list of ancestors, starting from parent until root. + # + # subchild1.ancestors # => [child1, root] + def ancestors + node, nodes = self, [] + nodes << node = node.parent until not node.has_parent? + nodes + end - define_method(:siblings) do - self_and_siblings - [self] - end + def root + node = self + node = node.parent until not node.has_parent? + node + end - define_method(:self_and_siblings) do - has_parent? ? parent.children : self.class.roots - end + def siblings + self_and_siblings - [self] + end + def self_and_siblings + has_parent? ? parent.children : self.class.roots end end end -- cgit v1.2.3