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

Commit

Permalink
Fix reverse not working on reordering, which broke calling reorder('.…
Browse files Browse the repository at this point in the history
…..').last.
  • Loading branch information
Octavian Neamtu committed Apr 25, 2015
1 parent 5542266 commit 10e7fad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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

0 comments on commit 10e7fad

Please sign in to comment.