aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/acts
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-07-01 18:33:38 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-07-01 18:33:38 +0000
commitebd7bf7945d8fc6881a7b576fe22eaaa2ca3efd8 (patch)
tree9fe8236251a7160393dd035bc733717318440183 /activerecord/lib/active_record/acts
parentf0e9fd74aed3d74e6ae8f53dbdcd1340d238cb57 (diff)
downloadrails-ebd7bf7945d8fc6881a7b576fe22eaaa2ca3efd8.tar.gz
rails-ebd7bf7945d8fc6881a7b576fe22eaaa2ca3efd8.tar.bz2
rails-ebd7bf7945d8fc6881a7b576fe22eaaa2ca3efd8.zip
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
Diffstat (limited to 'activerecord/lib/active_record/acts')
-rw-r--r--activerecord/lib/active_record/acts/tree.rb19
1 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/acts/tree.rb b/activerecord/lib/active_record/acts/tree.rb
index 61163941d3..2463a1209a 100644
--- a/activerecord/lib/active_record/acts/tree.rb
+++ b/activerecord/lib/active_record/acts/tree.rb
@@ -35,9 +35,26 @@ module ActiveRecord
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