diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-18 03:22:45 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-18 03:22:45 +0000 |
commit | a2fba43ebc8e916213b0110d21aff44572b15edb (patch) | |
tree | 369b7e47f28d2a5b978a1bed4ab4b24eba4cfe22 /activerecord | |
parent | ea617708c2ad9a770a1f3950e7a0028e9764d989 (diff) | |
download | rails-a2fba43ebc8e916213b0110d21aff44572b15edb.tar.gz rails-a2fba43ebc8e916213b0110d21aff44572b15edb.tar.bz2 rails-a2fba43ebc8e916213b0110d21aff44572b15edb.zip |
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
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/acts/tree.rb | 54 |
1 files changed, 29 insertions, 25 deletions
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 |