Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make scoped reorder override previous applied orders

[5528 state:committed]
  • Loading branch information...
commit f0113e59c25f340ffd5d2eee76f2edcdba728bfa 1 parent 02daf68
@spastorino spastorino authored
View
7 activerecord/lib/active_record/relation/query_methods.rb
@@ -6,7 +6,7 @@ module QueryMethods
extend ActiveSupport::Concern
attr_accessor :includes_values, :eager_load_values, :preload_values,
- :select_values, :group_values, :order_values, :joins_values, :where_values, :having_values,
+ :select_values, :group_values, :order_values, :reorder_flag, :joins_values, :where_values, :having_values,
:limit_value, :offset_value, :lock_value, :readonly_value, :create_with_value, :from_value
def includes(*args)
@@ -53,7 +53,10 @@ def order(*args)
def reorder(*args)
relation = clone
- relation.order_values = args if args.present?
+ if args.present?
+ relation.order_values = args
+ relation.reorder_flag = true
+ end
relation
end
View
11 activerecord/lib/active_record/relation/spawn_methods.rb
@@ -18,11 +18,20 @@ def merge(r)
end
end
- (Relation::MULTI_VALUE_METHODS - [:joins, :where]).each do |method|
+ (Relation::MULTI_VALUE_METHODS - [:joins, :where, :order]).each do |method|
value = r.send(:"#{method}_values")
merged_relation.send(:"#{method}_values=", merged_relation.send(:"#{method}_values") + value) if value.present?
end
+ order_value = r.order_values
+ if order_value.present?
+ if r.reorder_flag
+ merged_relation.order_values = order_value
+ else
+ merged_relation.order_values = merged_relation.order_values + order_value
+ end
+ end
+
merged_relation = merged_relation.joins(r.joins_values)
merged_wheres = @where_values

2 comments on commit f0113e5

@yaroslav

Thanks!

@spastorino
Owner

:), the code is not the best but we have tests now and we can refactor ;)

Please sign in to comment.
Something went wrong with that request. Please try again.