torque-postgresql 1.1.6 → 1.1.7

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: 302ae61bb97f2666a7c1ada70d7b3ff2a2c5e9dd5230438fe8746ddf5933dcb3
4
- data.tar.gz: 96771655afc7ea3a863c1069783e42858e7e27539e98724eb428f0d8d759ceb5
3
+ metadata.gz: 5ac2d4e3c96cf93edeac515986a6b7ba3ca412712c6eb19a38f09e0b0e5e448a
4
+ data.tar.gz: 133b756202e8e39738d7bda361f3fd9544829ef817b7ac518f972bf175da93c1
5
5
  SHA512:
6
- metadata.gz: 4da7ce39ab52a21dc00a0b1922e02296f96ffb3c4b8fe3c9c8e590a25482a9d04c17ab348e7da00259e5ae4c53865b512740d21e8bdf1f60c069955f4e58350e
7
- data.tar.gz: 5309c59fc60b31a0c4e37056e836951b4f9652104c757b687be9f5c93ddb8629ed0f5ad8de054dd84a5a7f5d4ec9b8527a767c6f6a8776527d91e94eb5cba15d
6
+ metadata.gz: '08a2cb8062f7bdcf6497f4c2b33dc102c6df5d45ed5c82f6c772caeddb02f7a87056b06b13e6c09359b9db52dff46eabcb64f644e17682b1f830fdd988ff44ea'
7
+ data.tar.gz: f419dd80f7a3c9972b0eb469ed63bf5454ede498ee688761f7c99587db8b2d48d06b5b40e4696a1760ea077f750d966a9e67a500b5b2d4b8b0fa3fccc1c0f884
@@ -28,11 +28,11 @@ module Torque
28
28
  end
29
29
 
30
30
  def ids_reader
31
- owner[reflection.active_record_primary_key]
31
+ owner[source_attr]
32
32
  end
33
33
 
34
34
  def ids_writer(new_ids)
35
- column = reflection.active_record_primary_key
35
+ column = source_attr
36
36
  owner.update_column(column, owner[column] = new_ids.presence)
37
37
  @association_scope = nil
38
38
  end
@@ -40,8 +40,8 @@ module Torque
40
40
  def insert_record(record, *)
41
41
  super
42
42
 
43
- attribute = (ids_reader || owner[reflection.active_record_primary_key] = [])
44
- attribute.push(record[klass_fk])
43
+ attribute = (ids_reader || owner[source_attr] = [])
44
+ attribute.push(record[klass_attr])
45
45
  record
46
46
  end
47
47
 
@@ -50,8 +50,8 @@ module Torque
50
50
  end
51
51
 
52
52
  def include?(record)
53
- list = owner[reflection.active_record_primary_key]
54
- ids_reader && ids_reader.include?(record[klass_fk])
53
+ list = owner[source_attr]
54
+ ids_reader && ids_reader.include?(record[klass_attr])
55
55
  end
56
56
 
57
57
  private
@@ -65,7 +65,7 @@ module Torque
65
65
  # When the idea is to nulligy the association, then just set the owner
66
66
  # +primary_key+ as empty
67
67
  def delete_count(method, scope, ids = nil)
68
- ids ||= scope.pluck(klass_fk)
68
+ ids ||= scope.pluck(klass_attr)
69
69
  scope.delete_all if method == :delete_all
70
70
  remove_stash_records(ids)
71
71
  end
@@ -76,13 +76,13 @@ module Torque
76
76
 
77
77
  # Deletes the records according to the <tt>:dependent</tt> option.
78
78
  def delete_records(records, method)
79
- ids = Array.wrap(records).each_with_object(klass_fk).map(&:[])
79
+ ids = Array.wrap(records).each_with_object(klass_attr).map(&:[])
80
80
 
81
81
  if method == :destroy
82
82
  records.each(&:destroy!)
83
83
  remove_stash_records(ids)
84
84
  else
85
- scope = self.scope.where(klass_fk => records)
85
+ scope = self.scope.where(klass_attr => records)
86
86
  delete_count(method, scope, ids)
87
87
  end
88
88
  end
