when 'lowest'
move_to_bottom
end
+ reset_positions_in_list
+ end
+
+ def reset_positions_in_list
+ acts_as_list_class.where(scope_condition).reorder("#{position_column} ASC, id ASC").each_with_index do |item, i|
+ unless item.send(position_column) == (i + 1)
+ acts_as_list_class.update_all({position_column => (i + 1)}, {:id => item.id})
+ end
+ end
end
# Removes the item from the list.
def bottom_item(except = nil)
conditions = scope_condition
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
- acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
+ acts_as_list_class.where(conditions).reorder("#{position_column} DESC").first
end
# Forces item to assume the bottom position in the list.
def test_option_name
assert_equal :enumeration_issue_priorities, IssuePriority.new.option_name
end
-end
+ def test_should_be_created_at_last_position
+ IssuePriority.delete_all
+
+ priorities = [1, 2, 3].map {|i| IssuePriority.create!(:name => "P#{i}")}
+ assert_equal [1, 2, 3], priorities.map(&:position)
+ end
+
+ def test_reset_positions_in_list_should_set_sequential_positions
+ IssuePriority.delete_all
+
+ priorities = [1, 2, 3].map {|i| IssuePriority.create!(:name => "P#{i}")}
+ priorities[0].update_attribute :position, 4
+ priorities[1].update_attribute :position, 2
+ priorities[2].update_attribute :position, 7
+ assert_equal [4, 2, 7], priorities.map(&:reload).map(&:position)
+
+ priorities[0].reset_positions_in_list
+ assert_equal [2, 1, 3], priorities.map(&:reload).map(&:position)
+ end
+
+ def test_moving_in_list_should_reset_positions
+ priority = IssuePriority.first
+ priority.expects(:reset_positions_in_list).once
+ priority.move_to = 'higher'
+ end
+end