Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
138 lines (100 sloc) 5.54 KB
= ActsAsOrderedTree
v.1.2
+----+-----------+----------+---------+
node_1 | id | parent_id | position | name |
\_ node_2 +----+-----------+----------+---------+
\_ node_3 | 1 | 0 | 1 | Node_1 |
| \_ node_4 | 2 | 1 | 1 | Node_2 |
| \_ node_5 | 3 | 1 | 2 | Node_3 |
| | \_ node_8 | 4 | 3 | 1 | Node_4 |
| | \_ node_9 | 5 | 3 | 2 | Node_5 |
| \_ node_10 | 6 | 1 | 3 | Node_6 |
| \_ node_11 | 7 | 1 | 4 | Node_7 |
\_ node_6 | 8 | 5 | 1 | Node_8 |
\_ node_7 | 9 | 5 | 2 | Node_9 |
| | 10 | 3 | 3 | Node_10 |
| | 11 | 3 | 4 | Node_11 |
node_12 | 12 | 0 | 2 | Node_12 |
\_ node_13 | 13 | 12 | 1 | Node_13 |
\_ node_14 | 14 | 12 | 2 | Node_14 |
| \_ node_15 | 15 | 14 | 1 | Node_15 |
| \_ node_16 | 16 | 14 | 2 | Node_16 |
| | \_ node_19 | 17 | 12 | 3 | Node_17 |
| | \_ node_20 | 18 | 12 | 4 | Node_18 |
| \_ node_21 | 19 | 16 | 1 | Node_19 |
| \_ node_22 | 20 | 16 | 2 | Node_20 |
\_ node_17 | 21 | 14 | 3 | Node_21 |
\_ node_18 | 22 | 14 | 4 | Node_22 |
+----+-----------+----------+---------+
class Person < ActiveRecord::Base
acts_as_ordered_tree :foreign_key => :parent_id,
:order => :position
end
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.column :parent_id ,:integer ,:null => false ,:default => 0
t.column :position ,:integer
end
add_index(:people, :parent_id)
end
end
Which "in effect" sets up the following:
belongs_to :parent,
:class_name => Person,
:foreign_key => :parent_id
has_many :children,
:class_name => Person,
:foreign_key => :parent_id,
:order => :position
= Overview
Actions Tree Methods List Method
--------------------------------------------------------------------------------------------------
Create
To create a child object at a specific position,
use one of the following:
Person.create(:parent_id => parent.id, :position => 2)
parent.children << Person.new(:position => 3)
parent.children.create(:position => 5)
To create a new 'root', use:
Person.create(:position => 2)
:position will default to the bottom of the parent's list
:parent_id defaults to 0 (Class.roots)
Read
roots (class method) self_and_siblings
root siblings
parent position_in_list
ancestors
children
descendants
Update
shift_to(parent = nil, position = nil) move_above(sibling = nil)
orphan move_higher
orphan_children move_lower
parent_adopts_children move_to_top
orphan_self_and_children move_to_bottom
orphan_self_and_parent_adopts_children
Destroy
destroy (deletes all descendants)
destroy_and_orphan_children
destroy_and_parent_adopts_children
= Install
./script/plugin install svn://rubyforge.org/var/svn/ordered-tree/acts_as_ordered_tree
To test, run 'rake test:plugins', or 'rake' from the plugin's directory.
There is a drag-n-drop demo located at svn://rubyforge.org/var/svn/ordered-tree/demo
= License
Copyright (c) 2006,2007 Brian D. Burns <wizard.rb@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Jump to Line
Something went wrong with that request. Please try again.