spec for habtm counter cache

This commit is contained in:
Dylan Knutson
2025-07-21 16:58:58 +00:00
parent 025cbd3f5c
commit fb7912e353
3 changed files with 13 additions and 34 deletions

View File

@@ -67,7 +67,6 @@ module HasAuxTable
setup_initialize_hook!(config)
setup_changed_hook!(config)
setup_save_hook!(config)
setup_reload_hook!(config)
setup_attributes_hook!(config)
setup_relation_extensions!(config)
setup_attribute_getter_setter_hooks!(config)
@@ -328,38 +327,6 @@ module HasAuxTable
end
end
sig { params(config: AuxTableConfig).void }
def setup_reload_hook!(config)
refresh_object =
lambda do |target, fresh|
target.instance_variable_set(
:@association_cache,
fresh.instance_variable_get(:@association_cache)
)
target
.instance_variable_get(:@association_cache)
.each_value { |association| association.owner = target }
target.instance_variable_set(
:@attributes,
fresh.instance_variable_get(:@attributes)
)
end
self.define_method(:reload) do |*args|
T.bind(self, ActiveRecord::Base)
aux_model = config.aux_model_for(self)
fresh_object = self.class.find(id)
refresh_object.call(self, fresh_object)
refresh_object.call(
aux_model,
fresh_object.association(config.aux_association_name).target
)
self
end
end
sig { params(config: AuxTableConfig).void }
def setup_attributes_hook!(config)
attributes_method = self.instance_method(:attributes)

View File

@@ -192,7 +192,6 @@ module HasAuxTable
aux_model = config.aux_model_for(self)
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

View File

@@ -957,6 +957,19 @@ RSpec.describe HasAuxTable do
verify_counter_cache(d, :ad_joins, 1)
end
end
it "is a join table connecting two subclasses" do
a = ModelA1.create!(a_field1: "a1_0", a1_field1: "a1_0")
ds = 3.times.map { |i| ModelD1.create!(d1_field1: "d1_#{i}") }
verify_counter_cache(a, :ad_joins, nil)
ds.each { |d| verify_counter_cache(d, :ad_joins, nil) }
ds.each_with_index do |d, i|
a.ad_joins.create!(model_d: d)
verify_counter_cache(a, :ad_joins, i + 1)
verify_counter_cache(d, :ad_joins, 1)
end
end
end
describe "joins model with aux tables" do