Class: Ensql::ActiveRecordAdapter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Adapter
Defined in:
lib/ensql/active_record_adapter.rb

Overview

Wraps an ActiveRecord connection pool to implement the Adapter interface for ActiveRecord. Requires an ActiveRecord connection to be configured and established. By default, uses the connection pool on ActiveRecord::Base. Other pools can be passed to the constructor.

Examples:

require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'mydb')
Ensql.adapter = Ensql::ActiveRecordAdapter.new
# Use database configuration for the Widget model instead
Ensql.adapter = Ensql::ActiveRecordAdapter.new(Widget)

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Adapter

#fetch_first_column, #fetch_first_field, #fetch_first_row

Constructor Details

#initialize(base = ActiveRecord::Base) ⇒ ActiveRecordAdapter

Returns a new instance of ActiveRecordAdapter.

Parameters:

  • base (Class) (defaults to: ActiveRecord::Base)

    an ActiveRecord class to source connections from



53
54
55
# File 'lib/ensql/active_record_adapter.rb', line 53

def initialize(base = ActiveRecord::Base)
  @base = base
end

Class Method Details

.pool(base = ActiveRecord::Base) ⇒ PoolWrapper

Wrap the raw connections from an Active Record connection pool. This allows us to safely checkout the underlying database connection for use in a database specific adapter.

Ensql.adapter = MySqliteAdapter.new(ActiveRecordAdapter.pool)

Parameters:

  • base (Class) (defaults to: ActiveRecord::Base)

    an ActiveRecord class to source connections from

Returns:



38
39
40
41
42
# File 'lib/ensql/active_record_adapter.rb', line 38

def self.pool(base = ActiveRecord::Base)
  PoolWrapper.new do |client_block|
    base.connection_pool.with_connection { |connection| client_block.call connection.raw_connection }
  end
end