module ActiveRecord::ConnectionHandling

Public Instance Methods

connected_shards(shards: nil) click to toggle source
# File lib/ar_shard/active_record.rb, line 19
def connected_shards(shards: nil)
  @connected_shards ||= begin
    config_data = parse_config(shards)
    @@klass_name = self.name
    connections = {}

    shard_model.add_shared_override!

    config_data.each do |shard|
      key, conf = shard.flatten
      handler = lookup_connect(key.to_sym)

      connections[key.to_sym] = handler.establish_connection(conf)
    end

    connections
  end
end
lookup_connect(handler_key) click to toggle source
# File lib/ar_shard/active_record.rb, line 49
def lookup_connect(handler_key)
  if defined?(connection_handlers)
    connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
  else
    @@connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
  end
end
parse_config(config) click to toggle source
# File lib/ar_shard/active_record.rb, line 38
def parse_config(config)
  return config.call if config.is_a?(Proc)
  return config if config.is_a?(Array)

  raise ARShard::Error.new 'Shard config should be Array or Proc'
end
shard_model() click to toggle source
# File lib/ar_shard/active_record.rb, line 45
def shard_model
  @shard_model ||= @@klass_name.constantize
end
with_shard(handler_key) { || ... } click to toggle source
# File lib/ar_shard/active_record.rb, line 57
def with_shard(handler_key, &blk)
  shard_model.isolated_connection = connected_shards[handler_key].connection
  return_value = yield
  return_value.load if return_value.is_a? ActiveRecord::Relation
  return_value
ensure
  shard_model.isolated_connection = nil
end