Skip to content

Commit

Permalink
Fixed behavior when *_destroy callbacks are critical.
Browse files Browse the repository at this point in the history
For example:
```ruby
class Category < ActiveRecord::Base
  acts_as_ordered_tree

  before_destroy :crit
  def crit
    # something very critical
  end
end

root = Category.create
child = root.children.create

root.destroy # callback called only for root, children deleted silently
```

Now <code>destroy</code> method called for every descendant of destroyed node.
  • Loading branch information
take-five committed Aug 22, 2012
1 parent f1f600c commit 5045dd1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/acts_as_ordered_tree.rb
Expand Up @@ -33,7 +33,8 @@ def acts_as_ordered_tree(options = {})
:class_name => name,
:foreign_key => options[:parent_column],
:order => options[:position_column],
:inverse_of => (:parent unless options[:polymorphic])
:inverse_of => (:parent unless options[:polymorphic]),
:dependent => :destroy
}

[:before_add, :after_add, :before_remove, :after_remove].each do |callback|
Expand Down Expand Up @@ -83,7 +84,7 @@ def acts_as_ordered_tree(options = {})
after_save "move_to_child_with_index(parent, #{position_column})",
:if => "#{position_column} && (#{position_column}_changed? || #{parent_column}_changed?)"

before_destroy :destroy_descendants
before_destroy :flush_descendants
after_destroy "decrement_lower_positions(#{parent_column}_was, #{position_column}_was)", :if => position_column

# setup validations
Expand Down
4 changes: 1 addition & 3 deletions lib/acts_as_ordered_tree/instance_methods.rb
Expand Up @@ -329,9 +329,7 @@ def set_scope! #:nodoc:
end
end

def destroy_descendants #:nodoc:
descendants.delete_all
# flush memoization
def flush_descendants #:nodoc:
@self_and_descendants = nil
end

Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_ordered_tree/version.rb
@@ -1,3 +1,3 @@
module ActsAsOrderedTree
VERSION = "1.0.3"
VERSION = "1.0.4"
end

0 comments on commit 5045dd1

Please sign in to comment.