diff --git a/lib/acts_as_list/active_record/acts/list.rb b/lib/acts_as_list/active_record/acts/list.rb index c750b558..6eb7adf6 100644 --- a/lib/acts_as_list/active_record/acts/list.rb +++ b/lib/acts_as_list/active_record/acts/list.rb @@ -266,7 +266,8 @@ def higher_items(limit=nil) limit ||= acts_as_list_list.count position_value = send(position_column) acts_as_list_list. - where("#{quoted_position_column_with_table_name} < ?", position_value). + where("#{quoted_position_column_with_table_name} <= ?", position_value). + where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.send(self.class.primary_key)). order("#{quoted_position_column_with_table_name} DESC"). limit(limit) end @@ -283,7 +284,8 @@ def lower_items(limit=nil) limit ||= acts_as_list_list.count position_value = send(position_column) acts_as_list_list. - where("#{quoted_position_column_with_table_name} > ?", position_value). + where("#{quoted_position_column_with_table_name} >= ?", position_value). + where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.send(self.class.primary_key)). order("#{quoted_position_column_with_table_name} ASC"). limit(limit) end diff --git a/test/shared_list_sub.rb b/test/shared_list_sub.rb index c0c8d2b6..8e03f5f0 100644 --- a/test/shared_list_sub.rb +++ b/test/shared_list_sub.rb @@ -85,6 +85,23 @@ def test_next_prev_groups assert_equal [], li1.higher_items end + def test_next_prev_groups_with_same_position + li1 = ListMixin.where(id: 1).first + li2 = ListMixin.where(id: 2).first + li3 = ListMixin.where(id: 3).first + li4 = ListMixin.where(id: 4).first + + li3.update_column(:pos, 2) # Make the same position as li2 + + assert_equal [1, 2, 2, 4], ListMixin.pluck(:pos) + + assert_equal [li3, li4], li2.lower_items + assert_equal [li2, li4], li3.lower_items + + assert_equal [li3, li1], li2.higher_items + assert_equal [li2, li1], li3.higher_items + end + def test_injection item = ListMixin.new("parent_id"=>1) assert_equal({ parent_id: 1 }, item.scope_condition)