Coder Social home page Coder Social logo

arboreal's Introduction

Arboreal

Arboreal is yet another extension to ActiveRecord to support tree-shaped data structures.

Arboreal surfaces relationships within the tree like children, ancestors, descendants, and siblings as scopes, so that additional filtering/pagination can be performed.

It delegates as much work as possible to the underlying DBMS, making it efficient to:

  • fetch all ancestors, descendants or siblings of a node

  • move nodes (or subtrees) around

  • prevent loops

  • rebuild the hierarchy

Getting started

First, install the “arboreal” gem, and add it to your Rails project’s config/environment.rb.

Next, you’ll need a migration to add parent_id and ancestry_string columns, and indices:

class MakeThingsArboreal < ActiveRecord::Migration

  def self.up
    add_column "things", "parent_id", :integer
    add_index "things", ["parent_id"]
    add_column "things", "ancestry_string", :string
    add_index "things", ["ancestry_string"]
  end

  def self.down
    remove_index "things", ["ancestry_string"]
    remove_column "things", "ancestry_string"
    remove_index "things", ["parent_id"]
    remove_column "things", "parent_id"
  end

end

Finally, you can declare your model arboreal:

class Thing < ActiveRecord::Base

  acts_arboreal

  # .. etc etc ...

end

Navigating the tree

Arboreal adds the basic relationships you’d expect:

  • parent

  • children

In addition, it provides the following handy methods on each tree-node:

  • ancestors

  • descendants

  • subtree (the node itself, plus descendants)

  • siblings

  • root (the topmost ancestor)

The first four return scopes, to which additional filtering, ordering or limits may be applied.

At the class-level:

  • roots is a named-scope returning all the nodes without parents

  • rebuild_ancestry rebuilds the ancestry cache, as described below

Rebuilding the ancestry cache

Internally, Arboreal uses the ancestry_string column to cache the path down the tree to each node (or more correctly, it’s parent. This technique - a variant of “path enumeration” or “materialized paths” - allows efficient retrieval of both ancestors and descendants.

It’s conceivable that the computed ancestry-string values may get out of whack, particularly if changes are made directly to the database. If you suspect corruption, you can restore sanity using rebuild_ancestry, e.g

Thing.rebuild_ancestry

The ancestry rebuild is implemented in SQL to leverage the underlying DBMS, and so is pretty efficient, even on large trees.

arboreal's People

Contributors

mdub avatar yob avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

arboreal's Issues

Support newer Rails versions

Hi!

I'm in the process of upgrading an old Rails application from Rails 3.2. It depends on arboreal which doesn't support newer versions at the moment. I've seen #4 which contains a lot of good work to upgrade but needs splitting up.

I have three options:

Switching to that Arboreal fork would involve renaming a database column and incorporating changes that may not be well tested. If I have to change the database, I may as well switch to Ancestry. So really, I'm weighing up between switching to Ancestry and fixing up Arboreal's Rails compatibility myself.

What is your vision for this gem? Would you like to release new versions that are not compatible with old Ruby and old Rails? Do you see advantages of Arboreal over Ancestry? @mdub

How do I run tests?

I’ve cloned the repo, ran bundle install, then rake spec and got this:

    ~/tmp/arboreal Ξ  rake spec
/home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/options.rb:188:in `colour=': Use RbConfig instead of obsolete and deprecated Config.
/home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/options.rb:188:in `colour=': Use RbConfig instead of obsolete and deprecated Config.
/home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:2002:in `method_missing'/home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:2449: warning: already initialized constant Class::VALID_FIND_OPTIONS
/home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:2449: warning: previous definition of VALID_FIND_OPTIONS was here
: undefined method `alias_method_chain' for #<Class:0xacfacc4> (NoMethodError)
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/validations.rb:387:in `block in included'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/validations.rb:386:in `class_eval'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/validations.rb:386:in `included'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:3210:in `include'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:3210:in `block in <module:ActiveRecord>'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:3208:in `class_eval'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:3208:in `<module:ActiveRecord>'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/activerecord-2.3.18/lib/active_record/base.rb:5:in `<top (required)>'
    from /home/vlad/tmp/arboreal/spec/spec_helper.rb:11:in `<top (required)>'
    from spec/arboreal_spec.rb:1:in `require'
    from spec/arboreal_spec.rb:1:in `<top (required)>'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/example_group_runner.rb:15:in `load'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/example_group_runner.rb:15:in `block in load_files'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/example_group_runner.rb:14:in `each'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/example_group_runner.rb:14:in `load_files'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/options.rb:134:in `run_examples'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/runner/command_line.rb:9:in `run'
    from /home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/bin/spec:5:in `<main>'
rake aborted!
Command /home/vlad/.rvm/rubies/ruby-2.1.2/bin/ruby -I"lib:lib:spec"  "/home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/bin/spec" "spec/arboreal_spec.rb" -fnested --color failed
/home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/rake/spectask.rb:177:in `block (2 levels) in define'
/home/vlad/.rvm/gems/ruby-2.1.2/gems/rspec-1.3.2/lib/spec/rake/spectask.rb:153:in `block in define'
/home/vlad/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/home/vlad/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => spec
(See full trace by running task with --trace)
    ~/tmp/arboreal Ξ  

How do I do this correctly? :-)

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.