torque-postgresql 3.3.0 → 3.3.1

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
  SHA256:
3
- metadata.gz: 2ea2d3fe5e64c12ea8cf8b77ee39a08092b23c5f15802442d5bc92464e248111
4
- data.tar.gz: 17541c4f9cbd22c940b5082be168c0fd62e9736abe77efc79340c73058342db4
3
+ metadata.gz: a8dfe147f5a65e1626923bff250d8c057c73e2fc2c2f2ea6812d67537da32e38
4
+ data.tar.gz: 3205fea618ea9da4d9aeccf3b5d48e3dff72a4a670e181c78c9b9a0b9896cfbf
5
5
  SHA512:
6
- metadata.gz: 39f36019706145c9b6ff5c4e407ee95b7965746d194338a52d1ee3d67876008b62f673996fb41bc0eb257184454ba74cbe3ffc2cb60805bcaf2d366a3a8ad54f
7
- data.tar.gz: 8c77b20d700a66a002eecf369d3d7e2f458d88b280e182e93c4e25bcd8ddbe10f03f77d578d50f13a7234683721ed3cc99d49eac9db7f3de1f0ade9c058f5e53
6
+ metadata.gz: ce88617dcacc292754245e9fff7f7dba62ec672a5b7b4760fff2ff2b28b4b25be7e8413b92bf509f5f9f9c4d0b096559b7ec46de4fdce5294181c363f34367fb
7
+ data.tar.gz: 6c3e8e96edd6b906bd2f0038869f9f15596fad7f22745c5bd63c08b623a55cf97f6185877206dac54a7e93e81ce5d7644bfeb048e7e2db1780bb7cadc7843159
@@ -11,20 +11,20 @@ module Torque
11
11
 
12
12
  def load_records_for_keys(keys, &block)
13
13
  condition = query_condition_for(keys)
14
+ return super if condition.nil?
15
+
14
16
  scope.where(condition).load(&block)
15
17
  end
16
18
 
17
19
  def query_condition_for(keys)
18
- if connected_through_array?
19
- value = scope.cast_for_condition(foreign_column, keys.to_a)
20
- scope.table[association_key_name].overlaps(value)
21
- else
22
- { association_key_name => keys }
23
- end
20
+ return unless connected_through_array?
21
+
22
+ value = scope.cast_for_condition(foreign_column, keys.to_a)
23
+ scope.table[association_key_name].overlaps(value)
24
24
  end
25
25
 
26
26
  def connected_through_array?
27
- foreign_column.array?
27
+ !association_key_name.is_a?(Array) && foreign_column.array?
28
28
  end
29
29
  end
30
30
 
@@ -4,7 +4,7 @@ module Torque
4
4
  module PostgreSQL
5
5
  module SchemaReflection
6
6
  def add_model_name(connection, table_name, model)
7
- cache(connection).add_model_name(table_name, model)
7
+ cache(connection).add_model_name(connection, table_name, model)
8
8
  end
9
9
 
10
10
  def dependencies(connection, table_name)
@@ -97,9 +97,11 @@ module Torque
97
97
  end
98
98
 
99
99
  # A way to manually add models name so it doesn't need the lookup method
100
- def add_model_name(table_name, model)
101
- return unless data_source_exists?(table_name) && model.is_a?(Class)
102
- @data_sources_model_names[table_name] = model
100
+ def add_model_name(*args)
101
+ model, *source = args.reverse
102
+ return unless data_source_exists?(*source.reverse) && model.is_a?(Class)
103
+
104
+ @data_sources_model_names[source.first] = model
103
105
  end
104
106
 
105
107
  # Get all the tables that the given one inherits from
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Torque
4
4
  module PostgreSQL
5
- VERSION = '3.3.0'
5
+ VERSION = '3.3.1'
6
6
  end
7
7
  end
@@ -201,6 +201,47 @@ RSpec.describe 'HasMany' do
201
201
  expect(query.to_sql).to match(/INNER JOIN "texts"/)
202
202
  expect { query.load }.not_to raise_error
203
203
  end
