acts_as_graph
acts_as_graph
The acts_as_graph plugin extends ActiveRecord to implement a connected graph.
WARNING: This plugin hasn’t been worked on in about a year, and hasn’t been tested with Rails 1.2. I plan on updating it some time in the future, but for now, use at your own risk. Contributions/bug fixes welcome.
Overview
Specify this act if you want to model a graph structure by providing an inbound association and an outbound association. This act requires that you have an edge table (used in the HABTM relationship), which by default is called CLASS_edges, which has two columns (child_id and parent_id by default) where CLASS is the name of your model.
Currently, only DAGs (Directed, Acyclic graphs) are supported.
See here and
here for more information.
class Task < ActiveRecord::Base
acts_as_graph :edge_table => "dependencies"
end
Example :
task1
+- task2
| +- task3
| \- task4
\- task3
task1 = Task.new(:name => "Task 1")
task2 = Task.new(:name => "Task 2")
task3 = Task.new(:name => "Task 3")
task4 = Task.new(:name => "Task 4")
task1.children << [task2, task3]
task2.children << task3
task2.children << task
task1.parents => []
task3.parents => [task1, task2]
task1.children => [task2, task3]
task1.children.recursive.to_a => [task2, task3, task4]
task1.children.recursive.each { |child| child.spank } => nil
The recursive object (of the Recursive class) is added to the parents and children associations,
and represents a DFS on those collections.
When coerced into an array, it gathers all of the child or parent records recursively (obviously) into a single array.
When each is called on the recursive object, it yields against each record in turn. This means
that some operations (such as include?) will be faster when run with the each implementation.
The following options are supported, but some have yet to be implemented:
|^.edge_table | HABTM table that represents graph edges. Defaults to class_name_id. |
|^. parent_col | Column in edge_table that references the parent node. Defaults to parent_id.|
|^. child_col | Column in edge_table that references the child node. Defaults to child_id.|
|^. child_collection | Name of the child collection. Defaults to children.|
|^. parent_collection | Name of the child collection. Defaults to parents.|
|^. allow_cycles | Determines whether or not the graph is cyclic. Defaults to false. Cyclic graphs are not yet implemented.|
|^. directed | Determines whether or not the graph is directed. Defaults to true. Undirected graphs are not yet implemented.|
Future directions
The following features are being worked on for this plugin:
- Cyclic and undirected graphs.
- DFS/BFS choices for the recursive object.
- Converting the recursive object to a proxy (such as the how the other association proxies are done).
Download
You can get acts_as_graph via cvs. From the rails project directory, run:
./script/plugin install -x svn://rubyforge.com/var/svn/acts-as-graph
Fork and edit this post on Github.
Co-Founder of