counter cache

This commit is contained in:
Dylan Knutson
2025-07-21 06:03:48 +00:00
parent 3a8d71e2f7
commit e234b48e7f
4 changed files with 167 additions and 42 deletions

View File

@@ -235,10 +235,20 @@ module HasAuxTable
if self.method_defined?(column_name.to_sym)
raise "invariant: method #{column_name} already defined"
end
config.define_aux_attribute_delegate(column_name)
config.define_aux_attribute_delegate(:"#{column_name}?")
config.define_aux_attribute_delegate(:"#{column_name}=")
[
"",
"_in_database",
"?",
"=",
"_changed?",
"_change",
%w[clear_ _change]
].each do |mod|
prefix, suffix = mod.is_a?(Array) ? mod : ["", mod]
config.define_aux_attribute_delegate(
:"#{prefix}#{column_name}#{suffix}"
)
end
end
result

View File

@@ -190,7 +190,10 @@ module HasAuxTable
main_class.define_method(method_name) do |*args, **kwargs, &block|
T.bind(self, ActiveRecord::Base)
aux_model = config.aux_model_for(self)
T.unsafe(aux_model).public_send(method_name, *args, **kwargs, &block)
ret =
T.unsafe(aux_model).public_send(method_name, *args, **kwargs, &block)
puts "#{self.class.name}##{method_name} -> #{config.aux_association_name} -> (#{args.inspect} => #{ret})"
ret
end
end