Skip to content

Commit

Permalink
acts_as_list position_column being updated to nil in before_destroy c…
Browse files Browse the repository at this point in the history
…allback

[#84 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
Ben Fyvie authored and jeremy committed Jul 1, 2010
1 parent 4354268 commit 931423d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def position_column
#{scope_condition_method}
before_destroy :remove_from_list
before_destroy :decrement_positions_on_lower_items
before_create :add_to_list_bottom
EOV
end
Expand Down
21 changes: 21 additions & 0 deletions test/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,27 @@ def test_remove_before_destroy_does_not_shift_lower_items_twice
assert_equal 3, ListMixin.find(4).pos
end

def test_before_destroy_callbacks_do_not_update_position_to_nil_before_deleting_the_record
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

# We need to trigger all the before_destroy callbacks without actually
# destroying the record so we can see the affect the callbacks have on
# the record.
list = ListMixin.find(2)
if list.respond_to?(:run_callbacks)
list.run_callbacks(:destroy)
else
list.send(:callback, :before_destroy)
end

assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(2).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
end

end

class ListSubTest < Test::Unit::TestCase
Expand Down

0 comments on commit 931423d

Please sign in to comment.