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

Commit 10e7fad

Browse files
author
Octavian Neamtu
committed
Fix reverse not working on reordering, which broke calling reorder('...').last.
1 parent 5542266 commit 10e7fad

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## 1.2.4 (Unreleased)
2-
2+
Fix reverse not working on reordering, which broke calling reorder('...').last. @oneamtu
33

44
## 1.2.3 (2015-2-5)
55
* Support the latest version of Rails 4.2 and 4.1. By @danielrhodes

lib/squeel/adapters/active_record/4.1/relation_extensions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def build_from
225225
def build_order(arel)
226226
orders = order_visit(dehashified_order_values)
227227
orders = orders.uniq.reject(&:blank?)
228-
orders = reverse_sql_order(orders) if reverse_order_value && !reordering_value
228+
orders = reverse_sql_order(orders) unless !reverse_order_value || (reordering_value && orders.empty?)
229229

230230
arel.order(*orders) unless orders.empty?
231231
end

spec/squeel/adapters/active_record/relation_extensions_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,26 @@ module ActiveRecord
918918
block.to_sql.should match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q}/
919919
end
920920

921+
it 'returns the proper #first on reordering' do
922+
queries = queries_for do
923+
@standard.reorder{id.asc}.first
924+
end
925+
queries.size.should eq(1)
926+
query = queries.first
927+
query.should match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} ASC/
928+
query.should_not match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} DESC/
929+
end
930+
931+
it 'returns the proper #last on reordering' do
932+
queries = queries_for do
933+
@standard.reorder{id.asc}.last
934+
end
935+
queries.size.should eq(1)
936+
query = queries.last
937+
query.should match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} DESC/
938+
query.should_not match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} ASC/
939+
end
940+
921941
it 'drops order by clause when passed nil' do
922942
block = @standard.reorder(nil)
923943
sql = block.to_sql

0 commit comments

Comments
 (0)