204
+
205
+ context 'with query constraint' do
206
+ let(:activity) { Activity.create! }
207
+
208
+ before do
209
+ skip('Only Rails 7.1 onwards') unless Post.respond_to?(:query_constraints)
210
+
211
+ Post.query_constraints :author_id, :id
212
+ Activity.query_constraints :author_id, :id
213
+ Activity.has_many :posts
214
+ end
215
+
216
+ after do
217
+ Post.instance_variable_set(:@has_query_constraints, false)
218
+ Post.instance_variable_set(:@query_constraints_list, nil)
219
+ Post.instance_variable_set(:@_query_constraints_list, nil)
220
+ Activity.instance_variable_set(:@has_query_constraints, false)
221
+ Activity.instance_variable_set(:@query_constraints_list, nil)
222
+ Activity.instance_variable_set(:@_query_constraints_list, nil)
223
+ end
224
+
225
+ it 'properly preload records' do
226
+ FactoryBot.create_list(:post, 5, activity: activity)
227
+ entries = Activity.all.includes(:posts).load
228
+
229
+ expect(entries.size).to be_eql(1)
230
+ expect(entries.first.posts).to be_loaded
231
+ expect(entries.first.posts.size).to be_eql(5)
232
+ end
233
+
234
+ it 'properly preload records using preloader' do
235
+ FactoryBot.create_list(:post, 5, activity: activity)
236
+ entries = ActiveRecord::Associations::Preloader.new(
237
+ records: Activity.all,
238
+ associations: [:posts],
239
+ ).call.first.records_by_owner
240
+
241
+ expect(entries.size).to be_eql(1)
242
+ expect(entries.values.first.size).to be_eql(5)
243
+ end
244
+ end
204
245
  end
205
246
 
206
247
  context 'on array' do
@@ -125,6 +125,7 @@ RSpec.describe 'TableInheritance' do
125
125
  subject.instance_variable_set(:@inheritance_loaded, true)
126
126
  subject.instance_variable_set(:@inheritance_dependencies, scenario)
127
127
  subject.instance_variable_set(:@inheritance_associations, subject.send(:generate_associations))
128
+ subject.instance_variable_set(:@data_sources_model_names, {})
128
129
  expect(subject.instance_variable_get(:@inheritance_associations)).to eql({
129
130
  'A' => %w(B D C N M),
130
131
  'B' => %w(C N M),
@@ -145,9 +146,9 @@ RSpec.describe 'TableInheritance' do
145
146
  end
146
147
 
147
148
  it 'respect irregular names' do
148
- Torque::PostgreSQL.config.irregular_models = {
149
+ allow(Torque::PostgreSQL.config).to receive(:irregular_models).and_return({
149
150
  'public.posts' => 'ActivityPost',
150
- }
151
+ })
151
152
 
152
153
  subject.send(:prepare_data_sources, *prepare_arguments)
153
154
  list = subject.instance_variable_get(:@data_sources_model_names)
@@ -156,15 +157,24 @@ RSpec.describe 'TableInheritance' do
156
157
  end
157
158
 
158
159
  it 'does not load irregular where the data source is not defined' do
159
- Torque::PostgreSQL.config.irregular_models = {
160
+ allow(Torque::PostgreSQL.config).to receive(:irregular_models).and_return({
160
161
  'products' => 'Product',
161
- }
162
+ })
162
163
 
163
164
  subject.send(:prepare_data_sources, *prepare_arguments)
164
165
  list = subject.instance_variable_get(:@data_sources_model_names)
165
166
  expect(list).to_not have_key('products')
166
167
  end
167
168
 
169
+ it 'works with eager loading' do
170
+ allow(Torque::PostgreSQL.config).to receive(:eager_load).and_return(true)
171
+ ActivityPost.reset_table_name
172
+
173
+ list = subject.instance_variable_get(:@data_sources_model_names)
174
+ expect(list).to have_key('activity_posts')
175
+ expect(list['activity_posts']).to eql(ActivityPost)
176
+ end
177
+
168
178
  {
169
179
  'activities' => 'Activity',
170
180
  'activity_posts' => 'ActivityPost',
@@ -237,7 +247,7 @@ RSpec.describe 'TableInheritance' do
237
247
  expect(other.table_name).to eql('authors')
238
248
  end
239
249
 
240
- it 'respects the table name prefix and sufix defined on parent module' do
250
+ it 'respects the table name prefix and suffix defined on parent module' do
241
251
  mod = Object.const_set('Private', Module.new)
242
252
  mod.define_singleton_method(:table_name_prefix) { 'private.' }
243
253
  mod.define_singleton_method(:table_name_suffix) { '_bundle' }
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: 3.3.0
4
+ version: 3.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-18 00:00:00.000000000 Z
11
+ date: 2024-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -289,6 +289,7 @@ licenses:
289
289
  metadata:
290
290
  source_code_uri: https://github.com/crashtech/torque-postgresql
291
291
  bug_tracker_uri: https://github.com/crashtech/torque-postgresql/issues
292
+ changelog_uri: https://github.com/crashtech/torque-postgresql/releases
292
293
  post_install_message:
293
294
  rdoc_options:
294
295
  - "--title"