Module: ActiveRecord::ConnectionHandling

Defined in:
lib/active_record/connection_adapters/chronomodel_adapter.rb

Defined Under Namespace

Modules: Connectionadapters

Instance Method Summary collapse

Instance Method Details

#chronomodel_connection(config) ⇒ Object

Install the new adapter in ActiveRecord. This approach is required because the PG adapter defines add_column itself, thus making impossible to use super() in overridden Module methods.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/connection_adapters/chronomodel_adapter.rb', line 10

def chronomodel_connection(config) # :nodoc:
  conn_params = config.symbolize_keys

  conn_params.delete_if { |_, v| v.nil? }

  # Map ActiveRecords param names to PGs.
  conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
  conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]

  # Forward only valid config params to PG::Connection.connect.
  valid_conn_param_keys = if ActiveRecord::VERSION::MAJOR == 4
                            VALID_CONN_PARAMS
                          else
                            PG::Connection.conndefaults_hash.keys + [:requiressl]
                          end
  conn_params.slice!(*valid_conn_param_keys)

  # The postgres drivers don't allow the creation of an unconnected PG::Connection object,
  # so just pass a nil connection object for the time being.
  adapter = ChronoModel::Adapter.new(nil, logger, conn_params, config)

  unless adapter.chrono_supported?
    raise ChronoModel::Error, "Your database server is not supported by ChronoModel. "\
      "Currently, only PostgreSQL >= 9.3 is supported."
  end

  adapter.chrono_setup!

  return adapter
end