active_record-json_associations 0.13.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d0210b855faca7f7e383b01be2e5e7fb6f78a24770fc5456f6fe6661e3862d2
|
4
|
+
data.tar.gz: 95abf57fc202bb226e8c3dd01c018c489c21ed47c3e00fbd1fc920b0df8f7856
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d396317afb6cb24bccf06d8f7d9a773f8b76ca65e117d9194230c9378804eeb26f9eb608b26e7240fa0de45e5292f03b6170e544473bfb77621d695a7a0cc887
|
7
|
+
data.tar.gz: 72cc81817ebb6f46f4d82a446f1a17235d60ca701f43300aa23425d2bbd298d5119587ad407eaaf441c74dca0a68aa0d58483b42ef315e064eacb4fe869f8784
|
@@ -79,7 +79,7 @@ module ActiveRecord
|
|
79
79
|
end
|
80
80
|
|
81
81
|
define_method one_ids_equals do |ids|
|
82
|
-
super Array(ids).select(&:present?).map(&:to_i)
|
82
|
+
super Array(ids).select(&:present?).map(&:to_i).uniq
|
83
83
|
end
|
84
84
|
|
85
85
|
define_method many do
|
@@ -137,7 +137,7 @@ module ActiveRecord
|
|
137
137
|
end
|
138
138
|
|
139
139
|
define_method one_ids_equals do |ids|
|
140
|
-
normalized_ids = Array(ids).select(&:present?).map(&:to_i)
|
140
|
+
normalized_ids = Array(ids).select(&:present?).map(&:to_i).uniq
|
141
141
|
send many_equals, klass.find(normalized_ids)
|
142
142
|
end
|
143
143
|
|
@@ -10,6 +10,7 @@ describe ActiveRecord::JsonAssociations do
|
|
10
10
|
t.string :name
|
11
11
|
t.text :child_ids
|
12
12
|
t.text :fuzzy_ids
|
13
|
+
t.json :dependent_types_and_ids
|
13
14
|
t.timestamps
|
14
15
|
end
|
15
16
|
|
@@ -274,216 +275,4 @@ describe ActiveRecord::JsonAssociations do
|
|
274
275
|
end
|
275
276
|
end
|
276
277
|
end
|
277
|
-
|
278
|
-
describe ".has_many :parents, json_foreign_key: true" do
|
279
|
-
subject { Child.create! }
|
280
|
-
|
281
|
-
let(:parents) { [Parent.create!, Parent.create!, Parent.create!] }
|
282
|
-
|
283
|
-
describe "#parent_ids" do
|
284
|
-
it "is empty by default" do
|
285
|
-
expect(subject.parent_ids).to eq []
|
286
|
-
end
|
287
|
-
|
288
|
-
it "is an accessor" do
|
289
|
-
subject.parent_ids = parents.map(&:id)
|
290
|
-
expect(subject.parent_ids).to eq parents.map(&:id)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
describe "#parent_ids=" do
|
295
|
-
before { parents } # ensure parents exist
|
296
|
-
|
297
|
-
it "normalizes to integers" do
|
298
|
-
subject.parent_ids = ["1",2,"3"]
|
299
|
-
expect(subject.parent_ids).to eq [1,2,3]
|
300
|
-
end
|
301
|
-
|
302
|
-
it "ignores empty strings" do
|
303
|
-
subject.parent_ids = ["","1","2","3"]
|
304
|
-
expect(subject.parent_ids).to eq [1,2,3]
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
describe "#parents" do
|
309
|
-
it "returns an empty array when there are no parents" do
|
310
|
-
expect(subject.parents).to eq []
|
311
|
-
end
|
312
|
-
|
313
|
-
it "finds the children by id" do
|
314
|
-
subject.parent_ids = parents.map(&:id)
|
315
|
-
expect(subject.parents).to eq parents
|
316
|
-
end
|
317
|
-
|
318
|
-
it "is an accessor" do
|
319
|
-
subject.parents = parents
|
320
|
-
expect(subject.parents).to eq parents
|
321
|
-
end
|
322
|
-
|
323
|
-
context "finds records with the specified id" do
|
324
|
-
let(:child) { Child.create! }
|
325
|
-
|
326
|
-
it "as the whole json array" do
|
327
|
-
parent = Parent.create(children: [child])
|
328
|
-
expect(child.parents).to eq [parent]
|
329
|
-
end
|
330
|
-
|
331
|
-
it "at the beginning of the json array" do
|
332
|
-
parent = Parent.create(children: [child, Child.create!])
|
333
|
-
expect(child.parents).to eq [parent]
|
334
|
-
end
|
335
|
-
|
336
|
-
it "in the middle of the json array" do
|
337
|
-
parent = Parent.create(children: [Child.create!, child, Child.create!])
|
338
|
-
expect(child.parents).to eq [parent]
|
339
|
-
end
|
340
|
-
|
341
|
-
it "at the end of the json array" do
|
342
|
-
parent = Parent.create(children: [Child.create!, child])
|
343
|
-
expect(child.parents).to eq [parent]
|
344
|
-
end
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
describe "#parents?" do
|
349
|
-
it "returns false when there are no parents" do
|
350
|
-
expect(subject.parents?).to be_falsey
|
351
|
-
end
|
352
|
-
|
353
|
-
it "returns true when there are parents" do
|
354
|
-
subject.parents = parents
|
355
|
-
expect(subject.parents?).to be_truthy
|
356
|
-
end
|
357
|
-
end
|
358
|
-
|
359
|
-
describe "#build_parent" do
|
360
|
-
it "doesnt save the record" do
|
361
|
-
parent = subject.build_parent
|
362
|
-
expect(parent).to be_new_record
|
363
|
-
end
|
364
|
-
|
365
|
-
it "sets the foreign key column" do
|
366
|
-
parent = subject.build_parent
|
367
|
-
expect(parent.children).to eq([subject])
|
368
|
-
end
|
369
|
-
|
370
|
-
it "passes attributes through" do
|
371
|
-
parent = subject.build_parent(name: "Parent")
|
372
|
-
expect(parent.name).to eq("Parent")
|
373
|
-
end
|
374
|
-
end
|
375
|
-
|
376
|
-
describe "#create_parent" do
|
377
|
-
it "saves the record" do
|
378
|
-
parent = subject.create_parent
|
379
|
-
expect(parent).to be_persisted
|
380
|
-
end
|
381
|
-
|
382
|
-
it "sets the foreign key column" do
|
383
|
-
parent = subject.create_parent
|
384
|
-
expect(parent.children).to eq([subject])
|
385
|
-
end
|
386
|
-
|
387
|
-
it "passes attributes through" do
|
388
|
-
parent = subject.create_parent(name: "Parent")
|
389
|
-
expect(parent.name).to eq("Parent")
|
390
|
-
end
|
391
|
-
|
392
|
-
it "calls create on the model" do
|
393
|
-
expect(Parent).to receive(:create)
|
394
|
-
subject.create_parent
|
395
|
-
end
|
396
|
-
end
|
397
|
-
|
398
|
-
describe "#create_parent!" do
|
399
|
-
it "saves the record" do
|
400
|
-
parent = subject.create_parent!
|
401
|
-
expect(parent).to be_persisted
|
402
|
-
end
|
403
|
-
|
404
|
-
it "sets the foreign key column" do
|
405
|
-
parent = subject.create_parent!
|
406
|
-
expect(parent.children).to eq([subject])
|
407
|
-
end
|
408
|
-
|
409
|
-
it "passes attributes through" do
|
410
|
-
parent = subject.create_parent!(name: "Parent")
|
411
|
-
expect(parent.name).to eq("Parent")
|
412
|
-
end
|
413
|
-
|
414
|
-
it "calls create! on the model" do
|
415
|
-
expect(Parent).to receive(:create!)
|
416
|
-
subject.create_parent!
|
417
|
-
end
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
|
-
describe ".has_many :parents, json_foreign_key: :fuzzy_ids" do
|
422
|
-
subject { Pet.create! }
|
423
|
-
|
424
|
-
let(:parents) { [Parent.create!, Parent.create!, Parent.create!] }
|
425
|
-
|
426
|
-
describe "#parent_ids" do
|
427
|
-
it "is empty by default" do
|
428
|
-
expect(subject.parent_ids).to eq []
|
429
|
-
end
|
430
|
-
|
431
|
-
it "is an accessor" do
|
432
|
-
subject.parent_ids = parents.map(&:id)
|
433
|
-
expect(subject.parent_ids).to eq parents.map(&:id)
|
434
|
-
end
|
435
|
-
end
|
436
|
-
|
437
|
-
describe "#parents" do
|
438
|
-
it "returns an empty array when there are no parents" do
|
439
|
-
expect(subject.parents).to eq []
|
440
|
-
end
|
441
|
-
|
442
|
-
it "finds the parents by id" do
|
443
|
-
subject.parent_ids = parents.map(&:id)
|
444
|
-
expect(subject.parents).to eq parents
|
445
|
-
end
|
446
|
-
|
447
|
-
it "is an accessor" do
|
448
|
-
subject.parents = parents
|
449
|
-
expect(subject.parents).to eq parents
|
450
|
-
end
|
451
|
-
|
452
|
-
context "finds records with the specified id" do
|
453
|
-
let(:pet) { Pet.create! }
|
454
|
-
|
455
|
-
it "as the whole json array" do
|
456
|
-
parent = Parent.create(fuzzies: [pet])
|
457
|
-
expect(pet.parents).to eq [parent]
|
458
|
-
end
|
459
|
-
|
460
|
-
it "at the beginning of the json array" do
|
461
|
-
parent = Parent.create(fuzzies: [pet, Pet.create!])
|
462
|
-
expect(pet.parents).to eq [parent]
|
463
|
-
end
|
464
|
-
|
465
|
-
it "in the middle of the json array" do
|
466
|
-
parent = Parent.create(fuzzies: [Pet.create!, pet, Pet.create!])
|
467
|
-
expect(pet.parents).to eq [parent]
|
468
|
-
end
|
469
|
-
|
470
|
-
it "at the end of the json array" do
|
471
|
-
parent = Parent.create(fuzzies: [Pet.create!, pet])
|
472
|
-
expect(pet.parents).to eq [parent]
|
473
|
-
end
|
474
|
-
end
|
475
|
-
end
|
476
|
-
|
477
|
-
describe "#parents?" do
|
478
|
-
it "returns false when there are no parents" do
|
479
|
-
expect(subject.parents?).to be_falsey
|
480
|
-
end
|
481
|
-
|
482
|
-
it "returns true when there are parents" do
|
483
|
-
subject.parents = parents
|
484
|
-
expect(subject.parents?).to be_truthy
|
485
|
-
end
|
486
|
-
end
|
487
|
-
end
|
488
278
|
end
|
489
|
-
|
@@ -0,0 +1,255 @@
|
|
1
|
+
require "active_record/json_associations"
|
2
|
+
|
3
|
+
describe ActiveRecord::JsonAssociations do
|
4
|
+
before do
|
5
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
6
|
+
|
7
|
+
silence_stream(STDOUT) do
|
8
|
+
ActiveRecord::Schema.define do
|
9
|
+
create_table :parents do |t|
|
10
|
+
t.string :name
|
11
|
+
t.text :child_ids
|
12
|
+
t.text :fuzzy_ids
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :children do |t|
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :pets do |t|
|
21
|
+
t.timestamps
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Parent < ActiveRecord::Base
|
27
|
+
belongs_to_many :children, touch: true
|
28
|
+
belongs_to_many :fuzzies, class_name: "Pet"
|
29
|
+
end
|
30
|
+
|
31
|
+
class Child < ActiveRecord::Base
|
32
|
+
has_many :parents, json_foreign_key: true
|
33
|
+
end
|
34
|
+
|
35
|
+
class Pet < ActiveRecord::Base
|
36
|
+
has_many :parents, json_foreign_key: :fuzzy_ids
|
37
|
+
|
38
|
+
# ensure that regular .has_many invocations still work
|
39
|
+
has_many :fallback_parents
|
40
|
+
has_many :fallback_parents_with_options, class_name: "Pet"
|
41
|
+
has_many :fallback_parents_with_scope, -> { order(:id) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe ".has_many :parents, json_foreign_key: true" do
|
46
|
+
subject { Child.create! }
|
47
|
+
|
48
|
+
let(:parents) { [Parent.create!, Parent.create!, Parent.create!] }
|
49
|
+
|
50
|
+
describe "#parent_ids" do
|
51
|
+
it "is empty by default" do
|
52
|
+
expect(subject.parent_ids).to eq []
|
53
|
+
end
|
54
|
+
|
55
|
+
it "is an accessor" do
|
56
|
+
subject.parent_ids = parents.map(&:id)
|
57
|
+
expect(subject.parent_ids).to eq parents.map(&:id)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#parent_ids=" do
|
62
|
+
before { parents } # ensure parents exist
|
63
|
+
|
64
|
+
it "normalizes to integers" do
|
65
|
+
subject.parent_ids = ["1",2,"3"]
|
66
|
+
expect(subject.parent_ids).to eq [1,2,3]
|
67
|
+
end
|
68
|
+
|
69
|
+
it "ignores empty strings" do
|
70
|
+
subject.parent_ids = ["","1","2","3"]
|
71
|
+
expect(subject.parent_ids).to eq [1,2,3]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#parents" do
|
76
|
+
it "returns an empty array when there are no parents" do
|
77
|
+
expect(subject.parents).to eq []
|
78
|
+
end
|
79
|
+
|
80
|
+
it "finds the children by id" do
|
81
|
+
subject.parent_ids = parents.map(&:id)
|
82
|
+
expect(subject.parents).to eq parents
|
83
|
+
end
|
84
|
+
|
85
|
+
it "is an accessor" do
|
86
|
+
subject.parents = parents
|
87
|
+
expect(subject.parents).to eq parents
|
88
|
+
end
|
89
|
+
|
90
|
+
context "finds records with the specified id" do
|
91
|
+
let(:child) { Child.create! }
|
92
|
+
|
93
|
+
it "as the whole json array" do
|
94
|
+
parent = Parent.create(children: [child])
|
95
|
+
expect(child.parents).to eq [parent]
|
96
|
+
end
|
97
|
+
|
98
|
+
it "at the beginning of the json array" do
|
99
|
+
parent = Parent.create(children: [child, Child.create!])
|
100
|
+
expect(child.parents).to eq [parent]
|
101
|
+
end
|
102
|
+
|
103
|
+
it "in the middle of the json array" do
|
104
|
+
parent = Parent.create(children: [Child.create!, child, Child.create!])
|
105
|
+
expect(child.parents).to eq [parent]
|
106
|
+
end
|
107
|
+
|
108
|
+
it "at the end of the json array" do
|
109
|
+
parent = Parent.create(children: [Child.create!, child])
|
110
|
+
expect(child.parents).to eq [parent]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#parents?" do
|
116
|
+
it "returns false when there are no parents" do
|
117
|
+
expect(subject.parents?).to be_falsey
|
118
|
+
end
|
119
|
+
|
120
|
+
it "returns true when there are parents" do
|
121
|
+
subject.parents = parents
|
122
|
+
expect(subject.parents?).to be_truthy
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "#build_parent" do
|
127
|
+
it "doesnt save the record" do
|
128
|
+
parent = subject.build_parent
|
129
|
+
expect(parent).to be_new_record
|
130
|
+
end
|
131
|
+
|
132
|
+
it "sets the foreign key column" do
|
133
|
+
parent = subject.build_parent
|
134
|
+
expect(parent.children).to eq([subject])
|
135
|
+
end
|
136
|
+
|
137
|
+
it "passes attributes through" do
|
138
|
+
parent = subject.build_parent(name: "Parent")
|
139
|
+
expect(parent.name).to eq("Parent")
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe "#create_parent" do
|
144
|
+
it "saves the record" do
|
145
|
+
parent = subject.create_parent
|
146
|
+
expect(parent).to be_persisted
|
147
|
+
end
|
148
|
+
|
149
|
+
it "sets the foreign key column" do
|
150
|
+
parent = subject.create_parent
|
151
|
+
expect(parent.children).to eq([subject])
|
152
|
+
end
|
153
|
+
|
154
|
+
it "passes attributes through" do
|
155
|
+
parent = subject.create_parent(name: "Parent")
|
156
|
+
expect(parent.name).to eq("Parent")
|
157
|
+
end
|
158
|
+
|
159
|
+
it "calls create on the model" do
|
160
|
+
expect(Parent).to receive(:create)
|
161
|
+
subject.create_parent
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "#create_parent!" do
|
166
|
+
it "saves the record" do
|
167
|
+
parent = subject.create_parent!
|
168
|
+
expect(parent).to be_persisted
|
169
|
+
end
|
170
|
+
|
171
|
+
it "sets the foreign key column" do
|
172
|
+
parent = subject.create_parent!
|
173
|
+
expect(parent.children).to eq([subject])
|
174
|
+
end
|
175
|
+
|
176
|
+
it "passes attributes through" do
|
177
|
+
parent = subject.create_parent!(name: "Parent")
|
178
|
+
expect(parent.name).to eq("Parent")
|
179
|
+
end
|
180
|
+
|
181
|
+
it "calls create! on the model" do
|
182
|
+
expect(Parent).to receive(:create!)
|
183
|
+
subject.create_parent!
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe ".has_many :parents, json_foreign_key: :fuzzy_ids" do
|
189
|
+
subject { Pet.create! }
|
190
|
+
|
191
|
+
let(:parents) { [Parent.create!, Parent.create!, Parent.create!] }
|
192
|
+
|
193
|
+
describe "#parent_ids" do
|
194
|
+
it "is empty by default" do
|
195
|
+
expect(subject.parent_ids).to eq []
|
196
|
+
end
|
197
|
+
|
198
|
+
it "is an accessor" do
|
199
|
+
subject.parent_ids = parents.map(&:id)
|
200
|
+
expect(subject.parent_ids).to eq parents.map(&:id)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
describe "#parents" do
|
205
|
+
it "returns an empty array when there are no parents" do
|
206
|
+
expect(subject.parents).to eq []
|
207
|
+
end
|
208
|
+
|
209
|
+
it "finds the parents by id" do
|
210
|
+
subject.parent_ids = parents.map(&:id)
|
211
|
+
expect(subject.parents).to eq parents
|
212
|
+
end
|
213
|
+
|
214
|
+
it "is an accessor" do
|
215
|
+
subject.parents = parents
|
216
|
+
expect(subject.parents).to eq parents
|
217
|
+
end
|
218
|
+
|
219
|
+
context "finds records with the specified id" do
|
220
|
+
let(:pet) { Pet.create! }
|
221
|
+
|
222
|
+
it "as the whole json array" do
|
223
|
+
parent = Parent.create(fuzzies: [pet])
|
224
|
+
expect(pet.parents).to eq [parent]
|
225
|
+
end
|
226
|
+
|
227
|
+
it "at the beginning of the json array" do
|
228
|
+
parent = Parent.create(fuzzies: [pet, Pet.create!])
|
229
|
+
expect(pet.parents).to eq [parent]
|
230
|
+
end
|
231
|
+
|
232
|
+
it "in the middle of the json array" do
|
233
|
+
parent = Parent.create(fuzzies: [Pet.create!, pet, Pet.create!])
|
234
|
+
expect(pet.parents).to eq [parent]
|
235
|
+
end
|
236
|
+
|
237
|
+
it "at the end of the json array" do
|
238
|
+
parent = Parent.create(fuzzies: [Pet.create!, pet])
|
239
|
+
expect(pet.parents).to eq [parent]
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe "#parents?" do
|
245
|
+
it "returns false when there are no parents" do
|
246
|
+
expect(subject.parents?).to be_falsey
|
247
|
+
end
|
248
|
+
|
249
|
+
it "returns true when there are parents" do
|
250
|
+
subject.parents = parents
|
251
|
+
expect(subject.parents?).to be_truthy
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-json_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.13.
|
4
|
+
version: 0.13.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -145,7 +145,8 @@ files:
|
|
145
145
|
- gemfiles/rails_7.2.gemfile
|
146
146
|
- lib/active_record/json_associations.rb
|
147
147
|
- lib/active_record/json_associations/version.rb
|
148
|
-
- spec/
|
148
|
+
- spec/belongs_to_many_spec.rb
|
149
|
+
- spec/has_many_spec.rb
|
149
150
|
- spec/spec_helper.rb
|
150
151
|
homepage: https://github.com/botandrose/active_record-json_associations
|
151
152
|
licenses:
|
@@ -171,5 +172,6 @@ signing_key:
|
|
171
172
|
specification_version: 4
|
172
173
|
summary: Instead of a many-to-many join table, serialize the ids into a JSON array.
|
173
174
|
test_files:
|
174
|
-
- spec/
|
175
|
+
- spec/belongs_to_many_spec.rb
|
176
|
+
- spec/has_many_spec.rb
|
175
177
|
- spec/spec_helper.rb
|