passive_record 0.3.16 → 0.3.17
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57f65c2807d7cfe4fdedb6ed50a176e4d93f6372
|
4
|
+
data.tar.gz: e2ec1ce8b73eee02815c22acf0ce143a7388bfc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3507ad80dd941d6d6afb5a5efe3542ae4b8052d53c31dfaa2c58ac737c5825d16c02b1189677e60ca80949d2e505e926c652ee46883da5ec9dcd0af3f07f055
|
7
|
+
data.tar.gz: 49c26b8bdfb5ffbfd4c8f4a0b152ea19763214ee8edae5ee4ed65be561e9ad7c4f15bc223c38dcd28c6c2a09ca1ab51d78ae938496b191282c746a452df687c0
|
@@ -17,17 +17,19 @@ module PassiveRecord
|
|
17
17
|
send(:"#{parent_model_id_field}=", parent_model.id)
|
18
18
|
else
|
19
19
|
nested_ids_field = nested_association.children_name_sym.to_s.singularize + "_ids"
|
20
|
-
intermediary_relation.
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
intermediary_model = intermediary_relation.singular? ?
|
21
|
+
intermediary_relation.lookup_or_create :
|
22
|
+
intermediary_relation.where(parent_model_id_field => parent_model.id).first_or_create
|
23
|
+
|
24
|
+
intermediary_model.update(
|
25
|
+
nested_ids_field => intermediary_model.send(nested_ids_field) + [ child.id ]
|
26
|
+
)
|
24
27
|
end
|
25
28
|
else
|
26
|
-
intermediary_relation.
|
29
|
+
intermediary_model = intermediary_relation.
|
27
30
|
where(
|
28
31
|
association.target_name_symbol.to_s.singularize + "_id" => child.id).
|
29
32
|
first_or_create
|
30
|
-
|
31
33
|
end
|
32
34
|
self
|
33
35
|
end
|
@@ -58,9 +60,10 @@ module PassiveRecord
|
|
58
60
|
end
|
59
61
|
|
60
62
|
def all
|
61
|
-
|
62
|
-
|
63
|
-
|
63
|
+
join_results = intermediate_results
|
64
|
+
if intermediate_results && !join_results.empty?
|
65
|
+
final_results = join_results.flat_map(&nested_association.target_name_symbol)
|
66
|
+
if final_results.first.is_a?(Associations::Relation)
|
64
67
|
final_results.flat_map(&:all)
|
65
68
|
else
|
66
69
|
Array(final_results)
|
@@ -71,11 +74,15 @@ module PassiveRecord
|
|
71
74
|
end
|
72
75
|
|
73
76
|
def intermediary_relation
|
74
|
-
association.base_association.to_relation(parent_model)
|
77
|
+
@intermediary_relation ||= association.base_association.to_relation(parent_model)
|
75
78
|
end
|
76
79
|
|
77
80
|
def intermediate_results
|
78
|
-
intermediary_relation.
|
81
|
+
if intermediary_relation.singular?
|
82
|
+
Array(intermediary_relation.lookup)
|
83
|
+
else
|
84
|
+
intermediary_relation.all
|
85
|
+
end
|
79
86
|
end
|
80
87
|
end
|
81
88
|
end
|
data/spec/passive_record_spec.rb
CHANGED
@@ -65,7 +65,7 @@ describe "passive record models" do
|
|
65
65
|
to eq("Family::Dog (id: #{dog.id.inspect}, breed: \"#{dog.breed}\", created_at: #{dog.created_at}, sound: \"bark\", child_id: #{child.id.inspect})")
|
66
66
|
|
67
67
|
expect(child.inspect).
|
68
|
-
to eq("Family::Child (id: #{child.id.inspect}, created_at: #{child.created_at}, name: \"Alice\", toy_id: nil, dog_ids: [#{dog.id.inspect}], parent_id: nil, secret_club_ids: [])")
|
68
|
+
to eq("Family::Child (id: #{child.id.inspect}, created_at: #{child.created_at}, name: \"Alice\", toy_id: nil, toy_quality_ids: [], dog_ids: [#{dog.id.inspect}], parent_id: nil, secret_club_ids: [])")
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -366,7 +366,7 @@ 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
|
-
|
369
|
+
expect { 3.times { parent.toys << Family::Toy.create } }.to change {Family::Toy.count}.by(3)
|
370
370
|
expect { 3.times { parent.toys << Family::Toy.create } }.to change {parent.toys.count}.by(3)
|
371
371
|
expect(parent.toys.last.child).not_to be_nil
|
372
372
|
|
@@ -453,5 +453,18 @@ describe "passive record models" do
|
|
453
453
|
expect(child.secret_clubs.first.create_child).to be_a(Family::Child)
|
454
454
|
end
|
455
455
|
end
|
456
|
+
|
457
|
+
context 'has many through has one' do
|
458
|
+
it 'should manage relationships' do
|
459
|
+
child = Family::Child.create
|
460
|
+
toy_quality = child.create_toy_quality(name: 'fun')
|
461
|
+
child.create_toy_quality(name: 'cool')
|
462
|
+
child.create_toy_quality(name: 'radical')
|
463
|
+
|
464
|
+
expect(child.toy_qualities.all).to include(toy_quality)
|
465
|
+
expect(child.toy_qualities.count).to eq(3)
|
466
|
+
expect(child.toy_qualities.map(&:name)).to eq(%w[ fun cool radical ])
|
467
|
+
end
|
468
|
+
end
|
456
469
|
end
|
457
470
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -26,14 +26,21 @@ module Family
|
|
26
26
|
after_create { @sound = 'bark' }
|
27
27
|
end
|
28
28
|
|
29
|
+
class ToyQuality < Model
|
30
|
+
attr_accessor :name
|
31
|
+
belongs_to :toy
|
32
|
+
end
|
33
|
+
|
29
34
|
class Toy < Model
|
30
35
|
belongs_to :child
|
36
|
+
has_many :toy_qualities
|
31
37
|
attr_reader :kind
|
32
38
|
after_create {@kind = %w[ stuffed_animal blocks cards ].sample}
|
33
39
|
end
|
34
40
|
|
35
41
|
class Child < Model
|
36
42
|
has_one :toy
|
43
|
+
has_many :toy_qualities, :through => :toy
|
37
44
|
has_many :dogs
|
38
45
|
belongs_to :parent
|
39
46
|
has_and_belongs_to_many :secret_clubs
|
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.17
|
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-03-
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|