Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 138 lines (100 sloc) 5.669 kb
91a7fb6 @ffmike code from existing svn repo
authored
1 = ActsAsOrderedTree
2 v.1.2
3 +----+-----------+----------+---------+
4 node_1 | id | parent_id | position | name |
5 \_ node_2 +----+-----------+----------+---------+
6 \_ node_3 | 1 | 0 | 1 | Node_1 |
7 | \_ node_4 | 2 | 1 | 1 | Node_2 |
8 | \_ node_5 | 3 | 1 | 2 | Node_3 |
9 | | \_ node_8 | 4 | 3 | 1 | Node_4 |
10 | | \_ node_9 | 5 | 3 | 2 | Node_5 |
11 | \_ node_10 | 6 | 1 | 3 | Node_6 |
12 | \_ node_11 | 7 | 1 | 4 | Node_7 |
13 \_ node_6 | 8 | 5 | 1 | Node_8 |
14 \_ node_7 | 9 | 5 | 2 | Node_9 |
15 | | 10 | 3 | 3 | Node_10 |
16 | | 11 | 3 | 4 | Node_11 |
17 node_12 | 12 | 0 | 2 | Node_12 |
18 \_ node_13 | 13 | 12 | 1 | Node_13 |
19 \_ node_14 | 14 | 12 | 2 | Node_14 |
20 | \_ node_15 | 15 | 14 | 1 | Node_15 |
21 | \_ node_16 | 16 | 14 | 2 | Node_16 |
22 | | \_ node_19 | 17 | 12 | 3 | Node_17 |
23 | | \_ node_20 | 18 | 12 | 4 | Node_18 |
24 | \_ node_21 | 19 | 16 | 1 | Node_19 |
25 | \_ node_22 | 20 | 16 | 2 | Node_20 |
26 \_ node_17 | 21 | 14 | 3 | Node_21 |
27 \_ node_18 | 22 | 14 | 4 | Node_22 |
28 +----+-----------+----------+---------+
29
30
31 class Person < ActiveRecord::Base
32 acts_as_ordered_tree :foreign_key => :parent_id,
33 :order => :position
34 end
35
36 class CreatePeople < ActiveRecord::Migration
37 def self.up
38 create_table :people do |t|
39 t.column :parent_id ,:integer ,:null => false ,:default => 0
40 t.column :position ,:integer
41 end
42 add_index(:people, :parent_id)
43 end
44 end
45
46 Which "in effect" sets up the following:
47
48 belongs_to :parent,
49 :class_name => Person,
50 :foreign_key => :parent_id
51
52 has_many :children,
53 :class_name => Person,
54 :foreign_key => :parent_id,
55 :order => :position
56
57
58
59 = Overview
60
61 Actions Tree Methods List Method
62 --------------------------------------------------------------------------------------------------
63 Create
64 To create a child object at a specific position,
65 use one of the following:
66 Person.create(:parent_id => parent.id, :position => 2)
67 parent.children << Person.new(:position => 3)
68 parent.children.create(:position => 5)
69
70 To create a new 'root', use:
71 Person.create(:position => 2)
72
73 :position will default to the bottom of the parent's list
74 :parent_id defaults to 0 (Class.roots)
75
76 Read
77 roots (class method) self_and_siblings
78
79 root siblings
80
81 parent position_in_list
82
83 ancestors
84
85 children
86
87 descendants
88
89 Update
90 shift_to(parent = nil, position = nil) move_above(sibling = nil)
91
92 orphan move_higher
93
94 orphan_children move_lower
95
96 parent_adopts_children move_to_top
97
98 orphan_self_and_children move_to_bottom
99
100 orphan_self_and_parent_adopts_children
101
102 Destroy
103 destroy (deletes all descendants)
104
105 destroy_and_orphan_children
106
107 destroy_and_parent_adopts_children
108
109 = Install
110
111 ./script/plugin install svn://rubyforge.org/var/svn/ordered-tree/acts_as_ordered_tree
112
113 To test, run 'rake test:plugins', or 'rake' from the plugin's directory.
114
115 There is a drag-n-drop demo located at svn://rubyforge.org/var/svn/ordered-tree/demo
116
117 = License
118
119 Copyright (c) 2006,2007 Brian D. Burns <wizard.rb@gmail.com>
120
121 Permission is hereby granted, free of charge, to any person obtaining a copy of
122 this software and associated documentation files (the "Software"), to deal in
123 the Software without restriction, including without limitation the rights to
124 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
125 the Software, and to permit persons to whom the Software is furnished to do so,
126 subject to the following conditions:
127
128 The above copyright notice and this permission notice shall be included in all
129 copies or substantial portions of the Software.
130
131 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
132 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
133 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
134 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
135 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
136 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
137
Something went wrong with that request. Please try again.