/
railtie.rb
152 lines (121 loc) · 4.08 KB
/
railtie.rb
1
2
3
4
5
6
7
8
9
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require 'rails'
require 'rom/rails/inflections'
require 'rom/rails/configuration'
require 'rom/rails/controller_extension'
require 'rom/rails/active_record/configuration'
Spring.after_fork { ROM::Rails::Railtie.disconnect } if defined?(Spring)
module ROM
module Rails
class Railtie < ::Rails::Railtie
COMPONENT_DIRS = %w(relations mappers commands).freeze
MissingGatewayConfigError = Class.new(StandardError)
# Make `ROM::Rails::Configuration` instance available to the user via
# `Rails.application.config` before other initializers run.
config.before_configuration do |_app|
config.rom = Configuration.new
end
initializer 'rom.configure_action_controller' do
ActiveSupport.on_load(:action_controller) do
ActionController::Base.send(:include, ControllerExtension)
ActionController::API.send(:include, ControllerExtension) if defined?(ActionController::API)
end
end
initializer 'rom.adjust_eager_load_paths' do |app|
paths =
auto_registration_paths.inject([]) do |result, root_path|
result.concat(COMPONENT_DIRS.map { |dir| ::Rails.root.join(root_path, dir).to_s })
end
app.config.eager_load_paths -= paths
end
rake_tasks do
load "rom/rails/tasks/db.rake" unless active_record?
end
# Reload ROM-related application code on each request.
config.to_prepare do |_config|
ROM.env = Railtie.create_container
end
console do |_app|
Railtie.configure_console_logger
end
# Behaves like `Railtie#configure` if the given block does not take any
# arguments. Otherwise yields the ROM configuration to the block.
#
# @example
# ROM::Rails::Railtie.configure do |config|
# config.gateways[:default] = [:yaml, 'yaml:///data']
# config.auto_registration_paths += [MyEngine.root]
# end
#
# @api public
def configure(&block)
config.rom = Configuration.new unless config.respond_to?(:rom)
if block.arity == 1
block.call(config.rom)
else
super
end
end
def create_configuration
ROM::Configuration.new(gateways)
end
# @api private
def create_container
configuration = create_configuration
auto_registration_paths.each do |root_path|
configuration.auto_registration(::Rails.root.join(root_path), namespace: false)
end
ROM.container(configuration)
end
# @api private
def gateways
if active_record?
load_active_record_config.each do |name, spec|
config.rom.gateways[name] ||= [:sql, spec[:uri], spec[:options]]
end
end
if config.rom.gateways.empty?
::Rails.logger.warn "It seems that you have not configured any gateways"
config.rom.gateways[:default] = [ :memory, "memory://test" ]
end
config.rom.gateways
end
# Attempt to infer all configured gateways from activerecord
def load_active_record_config
ROM::Rails::ActiveRecord::Configuration.new.call
end
def load_initializer
load "#{root}/config/initializers/rom.rb"
rescue LoadError
# do nothing
end
# @api private
def disconnect
container.disconnect unless container.nil?
end
# @api private
def root
::Rails.root
end
def container
ROM.env
end
def auto_registration_paths
config.rom.auto_registration_paths + [root]
end
# @api private
def active_record?
defined?(::ActiveRecord)
end
# @api private
def std_err_out_logger?
ActiveSupport::Logger.logger_outputs_to?(::Rails.logger, STDERR, STDOUT)
end
# @api private
def configure_console_logger
return if active_record? || std_err_out_logger?
console = ActiveSupport::Logger.new(STDERR)
::Rails.logger.extend ActiveSupport::Logger.broadcast console
end
end
end
end