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 1 commit
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
10 changes: 6 additions & 4 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,12 @@ def higher_item

# Return the next n higher items in the list
# selects all higher items by default
def higher_items(limit=nil)
def higher_items(limit = nil)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is causing your test failures. I'm not sure spaces are ok between the argument and the default value. So do limit=nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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 @@ -279,11 +280,12 @@ def lower_item

# Return the next n lower items in the list
# selects all lower items by default
def lower_items(limit=nil)
def lower_items(limit = nil)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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_columns(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