Module: SlavePoolsModule::ActiveRecordExtensions::ClassMethods

Defined in:
lib/slave_pools/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#cache(&block) ⇒ Object

Make sure caching always uses master connection



30
31
32
33
34
35
36
# File 'lib/slave_pools/active_record_extensions.rb', line 30

def cache(&block)
  if ActiveRecord::Base.configurations.blank?
    yield
  else
    ActiveRecord::Base.connection.cache(&block)
  end
end

#hijack_connectionObject



43
44
45
46
47
48
49
50
51
# File 'lib/slave_pools/active_record_extensions.rb', line 43

def hijack_connection
  return if ConnectionProxy.master_models.include?(self.to_s)
  # logger.info "[SlavePools] hijacking connection for #{self.to_s}" # commenting out noisy logging
  class << self
    def connection
      self.connection_proxy
    end
  end
end

#inherited(child) ⇒ Object



38
39
40
41
# File 'lib/slave_pools/active_record_extensions.rb', line 38

def inherited(child)
  super
  child.hijack_connection
end

#transaction(options = {}, &block) ⇒ Object

Make sure transactions always switch to the master



21
22
23
24
25
26
27
# File 'lib/slave_pools/active_record_extensions.rb', line 21

def transaction(options = {}, &block)
  if self.connection.kind_of?(ConnectionProxy)
    super
  else
    self.connection_proxy.with_master { super }
  end
end