Class: ActiveRecord::Turntable::MasterShard

Inherits:
Shard
  • Object
show all
Defined in:
lib/active_record/turntable/master_shard.rb

Constant Summary

Constants inherited from Shard

Shard::DEFAULT_CONFIG

Instance Attribute Summary

Attributes inherited from Shard

#name

Instance Method Summary collapse

Methods inherited from Shard

#connection

Constructor Details

#initialize(klass) ⇒ MasterShard

Returns a new instance of MasterShard.



3
4
5
6
7
8
# File 'lib/active_record/turntable/master_shard.rb', line 3

def initialize(klass)
  (klass and klass.connection_pool) or
    raise MasterShardNotConnected, "connection_pool is nil"
  @klass = klass
  @name  = 'master'
end

Instance Method Details

#connection_poolObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_record/turntable/master_shard.rb', line 10

def connection_pool
  if ActiveRecord::Base == @klass
    ActiveRecord::Base.connection_pool
  else
    # use parentclass connection which is turntable disabled
    klass = @klass.superclass
    candidate_connection_pool = nil
    while !candidate_connection_pool
      if klass == ActiveRecord::Base or !klass.turntable_enabled?
        candidate_connection_pool = klass.connection_pool
      else
        klass = klass.superclass
      end
    end
    candidate_connection_pool
  end
end