Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/slavery/active_record/base.rb

Direct Known Subclasses

Slavery::ConnectionHolder

Class Method Summary collapse

Class Method Details

.connectionObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/slavery/active_record/base.rb', line 6

def connection
  case Thread.current[:slavery]
  when :slave
    Slavery.connection_holder.connection_without_slavery
  when :master, NilClass
    connection_without_slavery
  else
    raise Slavery::Error.new("invalid target: #{Thread.current[:slavery]}")
  end
end

.connection_without_slaveryObject



4
# File 'lib/slavery/active_record/base.rb', line 4

alias_method :connection_without_slavery, :connection

.on_slaveObject

Generate scope at top level e.g. User.on_slave



18
19
20
21
22
23
24
# File 'lib/slavery/active_record/base.rb', line 18

def on_slave
  # Why where(nil)?
  # http://stackoverflow.com/questions/18198963/with-rails-4-model-scoped-is-deprecated-but-model-all-cant-replace-it
  context = where(nil)
  context.slavery_target = :slave
  context
end