1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #39046 from kamipo/concise_arel_ast

Improve `WhereClause#ast` to make concise Arel ast
This commit is contained in:
Ryuta Kamizono 2020-04-26 03:50:31 +09:00 committed by GitHub
commit a5469f0239
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 18 deletions

View file

@ -36,13 +36,15 @@ module ActiveRecord
arel = join_scope.arel(alias_tracker.aliases)
nodes = arel.constraints.first
others = nodes.children.extract! do |node|
!Arel.fetch_attribute(node) { |attr| attr.relation.name == table.name }
if nodes.is_a?(Arel::Nodes::And)
others = nodes.children.extract! do |node|
!Arel.fetch_attribute(node) { |attr| attr.relation.name == table.name }
end
end
joins << table.create_join(table, table.create_on(nodes), join_type)
unless others.empty?
if others && !others.empty?
joins.concat arel.join_sources
append_constraints(joins.last, others)
end
@ -77,7 +79,8 @@ module ActiveRecord
join_string = table.create_and(constraints.unshift(join.left))
join.left = Arel.sql(base_klass.connection.visitor.compile(join_string))
else
join.right.expr.children.concat(constraints)
right = join.right
right.expr = Arel::Nodes::And.new(constraints.unshift right.expr)
end
end
end

View file

@ -62,7 +62,8 @@ module ActiveRecord
end
def ast
Arel::Nodes::And.new(predicates_with_wrapped_sql_literals)
predicates = predicates_with_wrapped_sql_literals
predicates.one? ? predicates.first : Arel::Nodes::And.new(predicates)
end
def ==(other)

View file

@ -6,7 +6,6 @@ require "models/comment"
require "models/developer"
require "models/project"
require "models/computer"
require "models/vehicle"
require "models/cat"
require "concurrent/atomic/cyclic_barrier"
@ -499,13 +498,13 @@ class DefaultScopingTest < ActiveRecord::TestCase
test "a scope can remove the condition from the default scope" do
scope = DeveloperCalledJamis.david2
assert_equal 1, scope.where_clause.ast.children.length
assert_instance_of Arel::Nodes::Equality, scope.where_clause.ast
assert_equal Developer.where(name: "David").map(&:id), scope.map(&:id)
end
def test_with_abstract_class_where_clause_should_not_be_duplicated
scope = Bus.all
assert_equal scope.where_clause.ast.children.length, 1
scope = Lion.all
assert_instance_of Arel::Nodes::Equality, scope.where_clause.ast
end
def test_sti_conditions_are_not_carried_in_default_scope

View file

@ -1,9 +0,0 @@
# frozen_string_literal: true
class Vehicle < ActiveRecord::Base
self.abstract_class = true
default_scope -> { where("tires_count IS NOT NULL") }
end
class Bus < Vehicle
end