torque-postgresql 1.1.2 → 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
- SHA1:
3
- metadata.gz: 261ea2ffc412c3f06c841be868141f631fa9f983
4
- data.tar.gz: e5f58518bcc173eaa8be19ea24e25f086546e78c
2
+ SHA256:
3
+ metadata.gz: 5ac2d4e3c96cf93edeac515986a6b7ba3ca412712c6eb19a38f09e0b0e5e448a
4
+ data.tar.gz: 133b756202e8e39738d7bda361f3fd9544829ef817b7ac518f972bf175da93c1
5
5
  SHA512:
6
- metadata.gz: 2742264fd62ee56eea953349bd95dc441d37948e2ebce6e8acb69ec200aabab5ca95e016ad7654b6626fc4bac465797abf88e3bd259f5149f2e3d60f74cae566
7
- data.tar.gz: 879d2eb8e794009a63f1d69ea85b967a126709b242cee0fd9ccac6d1ad4734e8ca62aa69413c708b47c692557e1b935ed759562caebae56bc48e85afe3269bc9
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
 
@@ -31,11 +31,12 @@ module Torque
31
31
  end
32
32
 
33
33
  # Fast access to statement build
34
- def build(statement, base, options = nil, bound_attributes = [])
34
+ def build(statement, base, options = nil, bound_attributes = [], join_sources = [])
35
35
  klass = instantiate(statement, base, options)
36
36
  result = klass.build(base)
37
37
 
38
38
  bound_attributes.concat(klass.bound_attributes)
39
+ join_sources.concat(klass.join_sources)
39
40
  result
40
41
  end
41
42
 
@@ -105,7 +106,7 @@ module Torque
105
106
  delegate :config, :table, :table_name, :relation, :configure, :relation_query?,
106
107
  to: :class
107
108
 
108
- attr_reader :bound_attributes
109
+ attr_reader :bound_attributes, :join_sources
109
110
 
110
111
  # Start a new auxiliary statement giving extra options
111
112
  def initialize(*args)
@@ -117,15 +118,21 @@ module Torque
117
118
  @where = options.fetch(:where, {})
118
119
  @select = options.fetch(:select, {})
119
120
  @join_type = options.fetch(:join_type, nil)
121
+
120
122
  @bound_attributes = []
123
+ @join_sources = []
121
124
  end
122
125
 
123
126
  # Build the statement on the given arel and return the WITH statement
124
127
  def build(base)
128
+ @bound_attributes.clear
129
+ @join_sources.clear
130
+
131
+ # Prepare all the data for the statement
125
132
  prepare(base)
126
133
 
127
134
  # Add the join condition to the list
128
- base.joins_values += [build_join(base)]
135
+ @join_sources << build_join(base)
129
136
 
130
137
  # Return the statement with its dependencies
131
138
  [@dependencies, ::Arel::Nodes::As.new(table, build_query(base))]
@@ -288,7 +295,7 @@ module Torque
288
295
  cte.is_a?(dependent_klass)
289
296
  end
290
297
 
291
- AuxiliaryStatement.build(dependent, base, options, bound_attributes)
298
+ AuxiliaryStatement.build(dependent, base, options, bound_attributes, join_sources)
292
299
  end
293
300
  end
294
301
 
@@ -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
@@ -47,16 +47,16 @@ module Torque
47
47
 
48
48
  # Hook arel build to add the distinct on clause
49
49
  def build_arel(*)
50
- subqueries = build_auxiliary_statements
51
- return super if subqueries.nil?
52
- super.with(subqueries)
50
+ arel = super
51
+ subqueries = build_auxiliary_statements(arel)
52
+ subqueries.nil? ? arel : arel.with(subqueries)
53
53
  end
54
54
 
55
55
  # Build all necessary data for auxiliary statements
56
- def build_auxiliary_statements
57
- return unless self.auxiliary_statements_values.present?
58
- self.auxiliary_statements_values.map do |klass|
59
- klass.build(self)
56
+ def build_auxiliary_statements(arel)
57
+ return unless auxiliary_statements_values.present?
58
+ auxiliary_statements_values.map do |klass|
59
+ klass.build(self).tap { arel.join_sources.concat(klass.join_sources) }
60
60
  end
61
61
  end
62
62
 
@@ -31,21 +31,21 @@ module Torque
31
31
  if inherited_tables.present?
32
32
  stream.puts " # These are tables that has inheritance"
33
33
  inherited_tables.each do |table_name, inherits|
34
- unless ignored?(table_name)
35
- sub_stream = StringIO.new
36
- table(table_name, sub_stream)
37
-
38
- # Add the inherits setting
39
- sub_stream.rewind
40
- inherits.map!(&:to_sym)
41
- inherits = inherits.first if inherits.size === 1
42
- inherits = ", inherits: #{inherits.inspect} do |t|"
43
- table_dump = sub_stream.read.gsub(/ do \|t\|$/, inherits)
44
-
45
- # Ensure bodyless definitions
46
- table_dump.gsub!(/do \|t\|\n end/, '')
47
- stream.print table_dump
48
- end
34
+ next if ignored?(table_name)
35
+
36
+ sub_stream = StringIO.new
37
+ table(table_name, sub_stream)
38
+
39
+ # Add the inherits setting
40
+ sub_stream.rewind
41
+ inherits.map!(&:to_sym)
42
+ inherits = inherits.first if inherits.size === 1
43
+ inherits = ", inherits: #{inherits.inspect} do |t|"
44
+ table_dump = sub_stream.read.gsub(/ do \|t\|$/, inherits)
45
+
46
+ # Ensure bodyless definitions
47
+ table_dump.gsub!(/do \|t\|\n end/, '')
48
+ stream.print table_dump
49
49
  end
50
50
  end
51
51
 
@@ -55,6 +55,9 @@ module Torque
55
55
  foreign_keys(tbl, stream) unless ignored?(tbl)
56
56
  end
57
57
  end
58
+
59
+ # Scenic integration
60
+ views(stream) if defined?(::Scenic)
58
61
  end
59
62
 
60
63
  # Dump user defined types like enum
@@ -63,7 +66,7 @@ module Torque
63
66
  return unless types.any?
64
67
 
65
68
  stream.puts " # These are user-defined types used on this database"
66
- types.each { |name, type| send(type.to_sym, name, stream) }
69
+ types.sort_by(&:first).each { |name, type| send(type.to_sym, name, stream) }
67
70
  stream.puts
68
71
  rescue => e
69
72
  stream.puts "# Could not dump user-defined types because of following #{e.class}"
@@ -1,5 +1,5 @@
1
1
  module Torque
2
2
  module PostgreSQL
3
- VERSION = '1.1.2'
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.2
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: 2019-12-23 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
@@ -260,8 +260,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
260
260
  - !ruby/object:Gem::Version
261
261
  version: 1.8.11
262
262
  requirements: []
263
- rubyforge_project:
264
- rubygems_version: 2.6.14
263
+ rubygems_version: 3.1.4
265
264
  signing_key:
266
265
  specification_version: 4
267
266
  summary: ActiveRecord extension to access PostgreSQL advanced resources