@@ -131,6 +131,15 module ActiveRecord | |||
|
131 | 131 | when 'lowest' |
|
132 | 132 | move_to_bottom |
|
133 | 133 | end |
|
134 | reset_positions_in_list | |
|
135 | end | |
|
136 | ||
|
137 | def reset_positions_in_list | |
|
138 | acts_as_list_class.where(scope_condition).reorder("#{position_column} ASC, id ASC").each_with_index do |item, i| | |
|
139 | unless item.send(position_column) == (i + 1) | |
|
140 | acts_as_list_class.update_all({position_column => (i + 1)}, {:id => item.id}) | |
|
141 | end | |
|
142 | end | |
|
134 | 143 | end |
|
135 | 144 | |
|
136 | 145 | # Removes the item from the list. |
@@ -209,7 +218,7 module ActiveRecord | |||
|
209 | 218 | def bottom_item(except = nil) |
|
210 | 219 | conditions = scope_condition |
|
211 | 220 | conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except |
|
212 |
acts_as_list_class. |
|
|
221 | acts_as_list_class.where(conditions).reorder("#{position_column} DESC").first | |
|
213 | 222 | end |
|
214 | 223 | |
|
215 | 224 | # Forces item to assume the bottom position in the list. |
@@ -47,5 +47,30 class IssuePriorityTest < ActiveSupport::TestCase | |||
|
47 | 47 | def test_option_name |
|
48 | 48 | assert_equal :enumeration_issue_priorities, IssuePriority.new.option_name |
|
49 | 49 | end |
|
50 | end | |
|
51 | 50 | |
|
51 | def test_should_be_created_at_last_position | |
|
52 | IssuePriority.delete_all | |
|
53 | ||
|
54 | priorities = [1, 2, 3].map {|i| IssuePriority.create!(:name => "P#{i}")} | |
|
55 | assert_equal [1, 2, 3], priorities.map(&:position) | |
|
56 | end | |
|
57 | ||
|
58 | def test_reset_positions_in_list_should_set_sequential_positions | |
|
59 | IssuePriority.delete_all | |
|
60 | ||
|
61 | priorities = [1, 2, 3].map {|i| IssuePriority.create!(:name => "P#{i}")} | |
|
62 | priorities[0].update_attribute :position, 4 | |
|
63 | priorities[1].update_attribute :position, 2 | |
|
64 | priorities[2].update_attribute :position, 7 | |
|
65 | assert_equal [4, 2, 7], priorities.map(&:reload).map(&:position) | |
|
66 | ||
|
67 | priorities[0].reset_positions_in_list | |
|
68 | assert_equal [2, 1, 3], priorities.map(&:reload).map(&:position) | |
|
69 | end | |
|
70 | ||
|
71 | def test_moving_in_list_should_reset_positions | |
|
72 | priority = IssuePriority.first | |
|
73 | priority.expects(:reset_positions_in_list).once | |
|
74 | priority.move_to = 'higher' | |
|
75 | end | |
|
76 | end |
General Comments 0
You need to be logged in to leave comments.
Login now