Skip to content

Commit

Permalink
active record patches: use Ruby 3 argument forwarding (#158)
Browse files Browse the repository at this point in the history
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 <kyrofa@ubuntu.com>
  • Loading branch information
kyrofa authored Sep 6, 2024
1 parent 4af26ce commit 9193940
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/goldiloader/active_record_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9193940

Please # to comment.