1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix acts_as_list such that moving next-to-last item to the bottom does not result in duplicate positions

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1872 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2005-07-20 12:47:55 +00:00
parent 7077d8bb3d
commit d886ad5c8b
3 changed files with 25 additions and 5 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Fix acts_as_list so that moving next-to-last item to the bottom does not result in duplicate item positions
* Fixed incompatibility in DB2 adapter with the new limit/offset approach #1718 [Maik Schmidt]
* Added :select option to find which can specify a different value than the default *, like find(:all, :select => "first_name, last_name"), if you either only want to select part of the columns or exclude columns otherwise included from a join #1338 [Stefan Kaes]

View file

@ -163,17 +163,19 @@ module ActiveRecord
# Overwrite this method to define the scope of the list changes
def scope_condition() "1" end
def bottom_position_in_list
item = bottom_item
def bottom_position_in_list(except = nil)
item = bottom_item(except)
item ? item.send(position_column) : 0
end
def bottom_item
self.class.find(:first, :conditions => scope_condition, :order => "#{position_column} DESC")
def bottom_item(except = nil)
conditions = scope_condition
conditions = "#{conditions} AND id != #{except.id}" if except
self.class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
end
def assume_bottom_position
update_attribute(position_column, bottom_position_in_list.to_i + 1) unless last?
update_attribute(position_column, bottom_position_in_list(self).to_i + 1)
end
def assume_top_position

View file

@ -65,6 +65,22 @@ class ListTest < Test::Unit::TestCase
ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
end
def test_move_to_bottom_with_next_to_last_item
assert_equal [mixins(:list_1),
mixins(:list_2),
mixins(:list_3),
mixins(:list_4)],
ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
mixins(:list_3).move_to_bottom
assert_equal [mixins(:list_1),
mixins(:list_2),
mixins(:list_4),
mixins(:list_3)],
ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
end
def test_next_prev
assert_equal mixins(:list_2), mixins(:list_1).lower_item