Skip to content

Commit

Permalink
Show items with same position in higher and lower items (#231)
Browse files Browse the repository at this point in the history
* Show items with same position in higher and lower items
* Remove spacing, use update_column instead of update_columns
  • Loading branch information
jpalumickas authored and brendon committed Sep 21, 2016
1 parent 1a7b289 commit c0f4abd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/shared_list_sub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c0f4abd

Please sign in to comment.