active_record-json_associations 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e03d284df9e4faf9319475962d17aee50025fc58407f1cb641819c070958eca6
4
- data.tar.gz: 026dff5c42712728a8c85a59e413265f3c1c09922ec1fc366fee5db3404fb42a
3
+ metadata.gz: 3dcecc3d5ba41f7deed96e8178f04221c39678799e3706b0c95938f91f197daf
4
+ data.tar.gz: c9fcdad4e8cbe19a16fc7bf3d27c8e176e5cedb06a415e95c36aff1b734ffb85
5
5
  SHA512:
6
- metadata.gz: 449f81549f3ed177a88ce17070a92b2f6b07f252193ab1cf64d706d51c85d9065f636af21242bd3a689f1b2402384d2d0487c58cc74de1f07b1c3f441da3b34d
7
- data.tar.gz: 5dea4c68f936961403ca768f7775795fe810df3e811cadb147901faa0e2e5313512853aed0d5bdaaabefafaeb60f9cdc3375d5d5d986ca2cde42be6759b94125
6
+ metadata.gz: a13fc51a526b61d909d6559e22969a9e4f7c75e880efc2ffc50b0c01e1b2aa7daf4858c33beb0dfbac7a57aa77329fd030af02db02efcb7058b8b78f93386328
7
+ data.tar.gz: e6d33f695da6f13a6ed3253ab90a0246e77a788f7593b3d1338ec8e11d6e449b4f294a8c690f5f4f2f506fb10478c3494e198fba7275d50b8d3f72f38e0d1485
data/README.md CHANGED
@@ -63,6 +63,18 @@ parent = Parent.create children: [child]
63
63
  child.parents == [parent] #=> true
64
64
  ```
65
65
 
66
+ I can't figure out how to support building records off the association, so instead there are the `has_one`/`belongs_to` builder methods:
67
+
68
+ ```ruby
69
+ child.build_parent
70
+ child.create_parent
71
+ child.create_parent!
72
+
73
+ # also supports optional attributes:
74
+
75
+ child.build_parent(name: "Momma")
76
+ ```
77
+
66
78
  ## Requirements
67
79
 
68
80
  * ActiveRecord 5.0+
@@ -105,8 +105,12 @@ module ActiveRecord
105
105
  one_ids_equals = :"#{one_ids}="
106
106
  many_equals = :"#{many}="
107
107
  many_eh = :"#{many}?"
108
+ build_one = :"build_#{one}"
109
+ create_one = :"create_#{one}"
110
+ create_one_bang = :"create_#{one}!"
108
111
 
109
112
  class_name = options[:class_name] || one.classify
113
+ klass = class_name.constantize
110
114
 
111
115
  foreign_key = options[:json_foreign_key]
112
116
  foreign_key = :"#{model_name.singular}_ids" if foreign_key == true
@@ -117,13 +121,11 @@ module ActiveRecord
117
121
  end
118
122
 
119
123
  define_method one_ids_equals do |ids|
120
- klass = class_name.constantize
121
124
  normalized_ids = Array(ids).select(&:present?).map(&:to_i)
122
125
  send many_equals, klass.find(normalized_ids)
123
126
  end
124
127
 
125
128
  define_method many do
126
- klass = class_name.constantize
127
129
  FIELD_INCLUDE_SCOPE_BUILDER_PROC.call(klass, foreign_key, id)
128
130
  end
129
131
 
@@ -138,6 +140,18 @@ module ActiveRecord
138
140
  define_method many_eh do
139
141
  send(many).any?
140
142
  end
143
+
144
+ define_method build_one do |attributes={}|
145
+ klass.new attributes.merge!(foreign_key => [id])
146
+ end
147
+
148
+ define_method create_one do |attributes={}|
149
+ klass.create attributes.merge!(foreign_key => [id])
150
+ end
151
+
152
+ define_method create_one_bang do |attributes={}|
153
+ klass.create! attributes.merge!(foreign_key => [id])
154
+ end
141
155
  }
142
156
  end
143
157
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module JsonAssociations
3
- VERSION = "0.9.1"
3
+ VERSION = "0.10.0"
4
4
  end
5
5
  end
@@ -7,6 +7,7 @@ describe ActiveRecord::JsonAssociations do
7
7
  silence_stream(STDOUT) do
8
8
  ActiveRecord::Schema.define do
9
9
  create_table :parents do |t|
10
+ t.string :name
10
11
  t.text :child_ids
11
12
  t.text :fuzzy_ids
12
13
  t.timestamps
@@ -346,6 +347,67 @@ describe ActiveRecord::JsonAssociations do
346
347
  expect(subject.parents?).to be_truthy
347
348
  end
348
349
  end
350
+
351
+ describe "#build_parent" do
352
+ it "doesnt save the record" do
353
+ parent = subject.build_parent
354
+ expect(parent).to be_new_record
355
+ end
356
+
357
+ it "sets the foreign key column" do
358
+ parent = subject.build_parent
359
+ expect(parent.children).to eq([subject])
360
+ end
361
+
362
+ it "passes attributes through" do
363
+ parent = subject.build_parent(name: "Parent")
364
+ expect(parent.name).to eq("Parent")
365
+ end
366
+ end
367
+
368
+ describe "#create_parent" do
369
+ it "saves the record" do
370
+ parent = subject.create_parent
371
+ expect(parent).to be_persisted
372
+ end
373
+
374
+ it "sets the foreign key column" do
375
+ parent = subject.create_parent
376
+ expect(parent.children).to eq([subject])
377
+ end
378
+
379
+ it "passes attributes through" do
380
+ parent = subject.create_parent(name: "Parent")
381
+ expect(parent.name).to eq("Parent")
382
+ end
383
+
384
+ it "calls create on the model" do
385
+ expect(Parent).to receive(:create)
386
+ subject.create_parent
387
+ end
388
+ end
389
+
390
+ describe "#create_parent!" do
391
+ it "saves the record" do
392
+ parent = subject.create_parent!
393
+ expect(parent).to be_persisted
394
+ end
395
+
396
+ it "sets the foreign key column" do
397
+ parent = subject.create_parent!
398
+ expect(parent.children).to eq([subject])
399
+ end
400
+
401
+ it "passes attributes through" do
402
+ parent = subject.create_parent!(name: "Parent")
403
+ expect(parent.name).to eq("Parent")
404
+ end
405
+
406
+ it "calls create! on the model" do
407
+ expect(Parent).to receive(:create!)
408
+ subject.create_parent!
409
+ end
410
+ end
349
411
  end
350
412
 
351
413
  describe ".has_many :parents, json_foreign_key: :fuzzy_ids" do
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.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-24 00:00:00.000000000 Z
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord