Class: ROM::Rails::ActiveRecord::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/rails/active_record/configuration.rb

Overview

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

Constant Summary collapse

BASE_OPTIONS =
[
  :root,
  :adapter,
  :database,
  :password,
  :username,
  :hostname,
  :root
].freeze

Class Method Summary collapse

Class Method Details

.build(config) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • (Hash, String)

Returns:

  • (Hash)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rom/rails/active_record/configuration.rb', line 41

def self.build(config)
  adapter = config.fetch(:adapter)
  uri_options = config.except(:adapter).merge(scheme: adapter)
  other_options = config.except(*BASE_OPTIONS)

  builder_method = :"#{adapter}_uri"
  uri = if respond_to?(builder_method)
          send(builder_method, uri_options)
        else
          generic_uri(uri_options)
        end

  # JRuby connection strings require special care.
  uri = if RUBY_ENGINE == 'jruby' && adapter != 'postgresql'
          "jdbc:#{uri}"
        else
          uri
        end

  { uri: uri, options: other_options }
end

.build_uri(attrs) ⇒ Object



99
100
101
# File 'lib/rom/rails/active_record/configuration.rb', line 99

def self.build_uri(attrs)
  Addressable::URI.new(attrs).to_s
end

.callObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This relies on ActiveRecord being initialized already.

Returns repository configuration for the current environment.

Parameters:

  • (Rails::Application)


26
27
28
29
# File 'lib/rom/rails/active_record/configuration.rb', line 26

def self.call
  configuration = ::ActiveRecord::Base.configurations.fetch(::Rails.env)
  build(configuration.symbolize_keys.update(root: ::Rails.root))
end

.generic_uri(config) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/rom/rails/active_record/configuration.rb', line 88

def self.generic_uri(config)
  build_uri(
    scheme: config.fetch(:scheme),
    user: config[:username],
    password: config[:password],
    host: config[:host],
    port: config[:port],
    path: config[:database]
  )
end

.mysql_uri(config) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/rom/rails/active_record/configuration.rb', line 80

def self.mysql_uri(config)
  if config.key?(:username) && !config.key?(:password)
    config.update(password: '')
  end

  generic_uri(config)
end

.postgresql_uri(config) ⇒ Object



73
74
75
76
77
78
# File 'lib/rom/rails/active_record/configuration.rb', line 73

def self.postgresql_uri(config)
  generic_uri(config.merge(
    host: config.fetch(:host) { '' },
    scheme: 'postgres'
  ))
end

.sqlite3_uri(config) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/rom/rails/active_record/configuration.rb', line 63

def self.sqlite3_uri(config)
  path = Pathname.new(config.fetch(:root)).join(config.fetch(:database))

  build_uri(
    scheme: 'sqlite',
    host: '',
    path: path.to_s
  )
end