Module: Dynashard::ProxyExtensions

Defined in:
lib/dynashard/associations.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/dynashard/associations.rb', line 3

def self.included(base)
  base.alias_method_chain :initialize, :dynashard
end

Instance Method Details

#initialize_with_dynashard(owner, reflection) ⇒ Object

Initialize an association proxy. If the proxy target needs to be sharded, swap in a reflection with a sharded “klass” to ensure that the shard connection is always used to manage records in the target model.



10
11
12
13
14
15
16
17
18
# File 'lib/dynashard/associations.rb', line 10

def initialize_with_dynashard(owner, reflection)
  if needs_sharded_reflection?(owner, reflection)
    reflection = Dynashard.reflection_for(owner, reflection)
    if reflection.through_reflection != false && reflection.through_reflection.klass.sharding_enabled?
      reflection.instance_variable_set('@through_reflection', Dynashard.reflection_for(owner, reflection.through_reflection))
    end
  end
  initialize_without_dynashard(owner, reflection)
end