torque-postgresql 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/torque/postgresql/associations/belongs_to_many_association.rb +14 -10
- data/lib/torque/postgresql/autosave_association.rb +4 -4
- data/lib/torque/postgresql/reflection/belongs_to_many_reflection.rb +16 -4
- data/lib/torque/postgresql/version.rb +1 -1
- data/spec/tests/belongs_to_many_spec.rb +6 -0
- data/spec/tests/has_many_spec.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e426686cf04fcf990d84b945abe3b3435168237f1ff24c6f23ac3b4941bbc4e
|
4
|
+
data.tar.gz: 2f936f5676f80804e4766bc9bf64b81d70ddbd08e9409b373b0d59d7d20e8bf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 373d085090e02e76eb2523f7c2d5a32434c002f712cf892aa38f81bc9935a51f2426054c97bb5df534c47e23fd1800d84b670b9443266908e2f076f20928e7a2
|
7
|
+
data.tar.gz: f0441cc66de2ed015d0b8dbb13ae88dc4074a54c8477fc7a31fc1d51e2bfd7cb57ab0e3de529dcbd4feb3fa72dd1fc8894738868e8bfa36ad9490badc3d1c990
|
@@ -28,11 +28,11 @@ module Torque
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def ids_reader
|
31
|
-
owner[
|
31
|
+
owner[source_attr]
|
32
32
|
end
|
33
33
|
|
34
34
|
def ids_writer(new_ids)
|
35
|
-
column =
|
35
|
+
column = source_attr
|
36
36
|
command = owner.persisted? ? :update_column : :write_attribute
|
37
37
|
owner.public_send(command, column, new_ids.presence)
|
38
38
|
@association_scope = nil
|
@@ -41,8 +41,8 @@ module Torque
|
|
41
41
|
def insert_record(record, *)
|
42
42
|
super
|
43
43
|
|
44
|
-
attribute = (ids_reader || owner[
|
45
|
-
attribute.push(record[
|
44
|
+
attribute = (ids_reader || owner[source_attr] = [])
|
45
|
+
attribute.push(record[klass_attr])
|
46
46
|
record
|
47
47
|
end
|
48
48
|
|
@@ -51,8 +51,8 @@ module Torque
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def include?(record)
|
54
|
-
list = owner[
|
55
|
-
ids_reader && ids_reader.include?(record[
|
54
|
+
list = owner[source_attr]
|
55
|
+
ids_reader && ids_reader.include?(record[klass_attr])
|
56
56
|
end
|
57
57
|
|
58
58
|
private
|
@@ -66,7 +66,7 @@ module Torque
|
|
66
66
|
# When the idea is to nulligy the association, then just set the owner
|
67
67
|
# +primary_key+ as empty
|
68
68
|
def delete_count(method, scope, ids = nil)
|
69
|
-
ids ||= scope.pluck(
|
69
|
+
ids ||= scope.pluck(klass_attr)
|
70
70
|
scope.delete_all if method == :delete_all
|
71
71
|
remove_stash_records(ids)
|
72
72
|
end
|
@@ -77,13 +77,13 @@ module Torque
|
|
77
77
|
|
78
78
|
# Deletes the records according to the <tt>:dependent</tt> option.
|
79
79
|
def delete_records(records, method)
|
80
|
-
ids = Array.wrap(records).each_with_object(
|
80
|
+
ids = Array.wrap(records).each_with_object(klass_attr).map(&:[])
|
81
81
|
|
82
82
|
if method == :destroy
|
83
83
|
records.each(&:destroy!)
|
84
84
|
remove_stash_records(ids)
|
85
85
|
else
|
86
|
-
scope = self.scope.where(
|
86
|
+
scope = self.scope.where(klass_attr => records)
|
87
87
|
delete_count(method, scope, ids)
|
88
88
|
end
|
89
89
|
end
|
@@ -99,10 +99,14 @@ module Torque
|
|
99
99
|
ids_writer(ids_reader - Array.wrap(ids))
|
100
100
|
end
|
101
101
|
|
102
|
-
def
|
102
|
+
def source_attr
|
103
103
|
reflection.foreign_key
|
104
104
|
end
|
105
105
|
|
106
|
+
def klass_attr
|
107
|
+
reflection.active_record_primary_key
|
108
|
+
end
|
109
|
+
|
106
110
|
def difference(a, b)
|
107
111
|
a - b
|
108
112
|
end
|
@@ -25,11 +25,11 @@ module Torque
|
|
25
25
|
association = association_instance_get(reflection.name)
|
26
26
|
return unless association
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
klass_attr = reflection.active_record_primary_key
|
29
|
+
source_attr = reflection.foreign_key
|
30
30
|
|
31
|
-
records = association.target.each_with_object(
|
32
|
-
write_attribute(
|
31
|
+
records = association.target.each_with_object(klass_attr)
|
32
|
+
write_attribute(source_attr, records.map(&:read_attribute).compact)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -10,6 +10,10 @@ module Torque
|
|
10
10
|
true
|
11
11
|
end
|
12
12
|
|
13
|
+
def belongs_to?
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
13
17
|
def collection?
|
14
18
|
true
|
15
19
|
end
|
@@ -19,7 +23,7 @@ module Torque
|
|
19
23
|
end
|
20
24
|
|
21
25
|
def foreign_key
|
22
|
-
@foreign_key ||= options[:
|
26
|
+
@foreign_key ||= options[:foreign_key] || derive_foreign_key.freeze
|
23
27
|
end
|
24
28
|
|
25
29
|
def association_foreign_key
|
@@ -27,16 +31,24 @@ module Torque
|
|
27
31
|
end
|
28
32
|
|
29
33
|
def active_record_primary_key
|
30
|
-
@active_record_primary_key ||= options[:
|
34
|
+
@active_record_primary_key ||= options[:primary_key] || derive_primary_key
|
35
|
+
end
|
36
|
+
|
37
|
+
def join_primary_key(*)
|
38
|
+
active_record_primary_key
|
39
|
+
end
|
40
|
+
|
41
|
+
def join_foreign_key
|
42
|
+
foreign_key
|
31
43
|
end
|
32
44
|
|
33
45
|
private
|
34
46
|
|
35
|
-
def
|
47
|
+
def derive_primary_key
|
36
48
|
klass.primary_key
|
37
49
|
end
|
38
50
|
|
39
|
-
def
|
51
|
+
def derive_foreign_key
|
40
52
|
"#{name.to_s.singularize}_ids"
|
41
53
|
end
|
42
54
|
end
|
@@ -35,6 +35,11 @@ RSpec.describe 'BelongsToMany' do
|
|
35
35
|
expect(subject._reflections).to include('tags')
|
36
36
|
end
|
37
37
|
|
38
|
+
it 'has correct foreign key' do
|
39
|
+
item = subject._reflections['tags']
|
40
|
+
expect(item.foreign_key).to be_eql('tag_ids')
|
41
|
+
end
|
42
|
+
|
38
43
|
it 'loads associated records' do
|
39
44
|
subject.update(tag_ids: [initial.id])
|
40
45
|
expect(subject.tags.to_sql).to be_eql(<<-SQL.squish)
|
@@ -116,6 +121,7 @@ RSpec.describe 'BelongsToMany' do
|
|
116
121
|
|
117
122
|
subject.tags.concat(other.new(name: 'Test'))
|
118
123
|
subject.tags.reload
|
124
|
+
|
119
125
|
expect(subject.tags.size).to be_eql(2)
|
120
126
|
expect(subject.tag_ids.size).to be_eql(2)
|
121
127
|
expect(subject.tags.last.name).to be_eql('Test')
|
data/spec/tests/has_many_spec.rb
CHANGED
@@ -21,6 +21,11 @@ RSpec.describe 'HasMany' do
|
|
21
21
|
expect(subject._reflections).to include('texts')
|
22
22
|
end
|
23
23
|
|
24
|
+
it 'has correct foreign key' do
|
25
|
+
item = subject._reflections['texts']
|
26
|
+
expect(item.foreign_key).to be_eql('user_id')
|
27
|
+
end
|
28
|
+
|
24
29
|
it 'loads associated records' do
|
25
30
|
expect(subject.texts.to_sql).to match(Regexp.new(<<-SQL.squish))
|
26
31
|
SELECT "texts"\\.\\* FROM "texts" WHERE \\(?"texts"\\."user_id" = #{subject.id}\\)?
|
@@ -210,6 +215,11 @@ RSpec.describe 'HasMany' do
|
|
210
215
|
expect(subject._reflections).to include('videos')
|
211
216
|
end
|
212
217
|
|
218
|
+
it 'has correct foreign key' do
|
219
|
+
item = subject._reflections['videos']
|
220
|
+
expect(item.foreign_key).to be_eql('tag_ids')
|
221
|
+
end
|
222
|
+
|
213
223
|
it 'loads associated records' do
|
214
224
|
expect(subject.videos.to_sql).to match(Regexp.new(<<-SQL.squish))
|
215
225
|
SELECT "videos"\\.\\* FROM "videos"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: torque-postgresql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carlos Silva
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-08-
|
11
|
+
date: 2020-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|