mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #11958 from jetthoughts/extract_pre_process_orders_args
Re-use order arguments pre-processing for reorder
This commit is contained in:
commit
beb5ea8468
5 changed files with 52 additions and 15 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Re-use `order` argument pre-processing for `reorder`.
|
||||||
|
|
||||||
|
*Paul Nikitochkin*
|
||||||
|
|
||||||
* Fix PredicateBuilder so polymorhic association keys in `where` clause can
|
* Fix PredicateBuilder so polymorhic association keys in `where` clause can
|
||||||
also accept not only `ActiveRecord::Base` direct descendances (decorated
|
also accept not only `ActiveRecord::Base` direct descendances (decorated
|
||||||
models, for example).
|
models, for example).
|
||||||
|
|
|
@ -289,17 +289,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def order!(*args) # :nodoc:
|
def order!(*args) # :nodoc:
|
||||||
args.flatten!
|
preprocess_order_args(args)
|
||||||
validate_order_args(args)
|
|
||||||
|
|
||||||
references = args.grep(String)
|
|
||||||
references.map! { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }.compact!
|
|
||||||
references!(references) if references.any?
|
|
||||||
|
|
||||||
# if a symbol is given we prepend the quoted table name
|
|
||||||
args.map! do |arg|
|
|
||||||
arg.is_a?(Symbol) ? Arel::Nodes::Ascending.new(klass.arel_table[arg]) : arg
|
|
||||||
end
|
|
||||||
|
|
||||||
self.order_values += args
|
self.order_values += args
|
||||||
self
|
self
|
||||||
|
@ -320,8 +310,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def reorder!(*args) # :nodoc:
|
def reorder!(*args) # :nodoc:
|
||||||
args.flatten!
|
preprocess_order_args(args)
|
||||||
validate_order_args(args)
|
|
||||||
|
|
||||||
self.reordering_value = true
|
self.reordering_value = true
|
||||||
self.order_values = args
|
self.order_values = args
|
||||||
|
@ -1036,6 +1025,20 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def preprocess_order_args(order_args)
|
||||||
|
order_args.flatten!
|
||||||
|
validate_order_args(order_args)
|
||||||
|
|
||||||
|
references = order_args.grep(String)
|
||||||
|
references.map! { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }.compact!
|
||||||
|
references!(references) if references.any?
|
||||||
|
|
||||||
|
# if a symbol is given we prepend the quoted table name
|
||||||
|
order_args.map! do |arg|
|
||||||
|
arg.is_a?(Symbol) ? Arel::Nodes::Ascending.new(klass.arel_table[arg]) : arg
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Checks to make sure that the arguments are not blank. Note that if some
|
# Checks to make sure that the arguments are not blank. Note that if some
|
||||||
# blank-like object were initially passed into the query method, then this
|
# blank-like object were initially passed into the query method, then this
|
||||||
# method will not raise an error.
|
# method will not raise an error.
|
||||||
|
|
|
@ -1172,8 +1172,11 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "works in combination with order(:symbol)" do
|
test "works in combination with order(:symbol) and reorder(:symbol)" do
|
||||||
author = Author.includes(:posts).references(:posts).order(:name).where('posts.title IS NOT NULL').first
|
author = Author.includes(:posts).references(:posts).order(:name).find_by('posts.title IS NOT NULL')
|
||||||
|
assert_equal authors(:bob), author
|
||||||
|
|
||||||
|
author = Author.includes(:posts).references(:posts).reorder(:name).find_by('posts.title IS NOT NULL')
|
||||||
assert_equal authors(:bob), author
|
assert_equal authors(:bob), author
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -311,6 +311,15 @@ module ActiveRecord
|
||||||
assert relation.reordering_value
|
assert relation.reordering_value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test '#reorder! with symbol prepends the table name' do
|
||||||
|
assert relation.reorder!(:name).equal?(relation)
|
||||||
|
node = relation.order_values.first
|
||||||
|
|
||||||
|
assert node.ascending?
|
||||||
|
assert_equal :name, node.expr.name
|
||||||
|
assert_equal "posts", node.expr.relation.name
|
||||||
|
end
|
||||||
|
|
||||||
test 'reverse_order!' do
|
test 'reverse_order!' do
|
||||||
assert relation.reverse_order!.equal?(relation)
|
assert relation.reverse_order!.equal?(relation)
|
||||||
assert relation.reverse_order_value
|
assert relation.reverse_order_value
|
||||||
|
|
|
@ -1344,6 +1344,24 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
assert_equal [], scope.references_values
|
assert_equal [], scope.references_values
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_automatically_added_reorder_references
|
||||||
|
scope = Post.reorder('comments.body')
|
||||||
|
assert_equal %w(comments), scope.references_values
|
||||||
|
|
||||||
|
scope = Post.reorder('comments.body', 'yaks.body')
|
||||||
|
assert_equal %w(comments yaks), scope.references_values
|
||||||
|
|
||||||
|
# Don't infer yaks, let's not go down that road again...
|
||||||
|
scope = Post.reorder('comments.body, yaks.body')
|
||||||
|
assert_equal %w(comments), scope.references_values
|
||||||
|
|
||||||
|
scope = Post.reorder('comments.body asc')
|
||||||
|
assert_equal %w(comments), scope.references_values
|
||||||
|
|
||||||
|
scope = Post.reorder('foo(comments.body)')
|
||||||
|
assert_equal [], scope.references_values
|
||||||
|
end
|
||||||
|
|
||||||
def test_presence
|
def test_presence
|
||||||
topics = Topic.all
|
topics = Topic.all
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue