0
- def self.included(base)
0
- base.extend(ClassMethods)
0
- # An extension to DataMapper to easily allow the creation of tree
0
- # structures from your DataMapper models.
0
- # This requires a foreign key property for your model, which by default
0
- # would be called :parent_id.
0
- # include DataMapper::Resource
0
- # include DataMapper::Is::Tree
0
- # property :id, Fixnum
0
- # property :parent_id, Fixnum
0
- # property :name, String
0
- # is_a_tree :order => "name"
0
- # root = Category.create("name" => "root")
0
- # child = root.children.create("name" => "child")
0
- # grandchild1 = child1.children.create("name" => "grandchild1")
0
- # grandchild2 = child2.children.create("name" => "grandchild2")
0
- # root.parent # => nil
0
- # child.parent # => root
0
- # root.children # => [child]
0
- # root.children.first.children.first # => grandchild1
0
- # Category.first_root # => root
0
- # Category.roots # => [root]
0
- # The following instance methods are added:
0
- # * <tt>children</tt> - Returns all nodes with the current node as their parent, in the order specified by
0
- # <tt>:order</tt> (<tt>[grandchild1, grandchild2]</tt> when called on <tt>child</tt>)
0
- # * <tt>parent</tt> - Returns the node referenced by the foreign key (<tt>:parent_id</tt> by
0
- # default) (<tt>root</tt> when called on <tt>child</tt>)
0
- # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node
0
- # (<tt>[grandchild2]</tt> when called on <tt>grandchild1</tt>)
0
- # * <tt>generation</tt> - Returns all the children of the parent, including the current node (<tt>
0
- # [grandchild1, grandchild2]</tt> when called on <tt>grandchild1</tt>)
0
- # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[root, child1]</tt>
0
- # when called on <tt>grandchild2</tt>)
0
- # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>grandchild2</tt>)
0
- # Author:: Timothy Bennett (http://lanaer.com)
0
- # Configuration options are:
0
- # * <tt>child_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
0
- # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
0
- # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
0
- def is_a_tree(options = {})
0
- configuration = { :child_key => "parent_id" }
0
- configuration.update(options) if Hash === options
0
- belongs_to :parent, :class_name => name, :child_key => configuration[:child_key], :counter_cache => configuration[:counter_cache]
0
- has n, :children, :class_name => name, :child_key => configuration[:child_key], :order => configuration[:order]
0
- include DataMapper::Is::Tree::InstanceMethods
0
- class_eval <<-CLASS, __FILE__, __LINE__
0
- all :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
0
- first :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
0
- alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
0
- alias_method :can_has_tree, :is_a_tree # just for fun ;)
0
- module InstanceMethods
0
- # Returns list of ancestors, starting with the root.
0
- # grandchild1.ancestors # => [root, child]
0
- node, nodes = self, []
0
- nodes << node = node.parent while node.parent
0
- # Returns the root node of the current node’s tree.
0
- # grandchild1.root # => root
0
- node = node.parent while node.parent
0
- # Returns all siblings of the current node.
0
- # grandchild1.siblings # => [grandchild2]
0
- # Returns all children of the current node’s parent.
0
- # grandchild1.generation # => [grandchild1, grandchild2]
0
- parent ? parent.children : self.class.roots
0
- alias_method :self_and_siblings, :generation # for those used to the ActiveRecord acts_as_tree
Comments
No one has commented yet.