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

Ticket #7983: acts_as_tree.diff

File acts_as_tree.diff, 3.8 kB (added by fearoffish, 2 years ago)
  • tree.rb

    old new  
    1818      #         \_ subchild1 
    1919      #         \_ subchild2 
    2020      # 
    21       #   root      = Category.create("name" => "root") 
    22       #   child1    = root.children.create("name" => "child1") 
    23       #   subchild1 = child1.children.create("name" => "subchild1") 
     21      #   root      = Category.create(:name => "root") 
     22      #   child1    = root.children.create(:name => "child1") 
     23      #   subchild1 = child1.children.create(:name => "subchild1") 
    2424      # 
    2525      #   root.parent   # => nil 
    2626      #   child1.parent # => root 
     
    2929      # 
    3030      # In addition to the parent and children associations, the following instance methods are added to the class 
    3131      # after specifying the act: 
    32       # * siblings          : Returns all the children of the parent, excluding the current node ([ subchild2 ] when called from subchild1) 
    33       # * self_and_siblings : Returns all the children of the parent, including the current node ([ subchild1, subchild2 ] when called from subchild1) 
    34       # * ancestors         : Returns all the ancestors of the current node ([child1, root] when called from subchild2) 
    35       # * root              : Returns the root of the current node (root when called from subchild2) 
     32      # * siblings          : Returns all the children of the parent, excluding the current node  
     33      #                           ([ subchild2 ] when called from subchild1) 
     34      # * self_and_siblings : Returns all the children of the parent, including the current node  
     35      #                           ([ subchild1, subchild2 ] when called from subchild1) 
     36      # * ancestors         : Returns all the ancestors of the current node  
     37      #                           ([child1, root] when called from subchild2) 
     38      # * root              : Returns the root of the current node  
     39      #                           (root when called from subchild2) 
    3640      module ClassMethods 
    37         # Configuration options are: 
     41        # Declare a model as part of a tree structure, adding convenience methods that handle most of the  
     42        # moving/querying parts of that structure.  The database table should have a parent_id field (the name 
     43        # is configurable) which will be used to tie the 'branches' of the tree. 
    3844        # 
     45        # ==== Options 
     46        # 
    3947        # * <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). 
     48        # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet 
     49        # * <tt>counter_cache</tt> - keeps a count in a children_count column if set to true (default: false) 
     50        # 
     51        # ==== Examples 
     52        # 
     53        #   class Category < ActiveRecord::Base 
     54        #     acts_as_tree :order => "name" 
     55        #   end 
     56        #    
     57        #   # Gives: 
     58        #   root      = Category.create(:name => "root") 
     59        #   child1    = root.children.create(:name => "child1") 
     60        #   subchild1 = child1.children.create(:name => "subchild1") 
     61        # 
     62        #   # If your 'parent_id' field is named differently and you have a column called 'children_count' 
     63        #   class Category < ActiveRecord::Base 
     64        #     acts_as_tree :order => "name", :foreign_key => 'related_id', :counter_cache => true 
     65        #   end 
     66        # 
    4267        def acts_as_tree(options = {}) 
    4368          configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil } 
    4469          configuration.update(options) if options.is_a?(Hash)