Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8822: 2.0-PLUGIN_acts_as_list_remove_from_list_fix.diff

File 2.0-PLUGIN_acts_as_list_remove_from_list_fix.diff, 2.4 kB (added by mikel, 1 year ago)

Same patch for the 2.0 vendor/plugins/acts_as_list

  • test/list_test.rb

    old new  
    187187    new2.move_higher 
    188188    assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos') 
    189189  end 
    190  
     190   
     191   
     192  def test_remove_from_list_should_then_fail_in_list?  
     193    assert_equal true, ListMixin.find(1).in_list? 
     194    ListMixin.find(1).remove_from_list 
     195    assert_equal false, ListMixin.find(1).in_list? 
     196  end  
     197   
     198  def test_remove_from_list_should_set_position_to_nil  
     199    assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) 
     200   
     201    ListMixin.find(2).remove_from_list  
     202   
     203    assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) 
     204   
     205    assert_equal 1,   ListMixin.find(1).pos 
     206    assert_equal nil, ListMixin.find(2).pos 
     207    assert_equal 2,   ListMixin.find(3).pos 
     208    assert_equal 3,   ListMixin.find(4).pos 
     209  end  
     210   
     211  def test_remove_before_destroy_does_not_shift_lower_items_twice  
     212    assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) 
     213   
     214    ListMixin.find(2).remove_from_list  
     215    ListMixin.find(2).destroy  
     216   
     217    assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) 
     218   
     219    assert_equal 1, ListMixin.find(1).pos 
     220    assert_equal 2, ListMixin.find(3).pos 
     221    assert_equal 3, ListMixin.find(4).pos 
     222  end  
     223   
    191224end 
    192225 
    193226class ListSubTest < Test::Unit::TestCase 
  • lib/active_record/acts/list.rb

    old new  
    6363 
    6464            #{scope_condition_method} 
    6565 
    66             after_destroy :remove_from_list 
     66            before_destroy :remove_from_list 
    6767            before_create  :add_to_list_bottom 
    6868          EOV 
    6969        end 
     
    121121 
    122122        # Removes the item from the list. 
    123123        def remove_from_list 
    124           decrement_positions_on_lower_items if in_list? 
     124          if in_list? 
     125            decrement_positions_on_lower_items 
     126            update_attribute position_column, nil 
     127          end 
    125128        end 
    126129 
    127130        # Increase the position of this item without adjusting the rest of the list.