Module DataMapper::Is::Tree::ClassMethods
In: lib/data_mapper/is/tree.rb
lib/data_mapper/is/tree.rb

An extension to DataMapper to easily allow the creation of tree structures from your DataMapper Models. This requires a foreign key property for your model, which by default would be called :parent_id.

  Example:

  class Category < DataMapper::Base
                   property :parent_id, :integer
                   property :name, :string

    is_a_tree :order => "name"
  end

  root
    +- child
         +- grandchild1
         +- grandchild2

  root        = Category.create("name" => "root")
  child       = root.children.create("name" => "child")
  grandchild1 = child1.children.create("name" => "grandchild1")
  grandchild2 = child2.children.create("name" => "grandchild2")

  root.parent   # => nil
  child.parent  # => root
  root.children # => [child]
  root.children.first.children.first # => grandchild1
  Category.first_root  # => root
  Category.roots       # => [root]

The following instance methods are added:

  • children - Returns all nodes with the current node as their parent, in the order specified by :order ([grandchild1, grandchild2] when called on child)
  • parent - Returns the node referenced by the foreign key (:parent_id by default) (root when called on child)
  • siblings - Returns all the children of the parent, excluding the current node ([grandchild2] when called on grandchild1)
  • generation - Returns all the children of the parent, including the current node ( [grandchild1, grandchild2] when called on grandchild1)
  • ancestors - Returns all the ancestors of the current node ([root, child1] when called on grandchild2)
  • root - Returns the root of the current node (root when called on grandchild2)
Author:Timothy Bennett (lanaer.com)

Methods

Included Modules

DataMapper::Is::Tree::InstanceMethods DataMapper::Is::Tree::InstanceMethods

External Aliases

first_root -> root
first_root -> root

Public Instance methods

can_has_tree(options = {})

Alias for is_a_tree

can_has_tree(options = {})

Alias for is_a_tree

Configuration options are:

  • foreign_key - specifies the column name to use for tracking of the tree (default: parent_id)
  • order - makes it possible to sort the children according to this SQL snippet.
  • counter_cache - keeps a count in a children_count column if set to true (default: false).

[Source]

    # File lib/data_mapper/is/tree.rb, line 57
57:         def is_a_tree(options = {})
58:           configuration = { :foreign_key => "parent_id" }
59:           configuration.update(options) if options.is_a?(Hash)
60: 
61:           belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
62:           has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order]
63: 
64:                                         include DataMapper::Is::Tree::InstanceMethods
65: 
66:                                         class_eval "def self.roots\nself.all :\#{configuration[:foreign_key]} => nil, :order => \#{configuration[:order].inspect}\nend\n\ndef self.first_root\nself.first :\#{configuration[:foreign_key]} => nil, :order => \#{configuration[:order].inspect}\nend\n"
67: 
68:                                         class << self
69:                                                 alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
70:                                         end
71:         end

Configuration options are:

  • foreign_key - specifies the column name to use for tracking of the tree (default: parent_id)
  • order - makes it possible to sort the children according to this SQL snippet.
  • counter_cache - keeps a count in a children_count column if set to true (default: false).

[Source]

    # File lib/data_mapper/is/tree.rb, line 57
57:         def is_a_tree(options = {})
58:           configuration = { :foreign_key => "parent_id" }
59:           configuration.update(options) if options.is_a?(Hash)
60: 
61:           belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
62:           has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order]
63: 
64:                                         include DataMapper::Is::Tree::InstanceMethods
65: 
66:                                         class_eval "def self.roots\nself.all :\#{configuration[:foreign_key]} => nil, :order => \#{configuration[:order].inspect}\nend\n\ndef self.first_root\nself.first :\#{configuration[:foreign_key]} => nil, :order => \#{configuration[:order].inspect}\nend\n"
67: 
68:                                         class << self
69:                                                 alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
70:                                         end
71:         end

[Validate]