Project

dm-is-tree

0.02
Repository is archived
No commit activity in last 3 years
No release in over 3 years
DataMapper plugin allowing the creation of tree structures from data models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6.4
~> 0.9.2
~> 1.3.2

Runtime

~> 1.2.0
 Project Readme

dm-is-tree¶ ↑

DataMapper plugin enabling easy creation of tree structures from your DM models.

This requires a foreign key property for your model, which by default would be called :parent_id.

Installation¶ ↑

Stable¶ ↑

Install the dm-is-tree gem.

$ (sudo)? gem install dm-is-tree

Edge¶ ↑

Download or clone dm-is-versioned from Github.

$ cd /path/to/dm-is-tree

$ rake install            # will install dm-is-tree

Getting started¶ ↑

To start using this gem, just…

require 'dm-is-tree'

Lets say we have a Category model…

class Category
  include DataMapper::Resource
  property :id,         Serial
  property :name,       String
end

…and we want to have a tree structure within it, something like this:

the_parent
  +- child
      +- grandchild1
      +- grandchild2

To achieve this we just add the following to the model:

class Category
  <snip...>

  is :tree, :order => :name

end

# No need to define the :parent_id property, it will be added automatically
property :parent_id,  Integer

This will automatically add the following to your model:

Instance Methods¶ ↑

  • #parent / #parent=

  • #children / #children=

  • #siblings

  • #generation

  • #ancestors

    • also aliased as #self_and_siblings for those used to AR’s :acts_as_tree

  • #root

    • also aliased as #first_root for those used to AR’s :acts_as_tree

Class Methods¶ ↑

  • #first_root

  • #roots

Configuration Options¶ ↑

Before we go onto the usage examples, a few quick words about configuration options available:

:child_key¶ ↑

