Skip to content
Browse files

Added roots, root, and siblings to the batch of methods added by acts…

…_as_tree #1541 [michael@schuerig.de]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1585 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f0e9fd7 commit ebd7bf7945d8fc6881a7b576fe22eaaa2ca3efd8 @dhh dhh committed Jul 1, 2005
Showing with 20 additions and 1 deletion.
  1. +2 −0 activerecord/CHANGELOG
  2. +18 −1 activerecord/lib/active_record/acts/tree.rb
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added roots, root, and siblings to the batch of methods added by acts_as_tree #1541 [michael@schuerig.de]
+
* Added support for limit/offset with the MS SQL Server driver so that pagination will now work #1569 [DeLynn Berry]
* Added support for ODBC connections to MS SQL Server so you can connect from a non-Windows machine #1569 [Mark Imbriaco/DeLynn Berry]
View
19 activerecord/lib/active_record/acts/tree.rb
@@ -35,9 +35,26 @@ module ClassMethods
def acts_as_tree(options = {})
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
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
+
+ module_eval <<-END
+ def self.roots
+ self.find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => "#{configuration[:order]}")
+ end
+ def self.root
+ self.find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => "#{configuration[:order]}")
+ end
+ END
+
+ define_method(:siblings) do
+ if parent
+ self.class.find(:all, :conditions => [ "#{configuration[:foreign_key]} = ?", parent.id ], :order => configuration[:order])
+ else
+ self.class.roots
+ end
+ end
end
end
end

0 comments on commit ebd7bf7

Please sign in to comment.
Something went wrong with that request. Please try again.