passive_record 0.3.13 → 0.3.14

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
  SHA1:
3
- metadata.gz: de382da8a3a2ab040066d8602ae28c3949c53d63
4
- data.tar.gz: 91c7c31efa8ba22d39dbe0197c6260b5021e48b3
3
+ metadata.gz: 4b01350b2c51dae72a7383e4752d6bfaf450ba98
4
+ data.tar.gz: 3b0bcbc94736f53de51156504791562116687bdf
5
5
  SHA512:
6
- metadata.gz: 7e90366ff1718151ab079032aa41755c40f1bfb68c8fcedc45f58804f26f728405b840d2e3a82043b92de2afdbb7a2fe21999bd099eaa41632941df77ee3e183
7
- data.tar.gz: be96a58c45ac7cef2f498a6e0738ab4b1ed8f2119f904442a3c392a7cccb0be7b5e9661aa29db6df08c8042e437dbe76dc9fb72fe509dc290977e3b2e53e50b5
6
+ metadata.gz: 0a3f2c488bf01cea83122cfb4cb5a8b8d4f0e41f175827f0ccc0ab404a71b8b6c0104b170fe0cb15e68dd29da32424c53df0a714c603f29c0a32c9b7470d9684
7
+ data.tar.gz: bd24c57315e0b57bd6df836e19e55471dd90c699bbae4476e9eb4c3c0beea2dbb242555ee396e7a9404595b7c2094f32b378c864dfbb5153083f8856a763683e
data/README.md CHANGED
@@ -96,6 +96,7 @@ PassiveRecord may be right for you!
96
96
 
97
97
  A class `Role` which is declared to `include PassiveRecord` will gain the following instance methods:
98
98
  - `role.update(attrs_hash)`
99
+ - `role.destroy`
99
100
  - `role.to_h`
100
101
  - We override `role.inspect` to show ID and visible attributes
101
102
 
@@ -71,7 +71,7 @@ module PassiveRecord
71
71
  define_method(:"#{child_name_sym}_id=") do |new_child_id|
72
72
  relation = instance_eval{relata}.detect { |rel| rel.association == association }
73
73
  # detach existing child...
74
- relation.lookup.send(:"#{relation.parent_model_id_field}=", nil)
74
+ relation.lookup&.send(:"#{relation.parent_model_id_field}=", nil)
75
75
 
76
76
  relation.child_class.
77
77
  find(new_child_id).
@@ -95,6 +95,10 @@ module PassiveRecord
95
95
  associate!(association)
96
96
 
97
97
  define_method(:"#{collection_name_sym}=") do |new_collection|
98
+ send(:"#{collection_name_sym.to_s.singularize}_ids=", new_collection.map(&:id))
99
+ end
100
+
101
+ define_method(:"#{collection_name_sym.to_s.singularize}_ids=") do |new_collection_ids|
98
102
  relation = instance_eval{relata}.detect { |rel| rel.association == association }
99
103
 
100
104
  intermediary = relation.intermediary_relation
@@ -106,8 +110,8 @@ module PassiveRecord
106
110
 
107
111
  # add in new ones...
108
112
  target_name = relation.association.target_name_symbol.to_s
109
- new_collection.each do |child|
110
- intermediary.create(target_name.singularize + "_id" => child.id, relation.parent_model_id_field => relation.id )
113
+ new_collection_ids.each do |child_id|
114
+ intermediary.create(target_name.singularize + "_id" => child_id, relation.parent_model_id_field => relation.id )
111
115
  end
112
116
  end
113
117
  else
@@ -128,6 +132,11 @@ module PassiveRecord
128
132
  child.send(:"#{relation.parent_model_id_field}=", relation.id)
129
133
  end
130
134
  end
135
+
136
+ define_method(:"#{collection_name_sym.to_s.singularize}_ids=") do |new_collection_ids|
137
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
138
+ send(:"#{collection_name_sym}=", relation.child_class.find(new_collection_ids))
139
+ end
131
140
  end
132
141
 
133
142
  define_method(collection_name_sym) do
@@ -141,7 +150,6 @@ module PassiveRecord
141
150
 
142
151
  define_method(:"create_#{collection_name_sym.to_s.singularize}") do |attrs={}|
143
152
  relation = instance_eval{relata}.detect { |rel| rel.association == association }
144
- # binding.pry
145
153
  relation.create(attrs)
146
154
  end
147
155
  end
@@ -19,6 +19,10 @@ module PassiveRecord
19
19
  end
20
20
  def_delegators :all, :each
21
21
 
22
+ def last
23
+ all.last
24
+ end
25
+
22
26
  def all?(*args)
23
27
  all.all?(*args)
24
28
  end
@@ -8,10 +8,15 @@ module PassiveRecord
8
8
 
9
9
  class HasManyThroughRelation < HasManyRelation
10
10
  def <<(child)
11
- intermediary_relation.
12
- where(
13
- association.target_name_symbol.to_s.singularize + "_id" => child.id).
14
- first_or_create
11
+ if nested_association.is_a?(HasManyAssociation)
12
+ intermediary_relation.create(association.target_name_symbol.to_s.singularize + "_ids" => [child.id])
13
+ else
14
+ intermediary_relation.
15
+ where(
16
+ association.target_name_symbol.to_s.singularize + "_id" => child.id).
17
+ first_or_create
18
+
19
+ end
15
20
  self
16
21
  end
17
22
 
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.13"
3
+ VERSION = "0.3.14"
4
4
  end
@@ -366,6 +366,15 @@ describe "passive record models" do
366
366
  expect { parent.create_toy(child: child) }.to change {Family::Toy.count}.by(1)
367
367
  expect(Family::Toy.last.child).to eq(child)
368
368
  expect(Family::Toy.last.child.parent).to eq(parent)
369
+
370
+ expect { 3.times { parent.toys << Family::Toy.create } }.to change {parent.toys.count}.by(3)
371
+ expect(parent.toys.last.child).not_to be_nil
372
+
373
+ expect{parent.create_toy}.to change{Family::Toy.count}.by(1)
374
+ end
375
+
376
+ it 'should construct intermediary relations with many-through-many' do
377
+ expect{parent.create_dog}.to change{Family::Dog.count}.by(1)
369
378
  end
370
379
 
371
380
  it 'should accept class name' do
@@ -389,6 +398,10 @@ describe "passive record models" do
389
398
  expect(patient.doctors.all).to eq([doctor])
390
399
  expect(doctor.patients.all).to eq([patient])
391
400
  end
401
+
402
+ it 'should handle insertion' do
403
+ expect{patient.doctors << Doctor.create}.to change{patient.doctors.count}.by(1)
404
+ end
392
405
  end
393
406
 
394
407
  context 'self-referential many-to-many' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.13
4
+ version: 0.3.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-29 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport