Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show items with same position in higher and lower items #231

Merged
merged 2 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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