Skip to content
Browse files

Merge pull request #46 from saberma/master

support accepts_nested_attributes_for multi-destroy
  • Loading branch information...
2 parents f629561 + 8bc2642 commit f62c43265f0e47fe7666a93849abad5aa7c5c6d3 @swanandp committed Aug 17, 2012
Showing with 50 additions and 0 deletions.
  1. +5 −0 lib/acts_as_list/active_record/acts/list.rb
  2. +45 −0 test/test_list.rb
View
5 lib/acts_as_list/active_record/acts/list.rb
@@ -75,6 +75,7 @@ def position_column
#{scope_condition_method}
+ before_destroy :reload_position
after_destroy :decrement_positions_on_lower_items
before_create :add_to_list_#{configuration[:add_new_at]}
after_update :update_positions
@@ -325,6 +326,10 @@ def update_positions
return unless acts_as_list_class.unscoped.where("#{scope_condition} AND #{position_column} = #{new_position}").count > 1
shuffle_positions_on_intermediate_items old_position, new_position, id
end
+
+ def reload_position
+ self.reload
+ end
end
end
end
View
45 test/test_list.rb
@@ -349,6 +349,51 @@ def test_update_position
end
+class MultiDestroyTest < ActsAsListTestCase
+
+ def setup
+ setup_db
+ end
+
+ # example:
+ #
+ # class TodoList < ActiveRecord::Base
+ # has_many :todo_items, :order => "position"
+ # accepts_nested_attributes_for :todo_items, :allow_destroy => true
+ # end
+ #
+ # class TodoItem < ActiveRecord::Base
+ # belongs_to :todo_list
+ # acts_as_list :scope => :todo_list
+ # end
+ #
+ # Assume that there are three items.
+ # The user mark two items as deleted, click save button, form will be post:
+ #
+ # todo_list.todo_items_attributes = [
+ # {id: 1, _destroy: true},
+ # {id: 2, _destroy: true}
+ # ]
+ #
+ # Save toto_list, the position of item #3 should eql 1.
+ #
+ def test_destroy
+ new1 = DefaultScopedMixin.create
+ assert_equal 1, new1.pos
+
+ new2 = DefaultScopedMixin.create
+ assert_equal 2, new2.pos
+
+ new3 = DefaultScopedMixin.create
+ assert_equal 3, new3.pos
+
+ new1.destroy
+ new2.destroy
+ new3.reload
+ assert_equal 1, new3.pos
+ end
+end
+
#class TopAdditionMixin < Mixin
class TopAdditionTest < ActsAsListTestCase

0 comments on commit f62c432

Please sign in to comment.
Something went wrong with that request. Please try again.