Skip to content
This repository
Browse code

Merge pull request #46 from saberma/master

support accepts_nested_attributes_for multi-destroy
  • Loading branch information...
commit f62c43265f0e47fe7666a93849abad5aa7c5c6d3 2 parents f629561 + 8bc2642
Swanand Pagnis authored
5 lib/acts_as_list/active_record/acts/list.rb
@@ -75,6 +75,7 @@ def position_column
75 75
76 76 #{scope_condition_method}
77 77
  78 + before_destroy :reload_position
78 79 after_destroy :decrement_positions_on_lower_items
79 80 before_create :add_to_list_#{configuration[:add_new_at]}
80 81 after_update :update_positions
@@ -325,6 +326,10 @@ def update_positions
325 326 return unless acts_as_list_class.unscoped.where("#{scope_condition} AND #{position_column} = #{new_position}").count > 1
326 327 shuffle_positions_on_intermediate_items old_position, new_position, id
327 328 end
  329 +
  330 + def reload_position
  331 + self.reload
  332 + end
328 333 end
329 334 end
330 335 end
45 test/test_list.rb
@@ -349,6 +349,51 @@ def test_update_position
349 349
350 350 end
351 351
  352 +class MultiDestroyTest < ActsAsListTestCase
  353 +
  354 + def setup
  355 + setup_db
  356 + end
  357 +
  358 + # example:
  359 + #
  360 + # class TodoList < ActiveRecord::Base
  361 + # has_many :todo_items, :order => "position"
  362 + # accepts_nested_attributes_for :todo_items, :allow_destroy => true
  363 + # end
  364 + #
  365 + # class TodoItem < ActiveRecord::Base
  366 + # belongs_to :todo_list
  367 + # acts_as_list :scope => :todo_list
  368 + # end
  369 + #
  370 + # Assume that there are three items.
  371 + # The user mark two items as deleted, click save button, form will be post:
  372 + #
  373 + # todo_list.todo_items_attributes = [
  374 + # {id: 1, _destroy: true},
  375 + # {id: 2, _destroy: true}
  376 + # ]
  377 + #
  378 + # Save toto_list, the position of item #3 should eql 1.
  379 + #
  380 + def test_destroy
  381 + new1 = DefaultScopedMixin.create
  382 + assert_equal 1, new1.pos
  383 +
  384 + new2 = DefaultScopedMixin.create
  385 + assert_equal 2, new2.pos
  386 +
  387 + new3 = DefaultScopedMixin.create
  388 + assert_equal 3, new3.pos
  389 +
  390 + new1.destroy
  391 + new2.destroy
  392 + new3.reload
  393 + assert_equal 1, new3.pos
  394 + end
  395 +end
  396 +
352 397 #class TopAdditionMixin < Mixin
353 398
354 399 class TopAdditionTest < ActsAsListTestCase

0 comments on commit f62c432

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