Module: Switchman::ActiveRecord::Reflection::AssociationScopeCache

Defined in:
lib/switchman/active_record/reflection.rb

Instance Method Summary collapse

Instance Method Details

#association_scope_cache(conn, owner, &block) ⇒ Object

cache association scopes by shard. this technically belongs on AssociationReflection, but we put it on ThroughReflection as well, instead of delegating to its internal HasManyAssociation, losing its proper klass



29
30
31
32
33
34
35
36
37
38
# File 'lib/switchman/active_record/reflection.rb', line 29

def association_scope_cache(conn, owner, &block)
  key = conn.prepared_statements
  if polymorphic?
    key = [key, owner._read_attribute(@foreign_type)]
  end
  key = [key, shard(owner).id].flatten
  @association_scope_cache[key] ||= @scope_lock.synchronize {
    @association_scope_cache[key] ||= (::Rails.version >= "5.2" ? ::ActiveRecord::StatementCache.create(conn, &block) : block.call)
  }
end

#initialize(*args) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/switchman/active_record/reflection.rb', line 16

def initialize(*args)
  super
  # on ThroughReflection, these won't be initialized (cause it doesn't
  # inherit from AssociationReflection), so make sure they're
  # initialized here
  @association_scope_cache ||= {}
  @scope_lock ||= Mutex.new
end