forked from take-five/acts_as_ordered_tree
/
instance_methods.rb
373 lines (308 loc) · 10.8 KB
/
instance_methods.rb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# coding: utf-8
module ActsAsOrderedTree
module InstanceMethods
extend ActiveSupport::Concern
# Returns true if this is a root node.
def root?
self[parent_column].nil?
end
# Returns true if this is the end of a branch.
def leaf?
persisted? && if children_counter_cache_column
self[children_counter_cache_column] == 0
else
children.count == 0
end
end
def branch?
!leaf?
end
# Returns true is this is a child node
def child?
!root?
end
# Returns root (not really fast operation)
def root
root? ? self : parent.root
end
# Returns the array of all parents and self starting from root
def self_and_ancestors
# 1. recursively load ancestors
nodes = []
node = self
while node
nodes << node
node = node.parent
end
# 2. first ancestor is a root
nodes.reverse!
# 3. create fake scope
ActsAsOrderedTree::FakeScope.new(self.class, nodes, :where => {:id => nodes.map(&:id)})
end
# Returns the array of all parents starting from root
def ancestors
records = self_and_ancestors - [self]
scope = self_and_ancestors.where(arel[:id].not_eq(id))
ActsAsOrderedTree::FakeScope.new(scope, records)
end
# Returns the array of all children of the parent, including self
def self_and_siblings
ordered_tree_scope.where(parent_column => self[parent_column])
end
# Returns the array of all children of the parent, except self
def siblings
self_and_siblings.where(arel[:id].not_eq(id))
end
def level
if depth_column
# cached result becomes invalid when parent is changed
if new_record? ||
changed_attributes.include?(parent_column.to_s) ||
self[depth_column].blank?
self[depth_column] = compute_level
else
self[depth_column]
end
else
compute_level
end
end
# Returns a set of all of its children and nested children.
# A little bit tricky. use RDBMS with recursive queries support (PostgreSQL)
def descendants
records = fetch_self_and_descendants - [self]
ActsAsOrderedTree::FakeScope.new self.class, records, :where => {:id => records.map(&:id)}
end
# Returns a set of itself and all of its nested children
def self_and_descendants
records = fetch_self_and_descendants
ActsAsOrderedTree::FakeScope.new self.class, records, :where => {:id => records.map(&:id)}
end
def is_descendant_of?(other)
ancestors.include? other
end
def is_or_is_descendant_of?(other)
self == other || is_descendant_of?(other)
end
def is_ancestor_of?(other)
other.is_descendant_of? self
end
def is_or_is_ancestor_of?(other)
other.is_or_is_descendant_of? self
end
# Return +true+ if this object is the first in the list.
def first?
self[position_column] <= 1
end
# Return +true+ if this object is the last in the list.
def last?
!right_sibling
end
# Returns a left (upper) sibling of the node
def left_sibling
siblings.
where( arel[position_column].lt(self[position_column]) ).
reorder( arel[position_column].desc ).
first
end
alias higher_item left_sibling
# Returns a right (lower) sibling of the node
def right_sibling
siblings.
where( arel[position_column].gt(self[position_column]) ).
reorder( arel[position_column].asc ).
first
end
alias lower_item right_sibling
# Insert the item at the given position (defaults to the top position of 1).
# +acts_as_list+ compatability
def insert_at(position = 1)
move_to_child_with_index(parent, position - 1)
end
# Shorthand method for finding the left sibling and moving to the left of it.
def move_left
move_to_left_of left_sibling
end
alias move_higher move_left
# Shorthand method for finding the right sibling and moving to the right of it.
def move_right
move_to_right_of right_sibling
end
alias move_lower move_right
# Move the node to the left of another node
def move_to_left_of(node)
move_to node, :left
end
alias move_to_above_of move_to_left_of
# Move the node to the left of another node
def move_to_right_of(node)
move_to node, :right
end
alias move_to_bottom_of move_to_right_of
# Move the node to the child of another node
def move_to_child_of(node)
move_to node, :child
end
# Move the node to the child of another node with specify index
def move_to_child_with_index(node, index)
raise ActiveRecord::ActiveRecordError, "index cant be nil" unless index
new_siblings = node.try(:children) || self.class.roots.delete_if { |root_node| root_node == self }
if new_siblings.empty?
node ? move_to_child_of(node) : move_to_root
elsif new_siblings.count <= index
move_to_right_of(new_siblings.last)
elsif
index >= 0 ? move_to_left_of(new_siblings[index]) : move_to_right_of(new_siblings[index])
end
end
# Move the node to root nodes
def move_to_root
move_to nil, :root
end
# Returns +true+ it is possible to move node to left/right/child of +target+
def move_possible?(target)
same_scope?(target) && !is_or_is_ancestor_of?(target)
end
# Check if other model is in the same scope
def same_scope?(other)
scope_column_names.empty? || scope_column_names.all? do |attr|
self[attr] == other[attr]
end
end
private
# reloads relevant ordered_tree columns
def reload_node #:nodoc:
reload(
:select => [parent_column,
position_column,
depth_column,
children_counter_cache_column].compact,
:lock => true
)
end
def compute_level #:nodoc:
ancestors.count
end
def compute_ordered_tree_columns(target, pos) #:nodoc:
case pos
when :root then
parent_id = nil
position = if root? && self[position_column]
# already root node
self[position_column]
else
ordered_tree_scope.roots.maximum(position_column).try(:succ) || 1
end
depth = 0
when :left then
parent_id = target[parent_column]
position = target[position_column]
position -= 1 if target[parent_column] == send("#{parent_column}_was") && target[position_column] > position_was # right
depth = target.level
when :right then
parent_id = target[parent_column]
position = target[position_column]
position += 1 if target[parent_column] != send("#{parent_column}_was") || target[position_column] < position_was # left
depth = target.level
when :child then
parent_id = target.id
position = if self[parent_column] == parent_id && self[position_column]
# already children of target node
self[position_column]
else
target.children.maximum(position_column).try(:succ) || 1
end
depth = target.level + 1
else raise ActiveRecord::ActiveRecordError, "Position should be :child, :left, :right or :root ('#{pos}' received)."
end
return parent_id, position, depth
end
# This method do real node movements
def move_to(target, pos) #:nodoc:
if target.is_a? self.class.base_class
target.reload
elsif pos != :root && target
# load object if node is not an object
target = self.class.find(target)
end
unless pos == :root || target && move_possible?(target)
raise ActiveRecord::ActiveRecordError, "Impossible move"
end
position_was = send "#{position_column}_was".intern
parent_id_was = send "#{parent_column}_was".intern
parent_id, position, depth = compute_ordered_tree_columns(target, pos)
# nothing changed - quit
return if parent_id == parent_id_was && position == position_was
update = proc do
decrement_lower_positions parent_id_was, position_was if position_was
increment_lower_positions parent_id, position
columns = {parent_column => parent_id, position_column => position}
columns[depth_column] = depth if depth_column
ordered_tree_scope.update_all(columns, :id => id)
reload_node
end
move_kind = case
when id_was && parent_id != parent_id_was then :move
when id_was && position != position_was then :reorder
else nil
end
if move_kind
run_callbacks move_kind, &update
else
update.call
end
end
def decrement_lower_positions(parent_id, position) #:nodoc:
conditions = arel[parent_column].eq(parent_id).and(arel[position_column].gt(position))
ordered_tree_scope.update_all "#{position_column} = #{position_column} - 1", conditions
end
def increment_lower_positions(parent_id, position) #:nodoc:
conditions = arel[parent_column].eq(parent_id).and(arel[position_column].gteq(position))
ordered_tree_scope.update_all "#{position_column} = #{position_column} + 1", conditions
end
# recursively load descendants
def fetch_self_and_descendants #:nodoc:
@self_and_descendants ||= [self] + children.map { |child| [child, child.descendants] }.flatten
end
def set_depth! #:nodoc:
self[depth_column] = compute_level
end
def set_scope! #:nodoc:
scope_column_names.each do |column|
self[column] = parent[column]
end
end
def flush_descendants #:nodoc:
@self_and_descendants = nil
end
def update_descendants_depth #:nodoc:
depth_was = self[depth_column]
yield
diff = self[depth_column] - depth_was
if diff != 0
sign = diff > 0 ? "+" : "-"
# update categories set depth = depth - 1 where id in (...)
descendants.update_all(["#{depth_column} = #{depth_column} #{sign} ?", diff.abs])
end
end
def update_counter_cache #:nodoc:
parent_id_was = self[parent_column]
yield
parent_id_new = self[parent_column]
unless parent_id_new == parent_id_was
self.class.increment_counter(children_counter_cache_column, parent_id_new) if parent_id_new
self.class.decrement_counter(children_counter_cache_column, parent_id_was) if parent_id_was
end
end
def arel #:nodoc:
self.class.arel_table
end
def ordered_tree_scope
if scope_column_names.empty?
self.class.base_class.scoped
else
self.class.base_class.where Hash[scope_column_names.map { |column| [column, self[column]] }]
end
end
end # module InstanceMethods
end # module ActsAsOrderedTree