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

Changeset 7816

Show
Ignore:
Timestamp:
10/09/07 05:31:19 (1 year ago)
Author:
nzkoz
Message:

Make sure acts_as_list's remove_from_list and in_list? play nicely with one another. References #8822 [mikel]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/acts_as_list/lib/active_record/acts/list.rb

    r7456 r7816  
    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 
     
    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 
  • plugins/acts_as_list/test/list_test.rb

    r7814 r7816  
    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