passive_record 0.3.13 → 0.3.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/passive_record/associations.rb +12 -4
- data/lib/passive_record/associations/has_many.rb +4 -0
- data/lib/passive_record/associations/has_many_through.rb +9 -4
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b01350b2c51dae72a7383e4752d6bfaf450ba98
|
4
|
+
data.tar.gz: 3b0bcbc94736f53de51156504791562116687bdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
|
110
|
-
intermediary.create(target_name.singularize + "_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
|
@@ -8,10 +8,15 @@ module PassiveRecord
|
|
8
8
|
|
9
9
|
class HasManyThroughRelation < HasManyRelation
|
10
10
|
def <<(child)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
|
data/spec/passive_record_spec.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2016-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|