Module: SwitchConnection::Model

Defined in:
lib/switch_connection/model.rb

Defined Under Namespace

Modules: ClassMethods, MonkeyPatch

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/switch_connection/model.rb', line 8

def self.included(model)
  super
  model.singleton_class.class_eval do
    include ClassMethods
    prepend MonkeyPatch
    def find_by_sql(*args, &block)
      if switch_point_proxy && connection.open_transactions.zero?
        with_slave do
          super
        end
      else
        super
      end
    end

    def count_by_sql(*args, &block)
      if switch_point_proxy && connection.open_transactions.zero?
        with_slave do
          super
        end
      else
        super
      end
    end
  end
end

Instance Method Details

#reload(*args, &block) ⇒ Object



51
52
53
54
55
# File 'lib/switch_connection/model.rb', line 51

def reload(*args, &block)
  self.class.with_master do
    super(*args, &block)
  end
end

#transaction_with(*models, &block) ⇒ Object



47
48
49
# File 'lib/switch_connection/model.rb', line 47

def transaction_with(*models, &block)
  self.class.transaction_with(*models, &block)
end

#with_master(&block) ⇒ Object



39
40
41
# File 'lib/switch_connection/model.rb', line 39

def with_master(&block)
  self.class.with_master(&block)
end

#with_slave(&block) ⇒ Object



35
36
37
# File 'lib/switch_connection/model.rb', line 35

def with_slave(&block)
  self.class.with_slave(&block)
end

#with_switch_point(new_switch_point_name, &block) ⇒ Object



43
44
45
# File 'lib/switch_connection/model.rb', line 43

def with_switch_point(new_switch_point_name, &block)
  self.class.with_switch_point(new_switch_point_name, &block)
end