-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
137 lines (100 loc) · 5.54 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
= 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 <[email protected]>
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.