module MultiDb::ActiveRecordExtensions::ClassMethods

Public Class Methods

connection() click to toggle source
# File lib/multi_db/active_record_extensions.rb, line 47
def connection
  self.connection_proxy
end

Public Instance Methods

cache() { || ... } click to toggle source

make caching always use the ConnectionProxy

# File lib/multi_db/active_record_extensions.rb, line 30
def cache(&block)
  if ActiveRecord::Base.configurations.blank?
    yield
  else
    self.connection_proxy.cache(&block)
  end
end
hijack_connection() click to toggle source
# File lib/multi_db/active_record_extensions.rb, line 43
def hijack_connection
  return if ConnectionProxy.master_models.include?(self.to_s)
  logger.info "[MULTIDB] hijacking connection for #{self.to_s}"
  class << self
    def connection
      self.connection_proxy
    end
  end
end
inherited(child) click to toggle source
# File lib/multi_db/active_record_extensions.rb, line 38
def inherited(child)
  super
  child.hijack_connection
end
transaction(options = {}, &block) click to toggle source

Make sure transactions always switch to the master

# File lib/multi_db/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