module SwitchPoint::Model::ClassMethods

Public Instance Methods

switch_point_proxy() click to toggle source
# File lib/switch_point/model.rb, line 50
def switch_point_proxy
  if defined?(@switch_point_name)
    ProxyRepository.checkout(@switch_point_name)
  elsif self == ActiveRecord::Base
    nil
  else
    superclass.switch_point_proxy
  end
end
transaction_with(*models, &block) click to toggle source
# File lib/switch_point/model.rb, line 60
def transaction_with(*models, &block)
  unless can_transaction_with?(*models)
    raise Error.new("switch_point's model names must be consistent")
  end

  with_writable do
    transaction(&block)
  end
end
use_switch_point(name) click to toggle source
# File lib/switch_point/model.rb, line 45
def use_switch_point(name)
  assert_existing_switch_point!(name)
  @switch_point_name = name
end
with_readonly(&block) click to toggle source
# File lib/switch_point/model.rb, line 29
def with_readonly(&block)
  if switch_point_proxy
    switch_point_proxy.with_readonly(&block)
  else
    raise UnconfiguredError.new("#{name} isn't configured to use switch_point")
  end
end
with_writable(&block) click to toggle source
# File lib/switch_point/model.rb, line 37
def with_writable(&block)
  if switch_point_proxy
    switch_point_proxy.with_writable(&block)
  else
    raise UnconfiguredError.new("#{name} isn't configured to use switch_point")
  end
end

Private Instance Methods

assert_existing_switch_point!(name) click to toggle source
# File lib/switch_point/model.rb, line 72
def assert_existing_switch_point!(name)
  SwitchPoint.config.fetch(name)
end
can_transaction_with?(*models) click to toggle source
# File lib/switch_point/model.rb, line 76
def can_transaction_with?(*models)
  writable_switch_points = [self, *models].map do |model|
    if model.instance_variable_defined?(:@switch_point_name)
      SwitchPoint.config.model_name(
        model.instance_variable_get(:@switch_point_name),
        :writable
      )
    end
  end

  writable_switch_points.uniq.size == 1
end