From 16543ab3e798b9ea5ecfe6491bbc9dd3d8458396 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Sat, 16 Mar 2024 18:25:02 +0900 Subject: [PATCH] Avoid modifying unnecessary nodes --- lib/activerecord/originator.rb | 8 +++++++- .../originator/arel_visitor_extension.rb | 18 ++++++++++-------- .../originator/arel_visitor_extension.rbs | 1 + 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/activerecord/originator.rb b/lib/activerecord/originator.rb index f6e4f00..444c070 100644 --- a/lib/activerecord/originator.rb +++ b/lib/activerecord/originator.rb @@ -10,7 +10,13 @@ require_relative "originator/arel_visitor_extension" require_relative "originator/collector_proxy" -Arel::Nodes::Node.descendants.each do |klass| +ActiveRecord::Originator::ArelVisitorExtension::TARGET_NODE_CLASSESS.each do |name| + begin + klass = Arel::Nodes.const_get(name) + rescue NameError + # Some classes are not defined in old arel + next + end klass.prepend ActiveRecord::Originator::ArelNodeExtension end Arel::Visitors::ToSql.prepend ActiveRecord::Originator::ArelVisitorExtension diff --git a/lib/activerecord/originator/arel_visitor_extension.rb b/lib/activerecord/originator/arel_visitor_extension.rb index 604285b..18046c2 100644 --- a/lib/activerecord/originator/arel_visitor_extension.rb +++ b/lib/activerecord/originator/arel_visitor_extension.rb @@ -3,13 +3,7 @@ module ActiveRecord module Originator module ArelVisitorExtension - def accept(object, collector) - super(object, CollectorProxy.new(collector)) - end - - private - - %i[ + TARGET_NODE_CLASSESS = %i[ Ascending Descending Equality @@ -21,7 +15,15 @@ def accept(object, collector) LessThan LessThanOrEqual GreaterThanOrEqual - ].each do |klass_name| + ] + + def accept(object, collector) + super(object, CollectorProxy.new(collector)) + end + + private + + TARGET_NODE_CLASSESS.each do |klass_name| define_method(:"visit_Arel_Nodes_#{klass_name}") do |o, collector| __skip__ = begin comment = originator_comment(o) diff --git a/sig/activerecord/originator/arel_visitor_extension.rbs b/sig/activerecord/originator/arel_visitor_extension.rbs index 5155b80..1d670ed 100644 --- a/sig/activerecord/originator/arel_visitor_extension.rbs +++ b/sig/activerecord/originator/arel_visitor_extension.rbs @@ -1,6 +1,7 @@ module ActiveRecord module Originator module ArelVisitorExtension : _ArelVisitor + TARGET_NODE_CLASSESS: Array[Symbol] def accept: (untyped object, _Collector collector) -> untyped private