schema_associations 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/prs.yml +39 -0
- data/Gemfile +2 -0
- data/README.md +18 -2
- data/Rakefile +2 -0
- data/gemfiles/activerecord-6.1/Gemfile.base +4 -0
- data/gemfiles/activerecord-6.1/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-6.1/Gemfile.postgresql +10 -0
- data/gemfiles/activerecord-6.1/Gemfile.sqlite3 +10 -0
- data/gemfiles/activerecord-7.0/Gemfile.base +4 -0
- data/gemfiles/activerecord-7.0/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-7.0/Gemfile.postgresql +10 -0
- data/gemfiles/activerecord-7.0/Gemfile.sqlite3 +10 -0
- data/init.rb +2 -0
- data/lib/schema_associations/active_record/associations.rb +3 -1
- data/lib/schema_associations/version.rb +3 -1
- data/lib/schema_associations.rb +2 -0
- data/schema_associations.gemspec +5 -4
- data/schema_dev.yml +3 -0
- data/spec/association_spec.rb +147 -134
- data/spec/spec_helper.rb +4 -12
- metadata +17 -11
- data/spec/schema_associations.sqlite3 +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec0cfbd6657715659bd5a2d60d2b9c149952d84be81e0f6ca966fe29d28abb01
|
4
|
+
data.tar.gz: '0873c33c130ceebf4b01d5e3eebdc764810a8c11a0666172db16e5a08817a92d'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1c51343a4da26da68aea83231d7e9af020acb330822cb8a4c0c969af53dd47266c0d1f5d9ff2f9b85f66fc6494a679ae9428d8be2263502ee375a95f6a1d25e
|
7
|
+
data.tar.gz: 7ea061fa8ca06101f34dae365fbed8f0a6ef292c99eb8abc99b21d9b0358ed282af2fba92061598037090d425efc07c1a63cbc6909b3524c3218ba3d5128d0b8
|
data/.github/workflows/prs.yml
CHANGED
@@ -22,9 +22,12 @@ jobs:
|
|
22
22
|
- '2.5'
|
23
23
|
- '2.7'
|
24
24
|
- '3.0'
|
25
|
+
- '3.1'
|
25
26
|
activerecord:
|
26
27
|
- '5.2'
|
27
28
|
- '6.0'
|
29
|
+
- '6.1'
|
30
|
+
- '7.0'
|
28
31
|
db:
|
29
32
|
- mysql2
|
30
33
|
- sqlite3
|
@@ -34,6 +37,10 @@ jobs:
|
|
34
37
|
exclude:
|
35
38
|
- ruby: '3.0'
|
36
39
|
activerecord: '5.2'
|
40
|
+
- ruby: '3.1'
|
41
|
+
activerecord: '5.2'
|
42
|
+
- ruby: '2.5'
|
43
|
+
activerecord: '7.0'
|
37
44
|
- db: skip
|
38
45
|
dbversion: skip
|
39
46
|
include:
|
@@ -45,6 +52,10 @@ jobs:
|
|
45
52
|
activerecord: '6.0'
|
46
53
|
db: postgresql
|
47
54
|
dbversion: '9.6'
|
55
|
+
- ruby: '2.5'
|
56
|
+
activerecord: '6.1'
|
57
|
+
db: postgresql
|
58
|
+
dbversion: '9.6'
|
48
59
|
- ruby: '2.7'
|
49
60
|
activerecord: '5.2'
|
50
61
|
db: postgresql
|
@@ -53,10 +64,38 @@ jobs:
|
|
53
64
|
activerecord: '6.0'
|
54
65
|
db: postgresql
|
55
66
|
dbversion: '9.6'
|
67
|
+
- ruby: '2.7'
|
68
|
+
activerecord: '6.1'
|
69
|
+
db: postgresql
|
70
|
+
dbversion: '9.6'
|
71
|
+
- ruby: '2.7'
|
72
|
+
activerecord: '7.0'
|
73
|
+
db: postgresql
|
74
|
+
dbversion: '9.6'
|
75
|
+
- ruby: '3.0'
|
76
|
+
activerecord: '6.0'
|
77
|
+
db: postgresql
|
78
|
+
dbversion: '9.6'
|
79
|
+
- ruby: '3.0'
|
80
|
+
activerecord: '6.1'
|
81
|
+
db: postgresql
|
82
|
+
dbversion: '9.6'
|
56
83
|
- ruby: '3.0'
|
84
|
+
activerecord: '7.0'
|
85
|
+
db: postgresql
|
86
|
+
dbversion: '9.6'
|
87
|
+
- ruby: '3.1'
|
57
88
|
activerecord: '6.0'
|
58
89
|
db: postgresql
|
59
90
|
dbversion: '9.6'
|
91
|
+
- ruby: '3.1'
|
92
|
+
activerecord: '6.1'
|
93
|
+
db: postgresql
|
94
|
+
dbversion: '9.6'
|
95
|
+
- ruby: '3.1'
|
96
|
+
activerecord: '7.0'
|
97
|
+
db: postgresql
|
98
|
+
dbversion: '9.6'
|
60
99
|
env:
|
61
100
|
BUNDLE_GEMFILE: "${{ github.workspace }}/gemfiles/activerecord-${{ matrix.activerecord }}/Gemfile.${{ matrix.db }}"
|
62
101
|
MYSQL_DB_HOST: 127.0.0.1
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -420,9 +420,17 @@ SchemaAssociations is tested on all combinations of:
|
|
420
420
|
<!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
|
421
421
|
* ruby **2.5** with activerecord **5.2**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
422
422
|
* ruby **2.5** with activerecord **6.0**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
423
|
+
* ruby **2.5** with activerecord **6.1**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
423
424
|
* ruby **2.7** with activerecord **5.2**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
424
425
|
* ruby **2.7** with activerecord **6.0**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
426
|
+
* ruby **2.7** with activerecord **6.1**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
427
|
+
* ruby **2.7** with activerecord **7.0**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
425
428
|
* ruby **3.0** with activerecord **6.0**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
429
|
+
* ruby **3.0** with activerecord **6.1**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
430
|
+
* ruby **3.0** with activerecord **7.0**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
431
|
+
* ruby **3.1** with activerecord **6.0**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
432
|
+
* ruby **3.1** with activerecord **6.1**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
433
|
+
* ruby **3.1** with activerecord **7.0**, using **mysql2**, **postgresql:9.6** or **sqlite3**
|
426
434
|
|
427
435
|
<!-- SCHEMA_DEV: MATRIX - end -->
|
428
436
|
|
@@ -445,10 +453,18 @@ or in a Gemfile
|
|
445
453
|
|
446
454
|
## Release notes:
|
447
455
|
|
456
|
+
### 1.4.0
|
457
|
+
|
458
|
+
* Add AR 6.1 and 7.0
|
459
|
+
* Add Ruby 3.1
|
460
|
+
* drop schema_plus_compatibiltiy dependency (indirect through schema_plus_foreign_keys update)
|
461
|
+
|
448
462
|
### 1.3.0
|
449
463
|
|
450
|
-
*
|
451
|
-
*
|
464
|
+
* add AR 6.0
|
465
|
+
* add Ruby 3.0
|
466
|
+
* drop AR < 5.2
|
467
|
+
* drop Ruby < 2.5
|
452
468
|
|
453
469
|
### 1.2.7
|
454
470
|
|
data/Rakefile
CHANGED
data/init.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'ostruct'
|
2
4
|
|
3
5
|
module SchemaAssociations
|
@@ -272,7 +274,7 @@ module SchemaAssociations
|
|
272
274
|
end
|
273
275
|
|
274
276
|
def _method_exists?(name)
|
275
|
-
method_defined?(name) || private_method_defined?(name)
|
277
|
+
method_defined?(name) || private_method_defined?(name)
|
276
278
|
end
|
277
279
|
|
278
280
|
end
|
data/lib/schema_associations.rb
CHANGED
data/schema_associations.gemspec
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
$:.push File.expand_path("../lib", __FILE__)
|
3
4
|
require "schema_associations/version"
|
4
5
|
|
@@ -19,12 +20,12 @@ Gem::Specification.new do |gem|
|
|
19
20
|
|
20
21
|
gem.required_ruby_version = '>= 2.5'
|
21
22
|
|
22
|
-
gem.add_dependency 'activerecord', '>= 5.2', '<
|
23
|
-
gem.add_dependency 'schema_plus_foreign_keys', '~> 1.
|
23
|
+
gem.add_dependency 'activerecord', '>= 5.2', '< 7.1'
|
24
|
+
gem.add_dependency 'schema_plus_foreign_keys', '~> 1.1.0'
|
24
25
|
gem.add_dependency 'valuable'
|
25
26
|
|
26
27
|
gem.add_development_dependency 'bundler'
|
27
28
|
gem.add_development_dependency 'rake', '~> 13.0'
|
28
29
|
gem.add_development_dependency 'rspec', '~> 3.0'
|
29
|
-
gem.add_development_dependency 'schema_dev', '~> 4.
|
30
|
+
gem.add_development_dependency 'schema_dev', '~> 4.2.0'
|
30
31
|
end
|
data/schema_dev.yml
CHANGED
data/spec/association_spec.rb
CHANGED
@@ -1,20 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
4
|
|
4
5
|
describe ActiveRecord::Base do
|
6
|
+
def stub_model(name, base = ActiveRecord::Base, &block)
|
7
|
+
klass = Class.new(base)
|
5
8
|
|
6
|
-
|
7
|
-
|
9
|
+
if block_given?
|
10
|
+
klass.instance_eval(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
stub_const(name, klass)
|
8
14
|
end
|
9
15
|
|
10
16
|
context "in basic case" do
|
11
17
|
before(:each) do
|
12
18
|
create_tables(
|
13
19
|
"posts", {}, {},
|
14
|
-
"comments", {}, { :
|
20
|
+
"comments", {}, { post_id: {foreign_key: true} }
|
15
21
|
)
|
16
|
-
|
17
|
-
|
22
|
+
stub_model('Post')
|
23
|
+
stub_model('Comment')
|
18
24
|
end
|
19
25
|
|
20
26
|
it "should create belongs_to association when reflecting on it" do
|
@@ -43,13 +49,13 @@ describe ActiveRecord::Base do
|
|
43
49
|
|
44
50
|
it "should create association when accessing it" do
|
45
51
|
post = Post.create
|
46
|
-
comment = Comment.create(:
|
52
|
+
comment = Comment.create(post_id: post.id)
|
47
53
|
expect(comment.post.id).to eq(post.id)
|
48
54
|
end
|
49
55
|
|
50
56
|
it "should create association when creating record" do
|
51
57
|
post = Post.create
|
52
|
-
comment = Comment.create(:
|
58
|
+
comment = Comment.create(post: post)
|
53
59
|
expect(comment.reload.post.id).to eq(post.id)
|
54
60
|
end
|
55
61
|
|
@@ -72,11 +78,11 @@ describe ActiveRecord::Base do
|
|
72
78
|
"owners", {}, {},
|
73
79
|
"colors", {}, {},
|
74
80
|
"widgets", {}, {
|
75
|
-
:
|
81
|
+
owner_id: { foreign_key: true },
|
76
82
|
},
|
77
|
-
"parts", {}, { :
|
78
|
-
"manifests", {}, { :
|
79
|
-
"colors_widgets", {:
|
83
|
+
"parts", {}, { widget_id: { foreign_key: true } },
|
84
|
+
"manifests", {}, { widget_id: { foreign_key: true, index: {unique: true}} },
|
85
|
+
"colors_widgets", {id: false}, { widget_id: { foreign_key: true}, color_id: { foreign_key: true} }
|
80
86
|
)
|
81
87
|
end
|
82
88
|
|
@@ -91,78 +97,78 @@ describe ActiveRecord::Base do
|
|
91
97
|
end
|
92
98
|
|
93
99
|
it "should default as expected" do
|
94
|
-
|
95
|
-
check_reflections(:
|
100
|
+
stub_model('Widget')
|
101
|
+
check_reflections(owner: true, colors: true, parts: true, manifest: true)
|
96
102
|
end
|
97
103
|
|
98
104
|
it "should respect :only" do
|
99
|
-
|
100
|
-
schema_associations :
|
105
|
+
stub_model('Widget') do
|
106
|
+
schema_associations only: :owner
|
101
107
|
end
|
102
|
-
check_reflections(:
|
108
|
+
check_reflections(owner: true, colors: false, parts: false, manifest: false)
|
103
109
|
end
|
104
110
|
|
105
111
|
it "should respect :except" do
|
106
|
-
|
107
|
-
schema_associations :
|
112
|
+
stub_model('Widget') do
|
113
|
+
schema_associations except: :owner
|
108
114
|
end
|
109
|
-
check_reflections(:
|
115
|
+
check_reflections(owner: false, colors: true, parts: true, manifest: true)
|
110
116
|
end
|
111
117
|
|
112
118
|
it "should respect :only_type :belongs_to" do
|
113
|
-
|
114
|
-
schema_associations :
|
119
|
+
stub_model('Widget') do
|
120
|
+
schema_associations only_type: :belongs_to
|
115
121
|
end
|
116
|
-
check_reflections(:
|
122
|
+
check_reflections(owner: true, colors: false, parts: false, manifest: false)
|
117
123
|
end
|
118
124
|
|
119
125
|
it "should respect :except_type :belongs_to" do
|
120
|
-
|
121
|
-
schema_associations :
|
126
|
+
stub_model('Widget') do
|
127
|
+
schema_associations except_type: :belongs_to
|
122
128
|
end
|
123
|
-
check_reflections(:
|
129
|
+
check_reflections(owner: false, colors: true, parts: true, manifest: true)
|
124
130
|
end
|
125
131
|
|
126
132
|
it "should respect :only_type :has_many" do
|
127
|
-
|
128
|
-
schema_associations :
|
133
|
+
stub_model('Widget') do
|
134
|
+
schema_associations only_type: :has_many
|
129
135
|
end
|
130
|
-
check_reflections(:
|
136
|
+
check_reflections(owner: false, colors: false, parts: true, manifest: false)
|
131
137
|
end
|
132
138
|
|
133
139
|
it "should respect :except_type :has_many" do
|
134
|
-
|
135
|
-
schema_associations :
|
140
|
+
stub_model('Widget') do
|
141
|
+
schema_associations except_type: :has_many
|
136
142
|
end
|
137
|
-
check_reflections(:
|
143
|
+
check_reflections(owner: true, colors: true, parts: false, manifest: true)
|
138
144
|
end
|
139
145
|
|
140
146
|
it "should respect :only_type :has_one" do
|
141
|
-
|
142
|
-
schema_associations :
|
147
|
+
stub_model('Widget') do
|
148
|
+
schema_associations only_type: :has_one
|
143
149
|
end
|
144
|
-
check_reflections(:
|
150
|
+
check_reflections(owner: false, colors: false, parts: false, manifest: true)
|
145
151
|
end
|
146
152
|
|
147
153
|
it "should respect :except_type :has_one" do
|
148
|
-
|
149
|
-
schema_associations :
|
154
|
+
stub_model('Widget') do
|
155
|
+
schema_associations except_type: :has_one
|
150
156
|
end
|
151
|
-
check_reflections(:
|
157
|
+
check_reflections(owner: true, colors: true, parts: true, manifest: false)
|
152
158
|
end
|
153
159
|
|
154
160
|
it "should respect :only_type :has_and_belongs_to_many" do
|
155
|
-
|
156
|
-
schema_associations :
|
161
|
+
stub_model('Widget') do
|
162
|
+
schema_associations only_type: :has_and_belongs_to_many
|
157
163
|
end
|
158
|
-
check_reflections(:
|
164
|
+
check_reflections(owner: false, colors: true, parts: false, manifest: false)
|
159
165
|
end
|
160
166
|
|
161
167
|
it "should respect :except_type :has_and_belongs_to_many" do
|
162
|
-
|
163
|
-
schema_associations :
|
168
|
+
stub_model('Widget') do
|
169
|
+
schema_associations except_type: :has_and_belongs_to_many
|
164
170
|
end
|
165
|
-
check_reflections(:
|
171
|
+
check_reflections(owner: true, colors: false, parts: true, manifest: true)
|
166
172
|
end
|
167
173
|
|
168
174
|
end
|
@@ -172,12 +178,12 @@ describe ActiveRecord::Base do
|
|
172
178
|
with_associations_auto_create(true) do
|
173
179
|
create_tables(
|
174
180
|
"posts", {}, {},
|
175
|
-
"comments", {}, { :
|
181
|
+
"comments", {}, { post_id: {foreign_key: true} }
|
176
182
|
)
|
177
|
-
|
178
|
-
schema_associations :
|
183
|
+
stub_model('Post') do
|
184
|
+
schema_associations auto_create: false
|
179
185
|
end
|
180
|
-
|
186
|
+
stub_model('Comment')
|
181
187
|
expect(Post.reflect_on_association(:comments)).to be_nil
|
182
188
|
expect(Comment.reflect_on_association(:post)).not_to be_nil
|
183
189
|
end
|
@@ -188,12 +194,12 @@ describe ActiveRecord::Base do
|
|
188
194
|
with_associations_auto_create(false) do
|
189
195
|
create_tables(
|
190
196
|
"posts", {}, {},
|
191
|
-
"comments", {}, { :
|
197
|
+
"comments", {}, { post_id: {foreign_key: true} }
|
192
198
|
)
|
193
|
-
|
194
|
-
schema_associations :
|
199
|
+
stub_model('Post') do
|
200
|
+
schema_associations auto_create: true
|
195
201
|
end
|
196
|
-
|
202
|
+
stub_model('Comment')
|
197
203
|
expect(Post.reflect_on_association(:comments)).not_to be_nil
|
198
204
|
expect(Comment.reflect_on_association(:post)).to be_nil
|
199
205
|
end
|
@@ -203,12 +209,12 @@ describe ActiveRecord::Base do
|
|
203
209
|
with_associations_auto_create(false) do
|
204
210
|
create_tables(
|
205
211
|
"posts", {}, {},
|
206
|
-
"comments", {}, { :
|
212
|
+
"comments", {}, { post_id: {foreign_key: true} }
|
207
213
|
)
|
208
|
-
|
214
|
+
stub_model('Post') do
|
209
215
|
schema_associations
|
210
216
|
end
|
211
|
-
|
217
|
+
stub_model('Comment')
|
212
218
|
expect(Post.reflect_on_association(:comments)).not_to be_nil
|
213
219
|
expect(Comment.reflect_on_association(:post)).to be_nil
|
214
220
|
end
|
@@ -220,10 +226,10 @@ describe ActiveRecord::Base do
|
|
220
226
|
before(:each) do
|
221
227
|
create_tables(
|
222
228
|
"posts", {}, {},
|
223
|
-
"comments", {}, { :
|
229
|
+
"comments", {}, { post_id: {foreign_key: true, index: { unique: true} } }
|
224
230
|
)
|
225
|
-
|
226
|
-
|
231
|
+
stub_model('Post')
|
232
|
+
stub_model('Comment')
|
227
233
|
end
|
228
234
|
it "should create has_one association" do
|
229
235
|
reflection = Post.reflect_on_association(:comment)
|
@@ -247,10 +253,10 @@ describe ActiveRecord::Base do
|
|
247
253
|
before(:each) do
|
248
254
|
create_tables(
|
249
255
|
"posts", {}, {},
|
250
|
-
"comments", {}, { :
|
256
|
+
"comments", {}, { subject_post_id: { foreign_key: { references: "posts" }} }
|
251
257
|
)
|
252
|
-
|
253
|
-
|
258
|
+
stub_model('Post')
|
259
|
+
stub_model('Comment')
|
254
260
|
end
|
255
261
|
it "should name belongs_to according to column" do
|
256
262
|
reflection = Comment.reflect_on_association(:subject_post)
|
@@ -275,10 +281,10 @@ describe ActiveRecord::Base do
|
|
275
281
|
before(:each) do
|
276
282
|
create_tables(
|
277
283
|
"posts", {}, {},
|
278
|
-
"comments", {}, { :
|
284
|
+
"comments", {}, { post_cited: { foreign_key: {references: "posts" }} }
|
279
285
|
)
|
280
|
-
|
281
|
-
|
286
|
+
stub_model('Post')
|
287
|
+
stub_model('Comment')
|
282
288
|
end
|
283
289
|
it "should name belongs_to according to column" do
|
284
290
|
reflection = Comment.reflect_on_association(:post_cited)
|
@@ -303,10 +309,10 @@ describe ActiveRecord::Base do
|
|
303
309
|
before(:each) do
|
304
310
|
create_tables(
|
305
311
|
"posts", {}, {},
|
306
|
-
"comments", {}, { :
|
312
|
+
"comments", {}, { subject: {foreign_key: { references: "posts" }} }
|
307
313
|
)
|
308
|
-
|
309
|
-
|
314
|
+
stub_model('Post')
|
315
|
+
stub_model('Comment')
|
310
316
|
end
|
311
317
|
it "should name belongs_to according to column" do
|
312
318
|
reflection = Comment.reflect_on_association(:subject)
|
@@ -328,13 +334,18 @@ describe ActiveRecord::Base do
|
|
328
334
|
end
|
329
335
|
|
330
336
|
it "maps table prefix" do
|
331
|
-
with_associations_config(:
|
337
|
+
with_associations_config(table_prefix_map: { "wooga_" => "Happy"} ) do
|
332
338
|
create_tables(
|
333
339
|
"wooga_posts", {}, {},
|
334
|
-
"wooga_comments", {}, { :
|
340
|
+
"wooga_comments", {}, { wooga_post_id: { foreign_key: true} }
|
335
341
|
)
|
336
|
-
|
337
|
-
|
342
|
+
stub_model('HappyPost') do
|
343
|
+
self.table_name = 'wooga_posts'
|
344
|
+
end
|
345
|
+
stub_model('HappyComment') do
|
346
|
+
self.table_name = 'wooga_comments'
|
347
|
+
end
|
348
|
+
|
338
349
|
# Kernel.warn HappyPost.reflect_on_all_associations.inspect
|
339
350
|
expect(HappyComment.reflect_on_association(:post).class_name).to eq("HappyPost")
|
340
351
|
expect(HappyPost.reflect_on_association(:comments).class_name).to eq("HappyComment")
|
@@ -345,10 +356,10 @@ describe ActiveRecord::Base do
|
|
345
356
|
before(:each) do
|
346
357
|
create_tables(
|
347
358
|
"posts", {}, {},
|
348
|
-
"comments", {}, { :
|
359
|
+
"comments", {}, { post_id: { foreign_key: true} }
|
349
360
|
)
|
350
|
-
|
351
|
-
|
361
|
+
stub_model('Post')
|
362
|
+
stub_model('Comment')
|
352
363
|
end
|
353
364
|
it "should create unordered has_many association" do
|
354
365
|
reflection = Post.reflect_on_association(:comments)
|
@@ -365,10 +376,10 @@ describe ActiveRecord::Base do
|
|
365
376
|
before(:each) do
|
366
377
|
create_tables(
|
367
378
|
"posts", {}, {},
|
368
|
-
"comments", {}, { :
|
379
|
+
"comments", {}, { post_id: {foreign_key: true}, position: {} }
|
369
380
|
)
|
370
|
-
|
371
|
-
|
381
|
+
stub_model('Post')
|
382
|
+
stub_model('Comment')
|
372
383
|
end
|
373
384
|
it "should create ordered has_many association" do
|
374
385
|
reflection = Post.reflect_on_association(:comments)
|
@@ -388,10 +399,10 @@ describe ActiveRecord::Base do
|
|
388
399
|
before(:each) do
|
389
400
|
create_tables(
|
390
401
|
"posts", {}, {},
|
391
|
-
"comments", {}, { :
|
402
|
+
"comments", {}, { post_id: {}, position: {} }
|
392
403
|
)
|
393
|
-
|
394
|
-
|
404
|
+
stub_model('Post')
|
405
|
+
stub_model('Comment') do
|
395
406
|
scope :simple_scope, lambda { order(:id) }
|
396
407
|
end
|
397
408
|
end
|
@@ -405,10 +416,10 @@ describe ActiveRecord::Base do
|
|
405
416
|
before(:each) do
|
406
417
|
create_tables(
|
407
418
|
"posts", {}, {},
|
408
|
-
"comments", {}, { :
|
419
|
+
"comments", {}, { post_id: {}, position: {} }
|
409
420
|
)
|
410
|
-
|
411
|
-
|
421
|
+
stub_model('Post')
|
422
|
+
stub_model('Comment') do
|
412
423
|
scope :simple_scope, lambda { order(:id).includes(:post) }
|
413
424
|
end
|
414
425
|
end
|
@@ -424,21 +435,21 @@ describe ActiveRecord::Base do
|
|
424
435
|
|
425
436
|
before(:each) do
|
426
437
|
create_tables(
|
427
|
-
"nodes", {}, { :
|
438
|
+
"nodes", {}, { parent_id: { foreign_key: true} }
|
428
439
|
)
|
429
440
|
end
|
430
441
|
|
431
442
|
it "should use children as the inverse of parent" do
|
432
|
-
|
443
|
+
stub_model('Node')
|
433
444
|
reflection = Node.reflect_on_association(:children)
|
434
445
|
expect(reflection).not_to be_nil
|
435
446
|
end
|
436
447
|
|
437
448
|
it "should use child as the singular inverse of parent" do
|
438
449
|
migration.suppress_messages do
|
439
|
-
migration.add_index(:nodes, :parent_id, :
|
450
|
+
migration.add_index(:nodes, :parent_id, unique: true)
|
440
451
|
end
|
441
|
-
|
452
|
+
stub_model('Node')
|
442
453
|
reflection = Node.reflect_on_association(:child)
|
443
454
|
expect(reflection).not_to be_nil
|
444
455
|
end
|
@@ -450,32 +461,32 @@ describe ActiveRecord::Base do
|
|
450
461
|
def prefix_one
|
451
462
|
create_tables(
|
452
463
|
"posts", {}, {},
|
453
|
-
"post_comments", {}, { :
|
464
|
+
"post_comments", {}, { post_id: { foreign_key: true} }
|
454
465
|
)
|
455
|
-
|
456
|
-
|
466
|
+
stub_model('Post')
|
467
|
+
stub_model('PostComment')
|
457
468
|
end
|
458
469
|
|
459
470
|
def suffix_one
|
460
471
|
create_tables(
|
461
472
|
"posts", {}, {},
|
462
|
-
"comment_posts", {}, { :
|
473
|
+
"comment_posts", {}, { post_id: { foreign_key: true} }
|
463
474
|
)
|
464
|
-
|
465
|
-
|
475
|
+
stub_model('Post')
|
476
|
+
stub_model('PostComment')
|
466
477
|
end
|
467
478
|
|
468
479
|
def prefix_both
|
469
480
|
create_tables(
|
470
481
|
"blog_page_posts", {}, {},
|
471
|
-
"blog_page_comments", {}, { :
|
482
|
+
"blog_page_comments", {}, { blog_page_post_id: { foreign_key: true} }
|
472
483
|
)
|
473
|
-
|
474
|
-
|
484
|
+
stub_model('BlogPagePost')
|
485
|
+
stub_model('BlogPageComment')
|
475
486
|
end
|
476
487
|
|
477
488
|
it "should use concise association name for one prefix" do
|
478
|
-
with_associations_config(:
|
489
|
+
with_associations_config(auto_create: true, concise_names: true) do
|
479
490
|
prefix_one
|
480
491
|
reflection = Post.reflect_on_association(:comments)
|
481
492
|
expect(reflection).not_to be_nil
|
@@ -487,7 +498,7 @@ describe ActiveRecord::Base do
|
|
487
498
|
end
|
488
499
|
|
489
500
|
it "should use concise association name for one suffix" do
|
490
|
-
with_associations_config(:
|
501
|
+
with_associations_config(auto_create: true, concise_names: true) do
|
491
502
|
suffix_one
|
492
503
|
reflection = Post.reflect_on_association(:comments)
|
493
504
|
expect(reflection).not_to be_nil
|
@@ -499,7 +510,7 @@ describe ActiveRecord::Base do
|
|
499
510
|
end
|
500
511
|
|
501
512
|
it "should use concise association name for shared prefixes" do
|
502
|
-
with_associations_config(:
|
513
|
+
with_associations_config(auto_create: true, concise_names: true) do
|
503
514
|
prefix_both
|
504
515
|
reflection = BlogPagePost.reflect_on_association(:comments)
|
505
516
|
expect(reflection).not_to be_nil
|
@@ -511,7 +522,7 @@ describe ActiveRecord::Base do
|
|
511
522
|
end
|
512
523
|
|
513
524
|
it "should use full names and not concise names when so configured" do
|
514
|
-
with_associations_config(:
|
525
|
+
with_associations_config(auto_create: true, concise_names: false) do
|
515
526
|
prefix_one
|
516
527
|
reflection = Post.reflect_on_association(:post_comments)
|
517
528
|
expect(reflection).not_to be_nil
|
@@ -525,7 +536,7 @@ describe ActiveRecord::Base do
|
|
525
536
|
end
|
526
537
|
|
527
538
|
it "should use concise names and not full names when so configured" do
|
528
|
-
with_associations_config(:
|
539
|
+
with_associations_config(auto_create: true, concise_names: true) do
|
529
540
|
prefix_one
|
530
541
|
reflection = Post.reflect_on_association(:comments)
|
531
542
|
expect(reflection).not_to be_nil
|
@@ -546,10 +557,10 @@ describe ActiveRecord::Base do
|
|
546
557
|
create_tables(
|
547
558
|
"posts", {}, {},
|
548
559
|
"tags", {}, {},
|
549
|
-
"posts_tags", {:
|
560
|
+
"posts_tags", {id: false}, { post_id: { foreign_key: true}, tag_id: { foreign_key: true}}
|
550
561
|
)
|
551
|
-
|
552
|
-
|
562
|
+
stub_model('Post')
|
563
|
+
stub_model('Tag')
|
553
564
|
end
|
554
565
|
it "should create has_and_belongs_to_many association" do
|
555
566
|
reflection = Post.reflect_on_association(:tags)
|
@@ -564,13 +575,13 @@ describe ActiveRecord::Base do
|
|
564
575
|
before(:each) do
|
565
576
|
create_tables(
|
566
577
|
"users", {}, {},
|
567
|
-
"posts", {}, { :
|
568
|
-
"comments", {}, { :
|
578
|
+
"posts", {}, { user_id: { foreign_key: true}},
|
579
|
+
"comments", {}, { post_id: { foreign_key: true}},
|
569
580
|
)
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
has_many :comments, :
|
581
|
+
stub_model('Post')
|
582
|
+
stub_model('Comment')
|
583
|
+
stub_model('User') do
|
584
|
+
has_many :comments, through: :posts
|
574
585
|
end
|
575
586
|
end
|
576
587
|
|
@@ -591,37 +602,37 @@ describe ActiveRecord::Base do
|
|
591
602
|
before(:each) do
|
592
603
|
create_tables(
|
593
604
|
"types", {}, {},
|
594
|
-
"posts", {}, {:
|
605
|
+
"posts", {}, {type_id: { foreign_key: true}}
|
595
606
|
)
|
596
607
|
end
|
597
608
|
it "should define association normally if no existing method is defined" do
|
598
|
-
|
609
|
+
stub_model('Type')
|
599
610
|
expect(Type.reflect_on_association(:posts)).not_to be_nil # sanity check for this context
|
600
611
|
end
|
601
612
|
it "should not define association over existing public method" do
|
602
|
-
|
603
|
-
|
613
|
+
stub_model('Type') do
|
614
|
+
define_method(:posts) do
|
604
615
|
:existing
|
605
616
|
end
|
606
617
|
end
|
607
618
|
expect(Type.reflect_on_association(:posts)).to be_nil
|
608
619
|
end
|
609
620
|
it "should not define association over existing private method" do
|
610
|
-
|
611
|
-
|
612
|
-
def posts
|
621
|
+
stub_model('Type') do
|
622
|
+
define_method(:posts) do
|
613
623
|
:existing
|
614
624
|
end
|
625
|
+
private :posts
|
615
626
|
end
|
616
627
|
expect(Type.reflect_on_association(:posts)).to be_nil
|
617
628
|
end
|
618
629
|
it "should define association :type over (deprecated) kernel method" do
|
619
|
-
|
630
|
+
stub_model('Post')
|
620
631
|
expect(Post.reflect_on_association(:type)).not_to be_nil
|
621
632
|
end
|
622
633
|
it "should not define association :type over model method" do
|
623
|
-
|
624
|
-
|
634
|
+
stub_model('Post') do
|
635
|
+
define_method(:type) do
|
625
636
|
:existing
|
626
637
|
end
|
627
638
|
end
|
@@ -633,16 +644,16 @@ describe ActiveRecord::Base do
|
|
633
644
|
before(:each) do
|
634
645
|
create_tables(
|
635
646
|
"posts", {}, {},
|
636
|
-
"comments", {}, { :
|
647
|
+
"comments", {}, { post_id: { foreign_key: true}, type: {coltype: :string} },
|
637
648
|
"citers", {}, {},
|
638
|
-
"citations", {}, { :
|
649
|
+
"citations", {}, { comment_id: { foreign_key: true}, citer_id: { foreign_key: true}}
|
639
650
|
)
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
has_one :citer, :
|
651
|
+
stub_model('Post')
|
652
|
+
stub_model('Comment')
|
653
|
+
stub_model('Citation')
|
654
|
+
stub_model('SubComment', Comment)
|
655
|
+
stub_model('OwnComment', Comment) do
|
656
|
+
has_one :citer, through: :citations
|
646
657
|
end
|
647
658
|
end
|
648
659
|
|
@@ -661,8 +672,10 @@ describe ActiveRecord::Base do
|
|
661
672
|
create_tables(
|
662
673
|
"posts", {}, {}
|
663
674
|
)
|
664
|
-
|
665
|
-
|
675
|
+
stub_model('PostBase') do
|
676
|
+
self.abstract_class = true
|
677
|
+
end
|
678
|
+
stub_model('Post', PostBase)
|
666
679
|
end
|
667
680
|
|
668
681
|
it "should skip abstract classes" do
|
@@ -683,10 +696,10 @@ describe ActiveRecord::Base do
|
|
683
696
|
before(:each) do
|
684
697
|
create_tables(
|
685
698
|
"posts", {}, {},
|
686
|
-
"comments", {}, { :
|
699
|
+
"comments", {}, { post_id: { foreign_key: true} }
|
687
700
|
)
|
688
|
-
|
689
|
-
|
701
|
+
stub_model('Post')
|
702
|
+
stub_model('Comment')
|
690
703
|
end
|
691
704
|
|
692
705
|
it "should define associations before needed by relation" do
|
@@ -698,7 +711,7 @@ describe ActiveRecord::Base do
|
|
698
711
|
protected
|
699
712
|
|
700
713
|
def with_associations_auto_create(value, &block)
|
701
|
-
with_associations_config(:
|
714
|
+
with_associations_config(auto_create: value, &block)
|
702
715
|
end
|
703
716
|
|
704
717
|
def with_associations_config(opts, &block)
|
@@ -715,7 +728,7 @@ describe ActiveRecord::Base do
|
|
715
728
|
|
716
729
|
def create_tables(*table_defs)
|
717
730
|
ActiveRecord::Migration.suppress_messages do
|
718
|
-
ActiveRecord::Base.connection.
|
731
|
+
ActiveRecord::Base.connection.tables.sort.each do |table|
|
719
732
|
ActiveRecord::Migration.drop_table table, force: :cascade
|
720
733
|
end
|
721
734
|
table_defs.each_slice(3) do |table_name, opts, columns_with_options|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'simplecov'
|
2
|
-
SimpleCov.start
|
4
|
+
SimpleCov.start unless SimpleCov.running
|
3
5
|
|
4
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
7
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
@@ -12,16 +14,6 @@ require 'schema_dev/rspec'
|
|
12
14
|
|
13
15
|
SchemaDev::Rspec::setup
|
14
16
|
|
15
|
-
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
|
16
|
-
|
17
|
-
def remove_all_models
|
18
|
-
ObjectSpace.each_object(Class) do |c|
|
19
|
-
next unless c.ancestors.include? ActiveRecord::Base
|
20
|
-
next if c == ActiveRecord::Base
|
21
|
-
next if c.name.blank?
|
22
|
-
c.destroy_all rescue nil
|
23
|
-
ActiveSupport::Dependencies.remove_constant c.name
|
24
|
-
end
|
25
|
-
end
|
17
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
26
18
|
|
27
19
|
SimpleCov.command_name "[Ruby #{RUBY_VERSION} - ActiveRecord #{::ActiveRecord::VERSION::STRING}]"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ronen Barzel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2022-
|
12
|
+
date: 2022-06-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
version: '5.2'
|
21
21
|
- - "<"
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: '
|
23
|
+
version: '7.1'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -30,21 +30,21 @@ dependencies:
|
|
30
30
|
version: '5.2'
|
31
31
|
- - "<"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '7.1'
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: schema_plus_foreign_keys
|
36
36
|
requirement: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 1.
|
40
|
+
version: 1.1.0
|
41
41
|
type: :runtime
|
42
42
|
prerelease: false
|
43
43
|
version_requirements: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 1.
|
47
|
+
version: 1.1.0
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: valuable
|
50
50
|
requirement: !ruby/object:Gem::Requirement
|
@@ -107,14 +107,14 @@ dependencies:
|
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
110
|
+
version: 4.2.0
|
111
111
|
type: :development
|
112
112
|
prerelease: false
|
113
113
|
version_requirements: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
115
|
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 4.2.0
|
118
118
|
description: SchemaAssociations extends ActiveRecord to automatically create associations
|
119
119
|
by inspecting the database schema. This is more more DRY than the standard behavior,
|
120
120
|
for which in addition to specifying the foreign key in the migration, you must also
|
@@ -143,6 +143,14 @@ files:
|
|
143
143
|
- gemfiles/activerecord-6.0/Gemfile.mysql2
|
144
144
|
- gemfiles/activerecord-6.0/Gemfile.postgresql
|
145
145
|
- gemfiles/activerecord-6.0/Gemfile.sqlite3
|
146
|
+
- gemfiles/activerecord-6.1/Gemfile.base
|
147
|
+
- gemfiles/activerecord-6.1/Gemfile.mysql2
|
148
|
+
- gemfiles/activerecord-6.1/Gemfile.postgresql
|
149
|
+
- gemfiles/activerecord-6.1/Gemfile.sqlite3
|
150
|
+
- gemfiles/activerecord-7.0/Gemfile.base
|
151
|
+
- gemfiles/activerecord-7.0/Gemfile.mysql2
|
152
|
+
- gemfiles/activerecord-7.0/Gemfile.postgresql
|
153
|
+
- gemfiles/activerecord-7.0/Gemfile.sqlite3
|
146
154
|
- init.rb
|
147
155
|
- lib/schema_associations.rb
|
148
156
|
- lib/schema_associations/active_record/associations.rb
|
@@ -150,7 +158,6 @@ files:
|
|
150
158
|
- schema_associations.gemspec
|
151
159
|
- schema_dev.yml
|
152
160
|
- spec/association_spec.rb
|
153
|
-
- spec/schema_associations.sqlite3
|
154
161
|
- spec/spec_helper.rb
|
155
162
|
homepage: https://github.com/SchemaPlus/schema_associations
|
156
163
|
licenses: []
|
@@ -170,12 +177,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
170
177
|
- !ruby/object:Gem::Version
|
171
178
|
version: '0'
|
172
179
|
requirements: []
|
173
|
-
rubygems_version: 3.
|
180
|
+
rubygems_version: 3.1.6
|
174
181
|
signing_key:
|
175
182
|
specification_version: 4
|
176
183
|
summary: ActiveRecord extension that automatically (DRY) creates associations based
|
177
184
|
on the schema
|
178
185
|
test_files:
|
179
186
|
- spec/association_spec.rb
|
180
|
-
- spec/schema_associations.sqlite3
|
181
187
|
- spec/spec_helper.rb
|
Binary file
|