awesome_nested_set 2.1.3 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
2.1.4
|
2
|
+
* nested_set_options accept both Class & AR Relation. [Semyon Perepelitsa]
|
3
|
+
* Reduce the number of queries triggered by the canonical usage of `i.level` in the `nested_set` helpers. [thedarkone]
|
4
|
+
* Specifically require active_record [Bogdan Gusiev]
|
5
|
+
* compute_level now checks for a non nil association target. [Joel Nimety]
|
6
|
+
|
1
7
|
2.1.3
|
2
8
|
* Update child depth when parent node is moved. [Amanda Wagener]
|
3
9
|
* Added move_to_child_with_index. [Ben Zhang]
|
data/lib/awesome_nested_set.rb
CHANGED
@@ -259,6 +259,16 @@ module CollectiveIdea #:nodoc:
|
|
259
259
|
children.each { |c| yield(c, path.length-1) }
|
260
260
|
end
|
261
261
|
end
|
262
|
+
|
263
|
+
def associate_parents(objects)
|
264
|
+
id_indexed = objects.index_by(&:id)
|
265
|
+
objects.each do |object|
|
266
|
+
if !(association = object.association(:parent)).loaded? && (parent = id_indexed[object.parent_id])
|
267
|
+
association.target = parent
|
268
|
+
association.set_inverse_instance(parent)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
262
272
|
end
|
263
273
|
|
264
274
|
# Any instance method that returns a collection makes use of Rails 2.1's named_scope (which is bundled for Rails 2.0), so it can be treated as a finder.
|
@@ -338,7 +348,7 @@ module CollectiveIdea #:nodoc:
|
|
338
348
|
# Returns the level of this object in the tree
|
339
349
|
# root level is 0
|
340
350
|
def level
|
341
|
-
parent_id.nil? ? 0 :
|
351
|
+
parent_id.nil? ? 0 : compute_level
|
342
352
|
end
|
343
353
|
|
344
354
|
# Returns a set of itself and all of its nested children
|
@@ -444,6 +454,14 @@ module CollectiveIdea #:nodoc:
|
|
444
454
|
end
|
445
455
|
|
446
456
|
protected
|
457
|
+
def compute_level
|
458
|
+
node, nesting = self, 0
|
459
|
+
while (association = node.association(:parent)).loaded? && association.target
|
460
|
+
nesting += 1
|
461
|
+
node = node.parent
|
462
|
+
end
|
463
|
+
node == self ? ancestors.count : node.level + nesting
|
464
|
+
end
|
447
465
|
|
448
466
|
def without_self(scope)
|
449
467
|
scope.where(["#{self.class.quoted_table_name}.#{self.class.primary_key} != ?", self])
|
@@ -487,7 +505,7 @@ module CollectiveIdea #:nodoc:
|
|
487
505
|
|
488
506
|
# on creation, set automatically lft and rgt to the end of the tree
|
489
507
|
def set_default_left_and_right
|
490
|
-
highest_right_row = nested_set_scope(:order => "#{quoted_right_column_name} desc").
|
508
|
+
highest_right_row = nested_set_scope(:order => "#{quoted_right_column_name} desc").limit(1).lock(true).first
|
491
509
|
maxright = highest_right_row ? (highest_right_row[right_column_name] || 0) : 0
|
492
510
|
# adds the new node to the right of all existing nodes
|
493
511
|
self[left_column_name] = maxright + 1
|
@@ -517,11 +535,8 @@ module CollectiveIdea #:nodoc:
|
|
517
535
|
in_tenacious_transaction do
|
518
536
|
reload_nested_set
|
519
537
|
# select the rows in the model that extend past the deletion point and apply a lock
|
520
|
-
|
521
|
-
|
522
|
-
:conditions => ["#{quoted_left_column_name} >= ?", left],
|
523
|
-
:lock => true
|
524
|
-
)
|
538
|
+
nested_set_scope.where(["#{quoted_left_column_name} >= ?", left]).
|
539
|
+
select(id).lock(true)
|
525
540
|
|
526
541
|
if acts_as_nested_set_options[:dependent] == :destroy
|
527
542
|
descendants.each do |model|
|
@@ -529,21 +544,18 @@ module CollectiveIdea #:nodoc:
|
|
529
544
|
model.destroy
|
530
545
|
end
|
531
546
|
else
|
532
|
-
nested_set_scope.
|
533
|
-
|
534
|
-
left, right]
|
535
|
-
)
|
547
|
+
nested_set_scope.where(["#{quoted_left_column_name} > ? AND #{quoted_right_column_name} < ?", left, right]).
|
548
|
+
delete_all
|
536
549
|
end
|
537
550
|
|
538
551
|
# update lefts and rights for remaining nodes
|
539
552
|
diff = right - left + 1
|
540
|
-
nested_set_scope.update_all(
|
541
|
-
["#{quoted_left_column_name} = (#{quoted_left_column_name} - ?)", diff]
|
542
|
-
["#{quoted_left_column_name} > ?", right]
|
553
|
+
nested_set_scope.where(["#{quoted_left_column_name} > ?", right]).update_all(
|
554
|
+
["#{quoted_left_column_name} = (#{quoted_left_column_name} - ?)", diff]
|
543
555
|
)
|
544
|
-
|
545
|
-
|
546
|
-
["#{quoted_right_column_name}
|
556
|
+
|
557
|
+
nested_set_scope.where(["#{quoted_right_column_name} > ?", right]).update_all(
|
558
|
+
["#{quoted_right_column_name} = (#{quoted_right_column_name} - ?)", diff]
|
547
559
|
)
|
548
560
|
|
549
561
|
# Don't allow multiple calls to destroy to corrupt the set
|
@@ -25,12 +25,12 @@ module CollectiveIdea #:nodoc:
|
|
25
25
|
if class_or_item.is_a? Array
|
26
26
|
items = class_or_item.reject { |e| !e.root? }
|
27
27
|
else
|
28
|
-
class_or_item = class_or_item.roots if class_or_item.
|
28
|
+
class_or_item = class_or_item.roots if class_or_item.respond_to?(:scoped)
|
29
29
|
items = Array(class_or_item)
|
30
30
|
end
|
31
31
|
result = []
|
32
32
|
items.each do |root|
|
33
|
-
result += root.self_and_descendants.map do |i|
|
33
|
+
result += root.class.associate_parents(root.self_and_descendants).map do |i|
|
34
34
|
if mover.nil? || mover.new_record? || mover.move_possible?(i)
|
35
35
|
[yield(i), i.id]
|
36
36
|
end
|
@@ -66,7 +66,7 @@ module CollectiveIdea #:nodoc:
|
|
66
66
|
result = []
|
67
67
|
children = []
|
68
68
|
items.each do |root|
|
69
|
-
root.self_and_descendants.map do |i|
|
69
|
+
root.class.associate_parents(root.self_and_descendants).map do |i|
|
70
70
|
if mover.nil? || mover.new_record? || mover.move_possible?(i)
|
71
71
|
if !i.leaf?
|
72
72
|
children.sort_by! &order
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_nested_set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2012-
|
14
|
+
date: 2012-08-21 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
version: '0'
|
68
68
|
requirements: []
|
69
69
|
rubyforge_project:
|
70
|
-
rubygems_version: 1.8.
|
70
|
+
rubygems_version: 1.8.22
|
71
71
|
signing_key:
|
72
72
|
specification_version: 3
|
73
73
|
summary: An awesome nested set implementation for Active Record
|