Skip to content

Commit

Permalink
before_destroy does not trigger any save callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Feb 13, 2010
1 parent ac44c32 commit 571d7b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/acts_as_list.rb
Expand Up @@ -61,7 +61,7 @@ def position_column
#{scope_condition_method}
before_destroy :remove_from_list
before_destroy :eliminate_current_position
before_create :add_to_list_bottom
EOV
end
Expand Down Expand Up @@ -248,6 +248,10 @@ def insert_at_position(position)
increment_positions_on_lower_items(position)
self.update_attribute(position_column, position)
end

def eliminate_current_position
decrement_positions_on_lower_items if in_list?
end
end
end

Expand Down
17 changes: 16 additions & 1 deletion test/list_test.rb
Expand Up @@ -32,9 +32,17 @@ class ListMixin < Mixin
acts_as_list :column => "pos", :scope => :parent

def self.table_name() "mixins" end

attr_accessor :before_save_triggered
before_save :log_before_save

def log_before_save
self.before_save_triggered = true
end
end

class ListMixinSub1 < ListMixin

end

class ListMixinSub2 < ListMixin
Expand Down Expand Up @@ -219,7 +227,14 @@ def test_remove_before_destroy_does_not_shift_lower_items_twice
assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
end
end

def test_should_not_trigger_unexpected_callbacks_on_destroy
element = ListMixin.find(2)
assert !element.before_save_triggered
element.destroy
assert !element.before_save_triggered
end

# special thanks to openhood on github
def test_delete_middle_with_holes
Expand Down

0 comments on commit 571d7b3

Please sign in to comment.