# typed: strict
module CounterCacheWithFallback
  extend T::Sig

  sig { params(association_name: Symbol).returns(T.untyped) }
  def self.[](association_name)
    counter_cache_name = :"#{association_name}_count"
    Module.new do
      define_method(:size) do
        T.bind(self, ActiveRecord::Associations::CollectionProxy)
        assumed = proxy_association.owner.send(counter_cache_name)
        assumed ||= proxy_association.owner.send(association_name).count
        assumed
      end
    end
  end
end
