Giter Site home page Giter Site logo

lorddoig / mongoid-tree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from benedikt/mongoid-tree

0.0 1.0 0.0 381 KB

A tree structure for Mongoid documents using the materialized path pattern

Home Page: http://benedikt.github.com/mongoid-tree

License: MIT License

Ruby 100.00%

mongoid-tree's Introduction

mongoid-tree Build Status Dependency Status

A tree structure for Mongoid documents using the materialized path pattern

Requirements

  • mongoid (~> 3.0)

For a mongoid 2.x compatible version, please use mongoid-tree 0.7.x!

Install

To install mongoid_tree, simply add it to your Gemfile:

gem 'mongoid-tree', :require => 'mongoid/tree'

In order to get the latest development version of mongoid-tree:

gem 'mongoid-tree', :git => 'git://github.com/benedikt/mongoid-tree', :require => 'mongoid/tree'

You might want to remove the :require => 'mongoid/tree' option and explicitly require 'mongoid/tree' where needed and finally run

bundle install

Usage

Read the API documentation at http://benedikt.github.com/mongoid-tree and take a look at the Mongoid::Tree module

class Node
  include Mongoid::Document
  include Mongoid::Tree
end

Utility methods

There are several utility methods that help getting to other related documents in the tree:

Node.root
Node.roots
Node.leaves

node.root
node.parent
node.children
node.ancestors
node.ancestors_and_self
node.descendants
node.descendants_and_self
node.siblings
node.siblings_and_self
node.leaves

In addition it's possible to check certain aspects of the document's position in the tree:

node.root?
node.leaf?
node.depth
node.ancestor_of?(other)
node.descendant_of?(other)
node.sibling_of?(other)

See Mongoid::Tree for more information on these methods.

Ordering

Mongoid::Tree doesn't order children by default. To enable ordering of tree nodes include the Mongoid::Tree::Ordering module. This will add a position field to your document and provide additional utility methods:

node.lower_siblings
node.higher_siblings
node.first_sibling_in_list
node.last_sibling_in_list

node.move_up
node.move_down
node.move_to_top
node.move_to_bottom
node.move_above(other)
node.move_below(other)

node.at_top?
node.at_bottom?

Example:

class Node
  include Mongoid::Document
  include Mongoid::Tree
  include Mongoid::Tree::Ordering
end

See Mongoid::Tree::Ordering for more information on these methods.

Traversal

It's possible to traverse the tree using different traversal methods using the Mongoid::Tree::Traversal module.

Example:

class Node
  include Mongoid::Document
  include Mongoid::Tree
  include Mongoid::Tree::Traversal
end

node.traverse(:breadth_first) do |n|
  # Do something with Node n
end

Destroying

Mongoid::Tree does not handle destroying of nodes by default. However it provides several strategies that help you to deal with children of deleted documents. You can simply add them as before_destroy callbacks.

Available strategies are:

  • :nullify_children -- Sets the children's parent_id to null
  • :move_children_to_parent -- Moves the children to the current document's parent
  • :destroy_children -- Destroys all children by calling their #destroy method (invokes callbacks)
  • :delete_descendants -- Deletes all descendants using a database query (doesn't invoke callbacks)

Example:

class Node
  include Mongoid::Document
  include Mongoid::Tree

  before_destroy :nullify_children
end

Callbacks

There are two callbacks that are called before and after the rearranging process. This enables you to do additional computations after the documents position in the tree is updated. See Mongoid::Tree for details.

Example:

class Page
  include Mongoid::Document
  include Mongoid::Tree

  after_rearrange :rebuild_path

  field :slug
  field :path

  private

  def rebuild_path
    self.path = self.ancestors_and_self.collect(&:slug).join('/')
  end
end

Validations

Mongoid::Tree currently does not validate the document's children or parent associations by default. To explicitly enable validation for children and parent documents it's required to add a validates_associated validation.

Example:

class Node
  include Mongoid::Document
  include Mongoid::Tree

  validates_associated :parent, :children
end

Build Status

mongoid-tree is on Travis CI running the specs on Ruby Head, Ruby 1.9.3, JRuby (1.9 mode), and Rubinius (1.9 mode).

Known issues

See https://github.com/benedikt/mongoid-tree/issues

Repository

See https://github.com/benedikt/mongoid-tree and feel free to fork it!

Contributors

See a list of all contributors at https://github.com/benedikt/mongoid-tree/contributors. Thanks a lot everyone!

Support

If you like mongoid-tree and want to support the development, I would appreciate a small donation:

Pledgie

Flattr

Copyright

Copyright (c) 2010-2013 Benedikt Deicke. See LICENSE for details.

mongoid-tree's People

Contributors

benedikt avatar nickhoffman avatar jimneath avatar lorddoig avatar semaperepelitsa avatar tomk32 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.