Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7458

Show
Ignore:
Timestamp:
09/11/07 05:46:43 (1 year ago)
Author:
bitsweat
Message:

Fix generic file name in main load path.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/acts_as_tree/init.rb

    r7451 r7458  
    1 require 'acts_as_tree' 
     1ActiveRecord::Base.send :include, ActiveRecord::Acts::Tree 
  • plugins/acts_as_tree/lib/active_record/acts/tree.rb

    r7451 r7458  
    1 module ActsAsTree 
    2   def self.included(base) 
    3     base.extend(ClassMethods) 
    4   end 
     1module ActiveRecord 
     2  module Acts 
     3    module Tree 
     4      def self.included(base) 
     5        base.extend(ClassMethods) 
     6      end 
    57 
    6   # Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children 
    7   # association. This requires that you have a foreign key column, which by default is called +parent_id+. 
    8  
    9   #   class Category < ActiveRecord::Base 
    10   #     acts_as_tree :order => "name" 
    11   #   end 
    12  
    13   #   Example: 
    14   #   root 
    15   #    \_ child1 
    16   #         \_ subchild1 
    17   #         \_ subchild2 
    18  
    19   #   root      = Category.create("name" => "root") 
    20   #   child1    = root.children.create("name" => "child1") 
    21   #   subchild1 = child1.children.create("name" => "subchild1") 
    22  
    23   #   root.parent   # => nil 
    24   #   child1.parent # => root 
    25   #   root.children # => [child1] 
    26   #   root.children.first.children.first # => subchild1 
    27  
    28   # In addition to the parent and children associations, the following instance methods are added to the class 
    29   # after calling <tt>acts_as_tree</tt>: 
    30   # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node (<tt>[subchild2]</tt> when called on <tt>subchild1</tt>) 
    31   # * <tt>self_and_siblings</tt> - Returns all the children of the parent, including the current node (<tt>[subchild1, subchild2]</tt> when called on <tt>subchild1</tt>) 
    32   # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[child1, root]</tt> when called on <tt>subchild2</tt>) 
    33   # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>subchild2</tt>) 
    34   module ClassMethods 
    35     # Configuration options are: 
    36    
    37     # * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+) 
    38     # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet. 
    39     # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+). 
    40     def acts_as_tree(options = {}) 
    41       configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil } 
    42       configuration.update(options) if options.is_a?(Hash) 
     8      # Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children 
     9      # association. This requires that you have a foreign key column, which by default is called +parent_id+. 
     10     
     11      #   class Category < ActiveRecord::Base 
     12      #     acts_as_tree :order => "name" 
     13      #   end 
     14     
     15      #   Example: 
     16      #   root 
     17      #    \_ child1 
     18      #         \_ subchild1 
     19      #         \_ subchild2 
     20     
     21      #   root      = Category.create("name" => "root") 
     22      #   child1    = root.children.create("name" => "child1") 
     23      #   subchild1 = child1.children.create("name" => "subchild1") 
     24     
     25      #   root.parent   # => nil 
     26      #   child1.parent # => root 
     27      #   root.children # => [child1] 
     28      #   root.children.first.children.first # => subchild1 
     29     
     30      # In addition to the parent and children associations, the following instance methods are added to the class 
     31      # after calling <tt>acts_as_tree</tt>: 
     32      # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node (<tt>[subchild2]</tt> when called on <tt>subchild1</tt>) 
     33      # * <tt>self_and_siblings</tt> - Returns all the children of the parent, including the current node (<tt>[subchild1, subchild2]</tt> when called on <tt>subchild1</tt>) 
     34      # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[child1, root]</tt> when called on <tt>subchild2</tt>) 
     35      # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>subchild2</tt>) 
     36      module ClassMethods 
     37        # Configuration options are: 
     38       
     39        # * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+) 
     40        # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet. 
     41        # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+). 
     42        def acts_as_tree(options = {}) 
     43          configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil } 
     44          configuration.update(options) if options.is_a?(Hash) 
    4345 
    44       belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache] 
    45       has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy 
     46          belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache] 
     47          has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy 
    4648 
    47       class_eval <<-EOV 
    48         include ActsAsTree::InstanceMethods 
     49          class_eval <<-EOV 
     50            include ActsAsTree::InstanceMethods 
    4951 
    50         def self.roots 
    51           find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) 
     52            def self.roots 
     53              find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) 
     54            end 
     55 
     56            def self.root 
     57              find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) 
     58            end 
     59          EOV 
     60        end 
     61      end 
     62 
     63      module InstanceMethods 
     64        # Returns list of ancestors, starting from parent until root. 
     65        # 
     66        #   subchild1.ancestors # => [child1, root] 
     67        def ancestors 
     68          node, nodes = self, [] 
     69          nodes << node = node.parent while node.parent 
     70          nodes 
    5271        end 
    5372 
    54         def self.root 
    55           find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) 
     73        # Returns the root node of the tree. 
     74        def root 
     75          node = self 
     76          node = node.parent while node.parent 
     77          node 
    5678        end 
    57       EOV 
    58     end 
    59   end 
    6079 
    61   module InstanceMethods 
    62     # Returns list of ancestors, starting from parent until root. 
    63     # 
    64     #   subchild1.ancestors # => [child1, root] 
    65     def ancestors 
    66       node, nodes = self, [] 
    67       nodes << node = node.parent while node.parent 
    68       nodes 
    69     end 
     80        # Returns all siblings of the current node. 
     81        # 
     82        #   subchild1.siblings # => [subchild2] 
     83        def siblings 
     84          self_and_siblings - [self] 
     85        end 
    7086 
    71     # Returns the root node of the tree. 
    72     def root 
    73       node = self 
    74       node = node.parent while node.parent 
    75       node 
    76     end 
    77  
    78     # Returns all siblings of the current node. 
    79     # 
    80     #   subchild1.siblings # => [subchild2] 
    81     def siblings 
    82       self_and_siblings - [self] 
    83     end 
    84  
    85     # Returns all siblings and a reference to the current node. 
    86     # 
    87     #   subchild1.self_and_siblings # => [subchild1, subchild2] 
    88     def self_and_siblings 
    89       parent ? parent.children : self.class.roots 
     87        # Returns all siblings and a reference to the current node. 
     88        # 
     89        #   subchild1.self_and_siblings # => [subchild1, subchild2] 
     90        def self_and_siblings 
     91          parent ? parent.children : self.class.roots 
     92        end 
     93      end 
    9094    end 
    9195  end 
    9296end 
    93   
    94 ActiveRecord::Base.send(:include, ActsAsTree) 
  • plugins/acts_as_tree/Rakefile

    r7451 r7458  
    1313end 
    1414 
    15 desc 'Generate documentation for in_place_editing plugin.' 
     15desc 'Generate documentation for acts_as_tree plugin.' 
    1616Rake::RDocTask.new(:rdoc) do |rdoc| 
    1717  rdoc.rdoc_dir = 'rdoc' 
    18   rdoc.title    = 'InPlaceEditing
     18  rdoc.title    = 'acts_as_tree
    1919  rdoc.options << '--line-numbers' << '--inline-source' 
    2020  rdoc.rdoc_files.include('README') 
  • plugins/acts_as_tree/test/abstract_unit.rb

    r7451 r7458  
    11$:.unshift(File.dirname(__FILE__) + '/../../../rails/activesupport/lib') 
    22$:.unshift(File.dirname(__FILE__) + '/../../../rails/activerecord/lib') 
    3 $:.unshift(File.dirname(__FILE__) + '/../lib') 
    43 
    54require 'test/unit' 
     
    76require 'active_record' 
    87require 'active_record/fixtures' 
    9 require 'acts_as_tree' 
     8 
     9$:.unshift(File.dirname(__FILE__) + '/../lib') 
     10require "#{File.dirname(__FILE__)}/../init" 
    1011 
    1112config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))