From f033833fb9f48927995e8bc521ae032888813989 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 16 Dec 2004 16:56:30 +0000 Subject: Improving documentation... git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@191 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/acts/tree.rb | 66 +++++++++++++++-------------- 1 file changed, 35 insertions(+), 31 deletions(-) (limited to 'activerecord/lib/active_record/acts/tree.rb') diff --git a/activerecord/lib/active_record/acts/tree.rb b/activerecord/lib/active_record/acts/tree.rb index f05bbe9a78..c01b6f7ab7 100644 --- a/activerecord/lib/active_record/acts/tree.rb +++ b/activerecord/lib/active_record/acts/tree.rb @@ -1,39 +1,43 @@ module ActiveRecord - module Acts - # Including this mixin if you want to model a tree structure by providing a parent association and an children - # association. This mixin assumes that you have a column called parent_id - # - # class Category < ActiveRecord::Base - # include ActiveRecord::Mixins::Tree - # end - # - # Example : - # root - # \_ child1 - # \_ sub-child1 - # - # root = Category.create("name" => "root") - # child1 = root.children.create("name" => "child1") - # subchild1 = child1.children.create("name" => "subchild1") - # - # root.parent # => nil - # child1.parent # => root - # root.children # => [child1] - # root.children.first.children.first # => subchild1 - module Tree + module Acts #:nodoc: + module Tree #:nodoc: def self.append_features(base) super base.extend(ClassMethods) end - end - - module ClassMethods - def acts_as_tree(options = {}) - configuration = { :foreign_key => "parent_id", :order => nil } - configuration.update(options) if options.is_a?(Hash) - - belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key] - has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => true + + # Specify this act if you want to model a tree structure by providing a parent association and an children + # association. This act assumes that requires that you have a foreign key column, which by default is called parent_id. + # + # class Category < ActiveRecord::Base + # acts_as_tree :order => "name" + # end + # + # Example : + # root + # \_ child1 + # \_ sub-child1 + # + # root = Category.create("name" => "root") + # child1 = root.children.create("name" => "child1") + # subchild1 = child1.children.create("name" => "subchild1") + # + # root.parent # => nil + # child1.parent # => root + # root.children # => [child1] + # root.children.first.children.first # => subchild1 + module ClassMethods + # Configuration options are: + # + # * foreign_key - specifies the column name to use for track of the tree (default: parent_id) + # * order - makes it possible to sort the children according to this SQL snippet. + def acts_as_tree(options = {}) + configuration = { :foreign_key => "parent_id", :order => nil } + configuration.update(options) if options.is_a?(Hash) + + belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key] + has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => true + end end end end -- cgit v1.2.3