Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix issue #1272

Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
  • Loading branch information...
commit 9d6560421c1d9048003afb09e7ea333fc7be665f 1 parent 9dabbd0
@winterchord winterchord authored
View
2  activerecord/lib/active_record/relation.rb
@@ -6,7 +6,7 @@ class Relation
JoinOperation = Struct.new(:relation, :join_class, :on)
ASSOCIATION_METHODS = [:includes, :eager_load, :preload]
MULTI_VALUE_METHODS = [:select, :group, :order, :joins, :where, :having, :bind]
- SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :create_with, :from, :reorder]
+ SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :create_with, :from, :reorder, :reverse_order]
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches
View
13 activerecord/lib/active_record/relation/query_methods.rb
@@ -9,7 +9,7 @@ module QueryMethods
:select_values, :group_values, :order_values, :joins_values,
:where_values, :having_values, :bind_values,
:limit_value, :offset_value, :lock_value, :readonly_value, :create_with_value,
- :from_value, :reorder_value
+ :from_value, :reorder_value, :reverse_order_value
def includes(*args)
args.reject! {|a| a.blank? }
@@ -158,13 +158,9 @@ def extending(*modules)
end
def reverse_order
- order_clause = arel.order_clauses
-
- order = order_clause.empty? ?
- "#{table_name}.#{primary_key} DESC" :
- reverse_sql_order(order_clause).join(', ')
-
- except(:order).order(Arel.sql(order))
+ relation = clone
+ relation.reverse_order_value = true
+ relation
end
def arel
@@ -186,6 +182,7 @@ def build_arel
arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?
order = @reorder_value ? @reorder_value : @order_values
+ order = @reverse_order_value ? reverse_sql_order(order) : order
arel.order(*order.uniq.reject{|o| o.blank?}) unless order.empty?
build_select(arel, @select_values.uniq)
View
4 activerecord/test/cases/relation_scoping_test.rb
@@ -10,6 +10,10 @@
class RelationScopingTest < ActiveRecord::TestCase
fixtures :authors, :developers, :projects, :comments, :posts, :developers_projects
+
+ def test_modified_default_scoped_find_last
+ assert_not_equal DeveloperOrderedBySalary.first, DeveloperOrderedBySalary.last
+ end
def test_scoped_find
Developer.where("name = 'David'").scoping do
Please sign in to comment.
Something went wrong with that request. Please try again.