Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Fix reverse not working on reordering, which broke calling reorder('...').last. #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## 1.2.4 (Unreleased)

Fix reverse not working on reordering, which broke calling reorder('...').last. @oneamtu

## 1.2.3 (2015-2-5)
* Support the latest version of Rails 4.2 and 4.1. By @danielrhodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def build_from
def build_order(arel)
orders = order_visit(dehashified_order_values)
orders = orders.uniq.reject(&:blank?)
orders = reverse_sql_order(orders) if reverse_order_value && !reordering_value
orders = reverse_sql_order(orders) unless !reverse_order_value || (reordering_value && orders.empty?)

arel.order(*orders) unless orders.empty?
end
Expand Down
20 changes: 20 additions & 0 deletions spec/squeel/adapters/active_record/relation_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,26 @@ module ActiveRecord
block.to_sql.should match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q}/
end

it 'returns the proper #first on reordering' do
queries = queries_for do
@standard.reorder{id.asc}.first
end
queries.size.should eq(1)
query = queries.first
query.should match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} ASC/
query.should_not match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} DESC/
end

it 'returns the proper #last on reordering' do
queries = queries_for do
@standard.reorder{id.asc}.last
end
queries.size.should eq(1)
query = queries.last
query.should match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} DESC/
query.should_not match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} ASC/
end

it 'drops order by clause when passed nil' do
block = @standard.reorder(nil)
sql = block.to_sql
Expand Down