@@ -98,10 +98,14 @@ module Torque
98
98
  ids_writer(ids_reader - Array.wrap(ids))
99
99
  end
100
100
 
101
- def klass_fk
101
+ def source_attr
102
102
  reflection.foreign_key
103
103
  end
104
104
 
105
+ def klass_attr
106
+ reflection.active_record_primary_key
107
+ end
108
+
105
109
  def difference(a, b)
106
110
  a - b
107
111
  end
@@ -5,7 +5,15 @@ unless Torque::PostgreSQL::AR521
5
5
  module PostgreSQL
6
6
  module Associations
7
7
  module Preloader
8
- BelongsToMany = Class.new(::ActiveRecord::Associations::Preloader::HasMany)
8
+ class BelongsToMany < ::ActiveRecord::Associations::Preloader::HasMany
9
+ def association_key_name
10
+ reflection.active_record_primary_key
11
+ end
12
+
13
+ def owner_key_name
14
+ reflection.foreign_key
15
+ end
16
+ end
9
17
 
10
18
  def preloader_for(reflection, owners, *)
11
19
  return AlreadyLoaded \
@@ -25,11 +25,11 @@ module Torque
25
25
  association = association_instance_get(reflection.name)
26
26
  return unless association
27
27
 
28
- klass_fk = reflection.foreign_key
29
- acpk = reflection.active_record_primary_key
28
+ klass_attr = reflection.active_record_primary_key
29
+ source_attr = reflection.foreign_key
30
30
 
31
- records = association.target.each_with_object(klass_fk)
32
- write_attribute(acpk, records.map(&:read_attribute).compact)
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
 
@@ -3,6 +3,8 @@ module Torque
3
3
  include ActiveSupport::Configurable
4
4
 
5
5
  # Stores a version check for compatibility purposes
6
+ AR510 = (ActiveRecord.gem_version >= Gem::Version.new('5.1.0'))
7
+ AR520 = (ActiveRecord.gem_version >= Gem::Version.new('5.2.0'))
6
8
  AR521 = (ActiveRecord.gem_version >= Gem::Version.new('5.2.1'))
7
9
  AR523 = (ActiveRecord.gem_version >= Gem::Version.new('5.2.3'))
8
10
 
@@ -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[:primary_key] || derive_foreign_key.freeze
26
+ @foreign_key ||= options[:foreign_key] || derive_foreign_key.freeze
23
27
  end
24
28
 
25
29
  def association_foreign_key
@@ -27,16 +31,46 @@ module Torque
27
31
  end
28
32
 
29
33
  def active_record_primary_key
30
- @active_record_primary_key ||= options[:foreign_key] || derive_primary_key
34
+ @active_record_primary_key ||= options[:primary_key] || derive_primary_key
35
+ end
36
+
37
+ unless PostgreSQL::AR510
38
+ def join_keys(*)
39
+ JoinKeys.new(join_pk, join_fk)
40
+ end
41
+ end
42
+
43
+ if PostgreSQL::AR520
44
+ def join_primary_key(*)
45
+ active_record_primary_key
46
+ end
47
+
48
+ def join_foreign_key
49
+ foreign_key
50
+ end
51
+ else
52
+ def join_id_for(owner)
53
+ owner[foreign_key]
54
+ end
31
55
  end
32
56
 
33
57
  private
34
58
 
35
- def derive_foreign_key
36
- klass.primary_key
59
+ unless PostgreSQL::AR520
60
+ def join_pk(*)
61
+ active_record_primary_key
62
+ end
63
+
64
+ def join_fk
65
+ foreign_key
66
+ end
37
67
  end
38
68
 
39
69
  def derive_primary_key
70
+ klass.primary_key
71
+ end
72
+
73
+ def derive_foreign_key
40
74
  "#{name.to_s.singularize}_ids"
41
75
  end
42
76
  end
@@ -1,5 +1,5 @@
1
1
  module Torque
2
2
  module PostgreSQL
3
- VERSION = '1.1.6'
3
+ VERSION = '1.1.7'
4
4
  end
5
5
  end
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: 1.1.6
4
+ version: 1.1.7
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-25 00:00:00.000000000 Z
11
+ date: 2020-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails