Module: MultiDb::ActiveRecordExtensions::ClassMethods

Defined in:
lib/multi_db/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#cache(&block) ⇒ Object

make caching always use the ConnectionProxy



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

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

#hijack_connectionObject



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

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) ⇒ Object



39
40
41
42
# File 'lib/multi_db/active_record_extensions.rb', line 39

def inherited(child)
  super
  child.hijack_connection
end

#transaction(&block) ⇒ Object

Make sure transactions always switch to the master



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

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