class ROM::Rails::ActiveRecord::Configuration

A helper class to derive `rom-sql` configuration from ActiveRecord.

@private

Constants

BASE_OPTIONS

Attributes

configurations[R]
env[R]
root[R]
uri_builder[R]

Public Class Methods

new(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations) click to toggle source
# File lib/rom/rails/active_record/configuration.rb, line 25
def initialize(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations)
  @configurations = configurations
  @env  = env
  @root = root

  @uri_builder = ROM::Rails::ActiveRecord::UriBuilder.new
end

Public Instance Methods

build(config) click to toggle source

Builds a configuration hash from a flat database config hash.

This is used to support typical database.yml-complaint configs. It also uses adapter interface for things that are adapter-specific like handling schema naming.

@param [Hash,String] @return [Hash]

@api private

# File lib/rom/rails/active_record/configuration.rb, line 69
def build(config)
  adapter = config.fetch(:adapter)
  uri_options = config.except(:adapter).merge(
    root: root,
    scheme: adapter
  )
  other_options = config.except(*BASE_OPTIONS)

  uri = uri_builder.build(adapter, uri_options)
  { uri: uri, options: other_options }
end
call() click to toggle source

Returns gateway configuration for the current environment.

@note This relies on ActiveRecord being initialized already. @param [Rails::Application]

@api private

# File lib/rom/rails/active_record/configuration.rb, line 39
def call
  specs = { default: build(default_configuration.symbolize_keys) }

  if rails6?
    configurations.configs_for(env_name: env).each do |config|
      specs[config.spec_name.to_sym] = build(config.config.symbolize_keys)
    end
  end

  specs
end
default_configuration() click to toggle source
# File lib/rom/rails/active_record/configuration.rb, line 51
def default_configuration
  if rails6?
    configurations.default_hash(env)
  else
    configurations.fetch(env)
  end
end

Private Instance Methods

rails6?() click to toggle source
# File lib/rom/rails/active_record/configuration.rb, line 83
def rails6?
  ::ActiveRecord::VERSION::MAJOR >= 6
end