Module: RactorRailsShim::ReflectionAbstractPatch

Includes:
ReflectionMemoPatch
Defined in:
lib/ractor_rails_shim/patches/activerecord_reflection.rb

Instance Method Summary collapse

Methods included from ReflectionMemoPatch

#rrs_refl_cache

Instance Method Details

#class_nameObject



30
31
32
# File 'lib/ractor_rails_shim/patches/activerecord_reflection.rb', line 30

def class_name
  rrs_refl_cache[[object_id, :class_name]] ||= -(options[:class_name] || derive_class_name).to_s
end

#counter_cache_columnObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ractor_rails_shim/patches/activerecord_reflection.rb', line 34

def counter_cache_column
  rrs_refl_cache[[object_id, :counter_cache_column]] ||= begin
    counter_cache = options[:counter_cache]

    if belongs_to?
      if counter_cache
        counter_cache[:column] || -"#{active_record.name.demodulize.underscore.pluralize}_count"
      end
    else
      -((counter_cache && -counter_cache[:column]) || "#{name}_count")
    end
  end
end

#inverse_ofObject



48
49
50
51
52
# File 'lib/ractor_rails_shim/patches/activerecord_reflection.rb', line 48

def inverse_of
  return unless inverse_name

  rrs_refl_cache[[object_id, :inverse_of]] ||= klass._reflect_on_association inverse_name
end

#inverse_which_updates_counter_cacheObject Also known as: inverse_updates_counter_cache?

inverse_which_updates_counter_cache lazily memoizes on the (frozen, shared) reflection — which raises FrozenError in a worker Ractor when a dependent: cascade touches a counter_cache (e.g. deleting a parent whose child belongs_to it with counter_cache: true). Route the memoization through the per-worker cache instead, mirroring the other cache-backed reflection accessors above.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ractor_rails_shim/patches/activerecord_reflection.rb', line 61

def inverse_which_updates_counter_cache
  cache = rrs_refl_cache
  key = [object_id, :inverse_which_updates_counter_cache]
  return cache[key] if cache.key?(key)
  cache[key] =
    if counter_cache_column
      inverse_candidates = inverse_of ? [inverse_of] : klass.reflect_on_all_associations(:belongs_to)
      inverse_candidates.find do |inverse|
        inverse.counter_cache_column == counter_cache_column && (inverse.polymorphic? || inverse.klass == active_record)
      end
    end
end