rom-rails 2.0.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile +1 -1
- data/lib/generators/rom/install/templates/application_model.rb +1 -1
- data/lib/generators/rom/install/templates/types.rb +2 -2
- data/lib/rom/rails/active_record/configuration.rb +41 -59
- data/lib/rom/rails/active_record/uri_builder.rb +70 -0
- data/lib/rom/rails/railtie.rb +8 -8
- data/lib/rom/rails/version.rb +1 -1
- data/rom-rails.gemspec +1 -1
- data/spec/lib/active_record/configuration_spec.rb +71 -1
- data/spec/lib/generators/install_generator_spec.rb +3 -3
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2aa950bd64c7498d660f81d2b6f0f6206d88b81249b6c72103a598ccc4abdeb0
|
4
|
+
data.tar.gz: '04184007c2c8ec6f3adf047ccb4ab7856c9fdb0bfcf84ca4f59efc91a2cce625'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19289d877377affe80deede5ec1a195252354a6ab2ade3a2ce80b3c0c543295e57ba409e333c4199b38fdec08bea59b027f2eff47cc55af54e18dc5d7ad26884
|
7
|
+
data.tar.gz: 20199e51ec01cb205650c000bb6f79980c764ce1797634130bf73808e2038cc346aea47d6ef3aadd35ec42a8a9b14103769ac3a558bb084aa2a5f56e95a0388a
|
data/.rubocop.yml
CHANGED
@@ -31,10 +31,14 @@ AllCops:
|
|
31
31
|
- tmp/**/*
|
32
32
|
- vendor/**/*
|
33
33
|
|
34
|
+
Layout/AlignArguments:
|
35
|
+
EnforcedStyle: with_fixed_indentation
|
36
|
+
|
34
37
|
# The enforced style doesn’t match Vim’s defaults
|
35
38
|
Layout/AlignParameters:
|
36
39
|
Enabled: false
|
37
40
|
|
41
|
+
|
38
42
|
# UTF-8 is perfectly fine in comments
|
39
43
|
Style/AsciiComments:
|
40
44
|
Enabled: false
|
data/.travis.yml
CHANGED
@@ -9,6 +9,7 @@ rvm:
|
|
9
9
|
- 2.6.3
|
10
10
|
- jruby-9.2.7.0
|
11
11
|
env:
|
12
|
+
- RAILS_VERSION=6.0
|
12
13
|
- RAILS_VERSION=5.2
|
13
14
|
- RAILS_VERSION=5.1
|
14
15
|
- RAILS_VERSION=5.0
|
@@ -20,3 +21,7 @@ notifications:
|
|
20
21
|
on_success: change
|
21
22
|
on_failure: always
|
22
23
|
on_start: false
|
24
|
+
matrix:
|
25
|
+
exclude:
|
26
|
+
- rvm: 2.4.6
|
27
|
+
env: RAILS_VERSION=6.0
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
## v2.2.0 2019-09-12
|
2
|
+
|
3
|
+
### Changed
|
4
|
+
|
5
|
+
* Read new-style Rails6 configuration, inferring all defined databases (cflipse)
|
6
|
+
|
7
|
+
### Fixed
|
8
|
+
|
9
|
+
* Updated generator to produce files that are actually compatible with ROM 5.0 (cflipse)
|
10
|
+
|
11
|
+
[Compare v2.1.0...v2.2.0](https://github.com/rom-rb/rom-rails/compare/v2.1.0...v2.2.0)
|
12
|
+
|
13
|
+
## v2.1.0 2019-09-09
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
|
17
|
+
* Allow rails6 (cflipse)
|
18
|
+
|
19
|
+
[Compare v2.0.0...v2.1.0](https://github.com/rom-rb/rom-rails/compare/v2.0.0...v2.1.0)
|
20
|
+
|
1
21
|
## v2.0.0 2019-04-27
|
2
22
|
|
3
23
|
### Changed
|
data/Gemfile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require "dry/types"
|
2
2
|
|
3
3
|
module Types
|
4
|
-
include Dry
|
4
|
+
include Dry.Types
|
5
5
|
|
6
|
-
ID = Coercible::
|
6
|
+
ID = Coercible::Integer.optional.meta(primary_key: true)
|
7
7
|
|
8
8
|
# Include your own type definitions and coersions here.
|
9
9
|
# See http://dry-rb.org/gems/dry-types
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative 'uri_builder'
|
2
2
|
|
3
3
|
module ROM
|
4
4
|
module Rails
|
@@ -17,15 +17,43 @@ module ROM
|
|
17
17
|
:host
|
18
18
|
].freeze
|
19
19
|
|
20
|
+
attr_reader :configurations
|
21
|
+
attr_reader :env
|
22
|
+
attr_reader :root
|
23
|
+
attr_reader :uri_builder
|
24
|
+
|
25
|
+
def initialize(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations)
|
26
|
+
@configurations = configurations
|
27
|
+
@env = env
|
28
|
+
@root = root
|
29
|
+
|
30
|
+
@uri_builder = ROM::Rails::ActiveRecord::UriBuilder.new
|
31
|
+
end
|
32
|
+
|
20
33
|
# Returns gateway configuration for the current environment.
|
21
34
|
#
|
22
35
|
# @note This relies on ActiveRecord being initialized already.
|
23
36
|
# @param [Rails::Application]
|
24
37
|
#
|
25
38
|
# @api private
|
26
|
-
def
|
27
|
-
|
28
|
-
|
39
|
+
def call
|
40
|
+
specs = { default: build(default_configuration.symbolize_keys) }
|
41
|
+
|
42
|
+
if rails6?
|
43
|
+
configurations.configs_for(env_name: env).each do |config|
|
44
|
+
specs[config.spec_name.to_sym] = build(config.config.symbolize_keys)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
specs
|
49
|
+
end
|
50
|
+
|
51
|
+
def default_configuration
|
52
|
+
if rails6?
|
53
|
+
configurations.default_hash(env)
|
54
|
+
else
|
55
|
+
configurations.fetch(env)
|
56
|
+
end
|
29
57
|
end
|
30
58
|
|
31
59
|
# Builds a configuration hash from a flat database config hash.
|
@@ -38,68 +66,22 @@ module ROM
|
|
38
66
|
# @return [Hash]
|
39
67
|
#
|
40
68
|
# @api private
|
41
|
-
def
|
69
|
+
def build(config)
|
42
70
|
adapter = config.fetch(:adapter)
|
43
|
-
uri_options = config.except(:adapter).merge(
|
71
|
+
uri_options = config.except(:adapter).merge(
|
72
|
+
root: root,
|
73
|
+
scheme: adapter
|
74
|
+
)
|
44
75
|
other_options = config.except(*BASE_OPTIONS)
|
45
76
|
|
46
|
-
|
47
|
-
uri = if respond_to?(builder_method)
|
48
|
-
send(builder_method, uri_options)
|
49
|
-
else
|
50
|
-
generic_uri(uri_options)
|
51
|
-
end
|
52
|
-
|
53
|
-
# JRuby connection strings require special care.
|
54
|
-
if RUBY_ENGINE == 'jruby' && adapter != 'postgresql'
|
55
|
-
uri = "jdbc:#{uri}"
|
56
|
-
end
|
57
|
-
|
77
|
+
uri = uri_builder.build(adapter, uri_options)
|
58
78
|
{ uri: uri, options: other_options }
|
59
79
|
end
|
60
80
|
|
61
|
-
|
62
|
-
path = Pathname.new(config.fetch(:root)).join(config.fetch(:database))
|
63
|
-
|
64
|
-
build_uri(
|
65
|
-
scheme: 'sqlite',
|
66
|
-
host: '',
|
67
|
-
path: path.to_s
|
68
|
-
)
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.postgresql_uri(config)
|
72
|
-
generic_uri(config.merge(
|
73
|
-
host: config.fetch(:host) { '' },
|
74
|
-
scheme: 'postgres'
|
75
|
-
))
|
76
|
-
end
|
77
|
-
|
78
|
-
def self.mysql_uri(config)
|
79
|
-
if config.key?(:username) && !config.key?(:password)
|
80
|
-
config.update(password: '')
|
81
|
-
end
|
82
|
-
|
83
|
-
generic_uri(config)
|
84
|
-
end
|
85
|
-
|
86
|
-
def self.generic_uri(config)
|
87
|
-
build_uri(
|
88
|
-
scheme: config.fetch(:scheme),
|
89
|
-
user: escape_option(config[:username]),
|
90
|
-
password: escape_option(config[:password]),
|
91
|
-
host: config[:host],
|
92
|
-
port: config[:port],
|
93
|
-
path: config[:database]
|
94
|
-
)
|
95
|
-
end
|
96
|
-
|
97
|
-
def self.build_uri(attrs)
|
98
|
-
Addressable::URI.new(attrs).to_s
|
99
|
-
end
|
81
|
+
private
|
100
82
|
|
101
|
-
def
|
102
|
-
|
83
|
+
def rails6?
|
84
|
+
::ActiveRecord::VERSION::MAJOR >= 6
|
103
85
|
end
|
104
86
|
end
|
105
87
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'addressable/uri'
|
2
|
+
|
3
|
+
module ROM
|
4
|
+
module Rails
|
5
|
+
module ActiveRecord
|
6
|
+
class UriBuilder
|
7
|
+
def build(adapter, uri_options)
|
8
|
+
builder_method = :"#{adapter}_uri"
|
9
|
+
|
10
|
+
uri = if respond_to?(builder_method)
|
11
|
+
send(builder_method, uri_options)
|
12
|
+
else
|
13
|
+
generic_uri(uri_options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# JRuby connection strings require special care.
|
17
|
+
if RUBY_ENGINE == 'jruby' && adapter != 'postgresql'
|
18
|
+
uri = "jdbc:#{uri}"
|
19
|
+
end
|
20
|
+
|
21
|
+
uri
|
22
|
+
end
|
23
|
+
|
24
|
+
def sqlite3_uri(config)
|
25
|
+
path = Pathname.new(config.fetch(:root)).join(config.fetch(:database))
|
26
|
+
|
27
|
+
build_uri(
|
28
|
+
scheme: 'sqlite',
|
29
|
+
host: '',
|
30
|
+
path: path.to_s
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
def postgresql_uri(config)
|
35
|
+
generic_uri(config.merge(
|
36
|
+
host: config.fetch(:host) { '' },
|
37
|
+
scheme: 'postgres'
|
38
|
+
))
|
39
|
+
end
|
40
|
+
|
41
|
+
def mysql_uri(config)
|
42
|
+
if config.key?(:username) && !config.key?(:password)
|
43
|
+
config.update(password: '')
|
44
|
+
end
|
45
|
+
|
46
|
+
generic_uri(config)
|
47
|
+
end
|
48
|
+
|
49
|
+
def generic_uri(config)
|
50
|
+
build_uri(
|
51
|
+
scheme: config.fetch(:scheme),
|
52
|
+
user: escape_option(config[:username]),
|
53
|
+
password: escape_option(config[:password]),
|
54
|
+
host: config[:host],
|
55
|
+
port: config[:port],
|
56
|
+
path: config[:database]
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
def build_uri(attrs)
|
61
|
+
Addressable::URI.new(attrs).to_s
|
62
|
+
end
|
63
|
+
|
64
|
+
def escape_option(option)
|
65
|
+
option.nil? ? option : CGI.escape(option)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/rom/rails/railtie.rb
CHANGED
@@ -86,7 +86,11 @@ module ROM
|
|
86
86
|
|
87
87
|
# @api private
|
88
88
|
def gateways
|
89
|
-
|
89
|
+
if active_record?
|
90
|
+
load_active_record_config.each do |name, spec|
|
91
|
+
config.rom.gateways[name] ||= [:sql, spec[:uri], spec[:options]]
|
92
|
+
end
|
93
|
+
end
|
90
94
|
|
91
95
|
if config.rom.gateways.empty?
|
92
96
|
::Rails.logger.warn "It seems that you have not configured any gateways"
|
@@ -97,13 +101,9 @@ module ROM
|
|
97
101
|
config.rom.gateways
|
98
102
|
end
|
99
103
|
|
100
|
-
#
|
101
|
-
|
102
|
-
|
103
|
-
# @api private
|
104
|
-
def infer_default_gateway
|
105
|
-
spec = ROM::Rails::ActiveRecord::Configuration.call
|
106
|
-
[:sql, spec[:uri], spec[:options]]
|
104
|
+
# Attempt to infer all configured gateways from activerecord
|
105
|
+
def load_active_record_config
|
106
|
+
ROM::Rails::ActiveRecord::Configuration.new.call
|
107
107
|
end
|
108
108
|
|
109
109
|
def load_initializer
|
data/lib/rom/rails/version.rb
CHANGED
data/rom-rails.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.add_runtime_dependency 'addressable', '~> 2.3'
|
20
20
|
spec.add_runtime_dependency 'dry-core', '~> 0.3'
|
21
21
|
spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
|
22
|
-
spec.add_runtime_dependency 'railties', '>= 3.0', '< 6.
|
22
|
+
spec.add_runtime_dependency 'railties', '>= 3.0', '< 6.1'
|
23
23
|
spec.add_runtime_dependency 'rom', '~> 5.0'
|
24
24
|
|
25
25
|
spec.add_development_dependency "bundler"
|
@@ -1,15 +1,19 @@
|
|
1
1
|
require 'rom/rails/active_record/configuration'
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_record/database_configurations' if Rails::VERSION::MAJOR >= 6
|
2
4
|
|
3
5
|
RSpec.describe ROM::Rails::ActiveRecord::Configuration do
|
4
6
|
let(:root) { Pathname.new('/path/to/app') }
|
5
7
|
|
8
|
+
subject(:configuration) { described_class.new(root: root) }
|
9
|
+
|
6
10
|
def uri_for(config)
|
7
11
|
result = read(config)
|
8
12
|
result.is_a?(Hash) ? result[:uri] : result
|
9
13
|
end
|
10
14
|
|
11
15
|
def read(config)
|
12
|
-
|
16
|
+
configuration.build(config)
|
13
17
|
end
|
14
18
|
|
15
19
|
def parse(uri)
|
@@ -130,4 +134,70 @@ RSpec.describe ROM::Rails::ActiveRecord::Configuration do
|
|
130
134
|
end
|
131
135
|
end
|
132
136
|
end
|
137
|
+
|
138
|
+
if Rails::VERSION::MAJOR >= 6
|
139
|
+
context "with an activerecord 6 configuration" do
|
140
|
+
subject(:configuration) { described_class.new(root: root, configurations: railsconfig, env: "test") }
|
141
|
+
let(:railsconfig) { ActiveRecord::DatabaseConfigurations.new(config_file) }
|
142
|
+
|
143
|
+
context "with only a single database" do
|
144
|
+
let(:config_file) {
|
145
|
+
{
|
146
|
+
test: {
|
147
|
+
adapter: 'mysql',
|
148
|
+
host: 'example.com',
|
149
|
+
database: 'test',
|
150
|
+
username: 'user',
|
151
|
+
encoding: 'utf8'
|
152
|
+
}
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
it "returns the default hash" do
|
157
|
+
expected_uri = uri_for(config_file[:test])
|
158
|
+
expect(configuration.call[:default]).to match(uri: expected_uri, options: { encoding: 'utf8' })
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context "with multiple configured databases" do
|
163
|
+
let(:config_file) {
|
164
|
+
{
|
165
|
+
test: {
|
166
|
+
reader: {
|
167
|
+
adapter: 'mysql',
|
168
|
+
host: 'example.com',
|
169
|
+
database: 'test_reader',
|
170
|
+
username: 'user',
|
171
|
+
encoding: 'utf8'
|
172
|
+
},
|
173
|
+
writer: {
|
174
|
+
adapter: 'mysql',
|
175
|
+
host: 'write.example.com',
|
176
|
+
database: 'test_writer',
|
177
|
+
username: 'user',
|
178
|
+
encoding: 'utf8'
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
it "configures the first database as the default" do
|
185
|
+
expected_uri = uri_for(config_file[:test][:reader])
|
186
|
+
expect(configuration.call[:default]).to match(uri: expected_uri, options: {encoding: 'utf8'})
|
187
|
+
end
|
188
|
+
|
189
|
+
it "returns each configured database" do
|
190
|
+
options = { encoding: 'utf8' }
|
191
|
+
expected_reader_uri = uri_for(config_file[:test][:reader])
|
192
|
+
expected_writer_uri = uri_for(config_file[:test][:writer])
|
193
|
+
|
194
|
+
expect(configuration.call).to include(
|
195
|
+
reader: { uri: expected_reader_uri, options: options },
|
196
|
+
writer: { uri: expected_writer_uri, options: options }
|
197
|
+
)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
133
203
|
end
|
@@ -42,9 +42,9 @@ RSpec.describe ROM::Generators::InstallGenerator, type: :generator do
|
|
42
42
|
require "dry/types"
|
43
43
|
|
44
44
|
module Types
|
45
|
-
include Dry
|
45
|
+
include Dry.Types
|
46
46
|
|
47
|
-
ID = Coercible::
|
47
|
+
ID = Coercible::Integer.optional.meta(primary_key: true)
|
48
48
|
|
49
49
|
# Include your own type definitions and coersions here.
|
50
50
|
# See http://dry-rb.org/gems/dry-types
|
@@ -69,7 +69,7 @@ RSpec.describe ROM::Generators::InstallGenerator, type: :generator do
|
|
69
69
|
def self.inherited(base)
|
70
70
|
super
|
71
71
|
|
72
|
-
base.
|
72
|
+
base.transform_types(&:omittable)
|
73
73
|
|
74
74
|
base.extend ActiveModel::Naming
|
75
75
|
base.include ActiveModel::Conversion
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Flipse
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-09-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: addressable
|
@@ -62,7 +62,7 @@ dependencies:
|
|
62
62
|
version: '3.0'
|
63
63
|
- - "<"
|
64
64
|
- !ruby/object:Gem::Version
|
65
|
-
version: '6.
|
65
|
+
version: '6.1'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
68
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -72,7 +72,7 @@ dependencies:
|
|
72
72
|
version: '3.0'
|
73
73
|
- - "<"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '6.
|
75
|
+
version: '6.1'
|
76
76
|
- !ruby/object:Gem::Dependency
|
77
77
|
name: rom
|
78
78
|
requirement: !ruby/object:Gem::Requirement
|
@@ -179,6 +179,7 @@ files:
|
|
179
179
|
- lib/generators/rom/repository_generator.rb
|
180
180
|
- lib/rom-rails.rb
|
181
181
|
- lib/rom/rails/active_record/configuration.rb
|
182
|
+
- lib/rom/rails/active_record/uri_builder.rb
|
182
183
|
- lib/rom/rails/configuration.rb
|
183
184
|
- lib/rom/rails/controller_extension.rb
|
184
185
|
- lib/rom/rails/inflections.rb
|
@@ -279,8 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
279
280
|
- !ruby/object:Gem::Version
|
280
281
|
version: '0'
|
281
282
|
requirements: []
|
282
|
-
|
283
|
-
rubygems_version: 2.7.7
|
283
|
+
rubygems_version: 3.0.3
|
284
284
|
signing_key:
|
285
285
|
specification_version: 4
|
286
286
|
summary: Integrate Ruby Object Mapper with Rails
|