From 919394066f1c5abbac3c76174fa1c8ddaf6fa190 Mon Sep 17 00:00:00 2001 From: Kyle Fazzari Date: Fri, 6 Sep 2024 13:58:42 -0700 Subject: [PATCH] active record patches: use Ruby 3 argument forwarding (#158) Today this uses Ruby 2-era argument forwarding that doesn't separate out keyword arguments in Ruby 3. Update the forwarding technique to be compatible with Ruby 3. Fix #157 Signed-off-by: Kyle Fazzari --- lib/goldiloader/active_record_patches.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/goldiloader/active_record_patches.rb b/lib/goldiloader/active_record_patches.rb index 3e62be3..b205efc 100644 --- a/lib/goldiloader/active_record_patches.rb +++ b/lib/goldiloader/active_record_patches.rb @@ -152,7 +152,7 @@ def load_with_auto_include module SingularAssociationPatch private - def find_target(*args) + def find_target(...) load_with_auto_include { super } end end @@ -161,13 +161,13 @@ def find_target(*args) module CollectionAssociationPatch # Force these methods to load the entire association for fully_load associations [:size, :ids_reader, :empty?].each do |method| - define_method(method) do |*args, &block| + define_method(method) do |*args, **kwargs, &block| load_target if fully_load? - super(*args, &block) + super(*args, **kwargs, &block) end end - def load_target(*args) + def load_target(...) load_with_auto_include { super } end @@ -203,14 +203,14 @@ def auto_include? module CollectionProxyPatch # The CollectionProxy just forwards exists? to the underlying scope so we need to intercept this and # force it to use size which handles fully_load properly. - def exists?(*args) + def exists?(*args, **kwargs) # We don't fully_load the association when arguments are passed to exists? since Rails always # pushes this query into the database without any caching (and it likely not a common # scenario worth optimizing). - if args.empty? && @association.fully_load? + if args.empty? && kwargs.empty? && @association.fully_load? size > 0 else - scope.exists?(*args) + scope.exists?(*args, **kwargs) end end end