Module: CollectiveIdea::Acts::NestedSet::Model::Prunable

Defined in:
lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/prunable.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) destroy_descendants

Prunes a branch off of the tree, shifting all of the elements on the right back to the left so the counts still work.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/prunable.rb', line 9

def destroy_descendants
  return if right.nil? || left.nil? || skip_before_destroy

  in_tenacious_transaction do
    reload_nested_set
    # select the rows in the model that extend past the deletion point and apply a lock
    nested_set_scope.right_of(left).select(id).lock(true)

    destroy_or_delete_descendants

    # update lefts and rights for remaining nodes
    update_siblings_for_remaining_nodes

    # Reload is needed because children may have updated their parent (self) during deletion.
    reload

    # Don't allow multiple calls to destroy to corrupt the set
    self.skip_before_destroy = true
  end
end

- (Object) destroy_or_delete_descendants



30
31
32
33
34
35
36
37
38
39
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/prunable.rb', line 30

def destroy_or_delete_descendants
  if acts_as_nested_set_options[:dependent] == :destroy
    descendants.each do |model|
      model.skip_before_destroy = true
      model.destroy
    end
  else
    descendants.delete_all
  end
end

- (Object) diff



54
55
56
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/prunable.rb', line 54

def diff
  right - left + 1
end

- (Object) update_siblings(direction)



46
47
48
49
50
51
52
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/prunable.rb', line 46

def update_siblings(direction)
  full_column_name = send("quoted_#{direction}_column_full_name")
  column_name = send("quoted_#{direction}_column_name")

  nested_set_scope.where(["#{full_column_name} > ?", right]).
    update_all(["#{column_name} = (#{column_name} - ?)", diff])
end

- (Object) update_siblings_for_remaining_nodes



41
42
43
44
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/prunable.rb', line 41

def update_siblings_for_remaining_nodes
  update_siblings(:left)
  update_siblings(:right)
end