Skip to content

Commit

Permalink
Remove less interested where_clause_factory
Browse files Browse the repository at this point in the history
I'd not prefer to allocate its less interested object, it doesn't
improve readability, and make code slower.
  • Loading branch information
kamipo committed Jun 20, 2020
1 parent 4215766 commit 550ce69
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 43 deletions.
34 changes: 21 additions & 13 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -3,7 +3,6 @@
require "active_record/relation/from_clause"
require "active_record/relation/query_attribute"
require "active_record/relation/where_clause"
require "active_record/relation/where_clause_factory"
require "active_model/forbidden_attributes_protection"
require "active_support/core_ext/array/wrap"

Expand All @@ -16,8 +15,6 @@ module QueryMethods
# WhereChain objects act as placeholder for queries in which #where does not have any parameter.
# In this case, #where must be chained with #not to return a new relation.
class WhereChain
include ActiveModel::ForbiddenAttributesProtection

def initialize(scope)
@scope = scope
end
Expand All @@ -43,11 +40,7 @@ def initialize(scope)
# User.where.not(name: %w(Ko1 Nobu))
# # SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
def not(opts, *rest)
opts = sanitize_forbidden_attributes(opts)

where_clause = @scope.send(:where_clause_factory).build(opts, rest)

@scope.references_values |= PredicateBuilder.references(opts) if Hash === opts
where_clause = @scope.send(:build_where_clause, opts, rest)

if not_behaves_as_nor?(opts)
ActiveSupport::Deprecation.warn(<<~MSG.squish)
Expand Down Expand Up @@ -1109,9 +1102,21 @@ def build_subquery(subquery_alias, select_value) # :nodoc:
def build_where_clause(opts, rest = []) # :nodoc:
opts = sanitize_forbidden_attributes(opts)
self.references_values |= PredicateBuilder.references(opts) if Hash === opts
where_clause_factory.build(opts, rest) do |table_name|
lookup_reflection_from_join_dependencies(table_name)

case opts
when String, Array
parts = [klass.sanitize_sql(rest.empty? ? opts : [opts, *rest])]
when Hash
parts = predicate_builder.build_from_hash(opts) do |table_name|
lookup_reflection_from_join_dependencies(table_name)
end
when Arel::Nodes::Node
parts = [opts]
else
raise ArgumentError, "Unsupported argument type: #{opts} (#{opts.class})"
end

Relation::WhereClause.new(parts)
end
alias :build_having_clause :build_where_clause

Expand Down Expand Up @@ -1525,9 +1530,12 @@ def structurally_incompatible_values_for(other)
v1 == v2 || (!v1 || v1.empty?) && (!v2 || v2.empty?)
end
end
end

def where_clause_factory
@where_clause_factory ||= Relation::WhereClauseFactory.new(klass, predicate_builder)
end
class Relation # :nodoc:
# No-op WhereClauseFactory to work Mashal.load(File.read("legacy_relation.dump")).
# TODO: Remove the class once Rails 6.1 has released.
class WhereClauseFactory # :nodoc:
end
end
end
30 changes: 0 additions & 30 deletions activerecord/lib/active_record/relation/where_clause_factory.rb

This file was deleted.

Binary file not shown.

1 comment on commit 550ce69

@inopinatus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a historical note, this class was more substantial prior to 213796f#diff-5eed40af24f8d1632cf58d021e82e5b9.

Please sign in to comment.