Specifies the column name to use for tracking of the tree (default: #parent_id).

class Category
  <snip...>

  is :tree, :child_key => :some_other_foreign_key_id

end

:model¶ ↑

Specifies the name of the Model to use for the tree (default: Model class name defined in)

class Category
  <snip...>

  is :tree, :model => 'SomeStrangeModelName'

end

:order [Optional]¶ ↑

Specifies the sort order of the children when retrieving them (default: not present)

class Category
  <snip...>

  is :tree, :order => [:updated_at, :name]

end

Usage¶ ↑

To create the above structure, we would start with:

the_parent = Category.create(:name => "the_parent")

#parent & #parent=¶ ↑

The #parent instance method returns the node referenced by the foreign key - :parent_id or the defined :child_key.

# by default #parent and #parent_id return nil when there is no parent
the_parent.parent  # => nil

The #parent= instance method sets the :parent_id foreign key to the parent’s id attribute value.

To define a parent you can use either of these syntaxes:

a_child = Category.create(:name => "a_child", :parent => the_parent)

a_child = Category.create(:name => "a_child", :parent_id => the_parent.id)

a_child = Category.create(:name => "a_child")
a_child.parent = the_parent

When retrieving the parent, you will receive the full parent object ( or nil if none was declared )

a_child.parent  # => the_parent

#children & #children=¶ ↑

The #children instance method returns all nodes with the current node as their parent, in the order specified by the :order configuration option.

# by default #children return an empty Array when there are no children
a_child.children # => []

# or an Array with child objects when there are children
the_parent.children # => [a_child]

The #children= instance method adds children by setting the :parent_id foreign key to the parent’s id attribute value.

To add a child you can use either of these syntaxes:

child = the_parent.children.create(:name => "child")

grandchild1 = Category.create(:name => "grandchild1")
child.children << grandchild1

grandchild2 = child.children.create(:name => "grandchild2")

When retrieving children, or a child, you will receive an Array of child objects.

child.children # => [grandchild1, grandchild2,...]

# just retrieve the first child
child.children.first # => grandchild1

#siblings¶ ↑

The #siblings instance method returns all the children of the parent, excluding the current node.

# by default #siblings return an empty Array when there are no siblings
the_parent.siblings # => []

grandchild1.siblings # => [grandchild2]

#generation¶ ↑

The #generation instance method returns all the children of the parent, including the current node.

# by default #generation return an Array with itself only when it's a 'lonely child'
the_parent.generation # => [the_parent]

#
grandchild1.generation # => [grandchild1, grandchild2]

#ancestors¶ ↑

The #ancestors instance method returns all the ancestors of the current node.

# by default it returns an empty Array when born through immaculate conception (is root)
the_parent.ancestors # => []

grandchild2.ancestors # => [the_parent, a_child]

#root¶ ↑

The #root instance method returns the root (parent) of the current node.

# by default returns itself only when it's the root node
the_parent.root # =>  the_parent

a_child.root # => the_parent

grandchild2.root # => the_parent

self.#first_root¶ ↑

The #first_root class method returns the first root declared in the model.

Category.first_root # =>  the_parent

self.#roots¶ ↑

The #roots class method returns an Array of the roots declared in the model.

Category.roots # =>  [the_parent]

parent2 = Category.create(:name => 'parent2')

Category.roots # =>  [the_parent, parent2]

Summary¶ ↑

parent = Category.create(:name => "parent")

child = parent.children.create(:name => "child")

grandchild1 = child.children.create(:name => "grandchild1")

grandchild2 = Category.create(:name => "grandchild2")
child.children << grandchild2

grandchild3 = Category.create(:name => "grandchild2")
grandchild3.parent = child

parent.parent  # => nil
child.parent  # => parent

parent.children  # => [child]
parent.children.first.children.first  # => grandchild1

parent.siblings # => []
grandchild1.siblings # => [grandchild2]

parent.generation # => [parent]
grandchild1.generation # => [grandchild1, grandchild2]

parent.ancestors # => []
grandchild2.ancestors # => [parent, child]

parent.root # =>  parent
parent.root # => parent
grandchild2.root # => parent

Category.first_root  # => parent
Category.roots  # => [parent]

Gotchas¶ ↑

Now there are some gotcha’s that might not be entirely obvious to everyone, so let’s clarify them here.

Prevent a node being made a child of it self¶ ↑

By default dm-is-tree allows you to save a record as a child of it self, which is quite unnatural. To prevent this, I would humbly suggest adding this custom validation code to your model(s).

class Category
  <snip...>

  # prevent saving Category as child of self, except when new?
  validates_with_method :parent_id,
                        :method => :category_cannot_be_made_a_child_of_self,
                        :unless => :new?

  protected

    def category_cannot_be_made_a_child_of_self
      if self.id === self.parent_id
        return [
          false,
          "A Category [ #{self.name} ] cannot be made a child of it self [ #{self.name} ]"
        ]
      else
        return true
      end
    end

end

An example:

parent = Category.create(:name => "parent")
child = parent.children.create(:name => 'child')

child.parent = child

child.save  # => return false

child.errors.on(:parent_id)
  # => ["A Category [ child ] cannot be made a child of it self [ child ]"]

Sorting order within nodes¶ ↑

By default the sorting order is alphabetic, but this spans the entire table (with all nodes), which might not be what you want.

To prevent this, order the results by :parent_id first, and secondly by :name or whatever you wish to sort by.

class Category
  <snip...>

  is :tree, :order => [:parent_id, :name]

end

That’s about it.

Errors / Bugs¶ ↑

If something is not behaving intuitively, it is a bug, and should be reported. Report it here: datamapper.lighthouseapp.com/

TODOs¶ ↑

  • Make it automatically prevent saving self as child of self.

  • Anything else missing?

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so we don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history.

    • (if you want to have your own version, that is fine but bump version in a commit by itself we can ignore when we pull)

  • Send us a pull request. Bonus points for topic branches.

Copyright © 2011 Timothy Bennett. Released under the MIT License.

See LICENSE for details.

Credits¶ ↑

Credit also goes to these contributors.

Current Maintainer: Garrett Heaver (www.linkedin.com/pub/dir/garrett/heaver)