couch_potato 1.13.0 → 1.14.0

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: c36fc0534a7564b1fcf3b2a59ca91e3e4e97452c2a74d77e30540eb4b7617211
4
- data.tar.gz: b1583818a743459e8ab86d88350f7ad5c6953c058c3b49c8b90da50610bdd197
3
+ metadata.gz: dd4dede2152745db1f3f0bf7ff3a94dfea3c7eb427beb02292034866dd7dac22
4
+ data.tar.gz: 29ca77512b6bdeebcdd31847deac4957792946519dda4f658f60dd90c9dd83e4
5
5
  SHA512:
6
- metadata.gz: ceffb27b13b2cf9825cd5fcf507bb0e13a6ed9b5caa612fd1f5f27ba98934eb9dae34c4f4dc70835352a9176bc4562e49da12d489b925ebbeb37a3c9eaa964f4
7
- data.tar.gz: c5e01a2d0d83ea11b14a618f218fe3ff66af35be893a4d74644089938b852045c52a8e1d8ecf31b53e70396737f9db0a8849ef0fbacd843c89137ec4d8bfc0e2
6
+ metadata.gz: 21728691cd7eb4b23bd6600899655cc7fa1dbb6a15a6d40c669b53941fab30bf38cb8a95fa9a3e8f85e05d44af116f58cd20a424eedd60d417972db893c07aff
7
+ data.tar.gz: d2d26373afd0bcce487b35703be3bd7bd08cc7e4e108f98e8279bf81c41715b30f5590fdf8080c4b136ec95e356c857b5c50bef123b6971e67294e79cdeb180a
data/.gitignore CHANGED
@@ -7,3 +7,4 @@ pkg
7
7
 
8
8
  .ruby-version
9
9
  .tool-versions
10
+ .vscode/settings.json
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## Changes
2
2
 
3
+ # 1.14.0
4
+
5
+ - add database_collection to models to help avoid n+1 requests
6
+
3
7
  # 1.13.0
4
8
 
5
9
  - add Ruby 3.2 support
@@ -224,6 +224,7 @@ module CouchPotato
224
224
  elsif processed_results.respond_to?(:each)
225
225
  processed_results.each do |document|
226
226
  document.database = self if document.respond_to?(:database=)
227
+ document.database_collection = processed_results if document.respond_to?(:database_collection=)
227
228
  end
228
229
  end
229
230
  processed_results
@@ -297,6 +298,7 @@ module CouchPotato
297
298
  docs = response['rows'].map { |row| row['doc'] }.compact
298
299
  docs.each do |doc|
299
300
  doc.database = self if doc.respond_to?(:database=)
301
+ doc.database_collection = docs if doc.respond_to?(:database_collection=)
300
302
  end
301
303
  end
302
304
 
@@ -27,7 +27,7 @@ module CouchPotato
27
27
  ForbiddenAttributesProtection, Revisions
28
28
  base.send :include, Validation
29
29
  base.class_eval do
30
- attr_accessor :_id, :_rev, :_deleted, :database
30
+ attr_accessor :_id, :_rev, :_deleted, :database, :database_collection
31
31
  alias_method :id, :_id
32
32
  alias_method :id=, :_id=
33
33
 
@@ -1,4 +1,4 @@
1
1
  module CouchPotato
2
- VERSION = '1.13.0'.freeze
2
+ VERSION = '1.14.0'.freeze
3
3
  RSPEC_VERSION = '4.1.0'.freeze
4
4
  end
@@ -73,9 +73,11 @@ module CouchPotato
73
73
  end
74
74
 
75
75
  def docs
76
+ all_docs = rows.map { |r| r['doc'] }
76
77
  rows.map do |row|
77
78
  doc = row['doc']
78
79
  doc.database = database if doc.respond_to?(:database=)
80
+ doc.database_collection = all_docs if doc.respond_to?(:database_collection=)
79
81
  doc
80
82
  end
81
83
  end
data/lib/couch_potato.rb CHANGED
@@ -54,7 +54,7 @@ module CouchPotato
54
54
  def self.use(database_name)
55
55
  resolved_database_name = resolve_database_name(database_name)
56
56
  Thread.current[:__couch_potato_databases] ||= {}
57
- Thread.current[:__couch_potato_databases][resolved_database_name] ||= Database.new(couchrest_database_for_name!(resolved_database_name), name: database_name)
57
+ Thread.current[:__couch_potato_databases][resolved_database_name] ||= Database.new(couchrest_database_for_name(resolved_database_name), name: database_name)
58
58
  end
59
59
 
60
60
  # resolves a name to a database name/full url configured under additional databases
data/spec/create_spec.rb CHANGED
@@ -34,14 +34,24 @@ describe "create" do
34
34
  end
35
35
 
36
36
  describe "multi-db" do
37
- TEST_DBS = ['comment_a', 'comment_b', 'comment_c']
37
+ let(:test_dbs) do
38
+ ['comment_a', 'comment_b', 'comment_c'].map do |name|
39
+ if ENV['DATABASE']
40
+ uri = URI.parse(ENV['DATABASE'])
41
+ uri.path = "/#{name}"
42
+ uri.to_s
43
+ else
44
+ name
45
+ end
46
+ end
47
+ end
38
48
 
39
49
  before(:each) do
40
- TEST_DBS.each { |db_name| CouchPotato.couchrest_database_for_name(db_name).recreate! }
50
+ test_dbs.each { |db_name| CouchPotato.couchrest_database_for_name(db_name).recreate! }
41
51
  end
42
52
 
43
53
  it "should create documents in multiple dbs" do
44
- TEST_DBS.each do |db_name|
54
+ test_dbs.each do |db_name|
45
55
  @comment = Comment.new(:title => 'my_title')
46
56
  CouchPotato.with_database(db_name) do |couch|
47
57
  couch.save_document! @comment
@@ -56,6 +56,16 @@ describe CouchPotato::Database, 'load' do
56
56
  db.load '1'
57
57
  end
58
58
 
59
+ it 'does not set database_collection on the model' do
60
+ user = double('user', 'database_collection=': nil).as_null_object
61
+ allow(DbTestUser).to receive(:new).and_return(user)
62
+ allow(couchrest_db).to receive(:get).and_return DbTestUser.json_create({ JSON.create_id => 'DbTestUser' })
63
+
64
+ db.load '1'
65
+
66
+ expect(user).not_to have_received(:database_collection=).with(db)
67
+ end
68
+
59
69
  it 'should load namespaced models' do
60
70
  allow(couchrest_db).to receive(:get).and_return Parent::Child.json_create({ JSON.create_id => 'Parent::Child' })
61
71
  expect(db.load('1').class).to eq(Parent::Child)
@@ -89,7 +99,7 @@ describe CouchPotato::Database, 'load' do
89
99
 
90
100
  it 'does not write itself to a document that has no database= method' do
91
101
  doc1 = double(:doc1)
92
- allow(doc1).to receive(:respond_to?).with(:database=) { false }
102
+ allow(doc1).to receive(:respond_to?) { false }
93
103
  allow(couchrest_db).to receive(:bulk_load) do
94
104
  { 'rows' => [{ 'doc' => doc1 }] }
95
105
  end
@@ -99,6 +109,24 @@ describe CouchPotato::Database, 'load' do
99
109
  db.load(['1'])
100
110
  end
101
111
 
112
+ it 'sets database_collection on each of the documents' do
113
+ db.load(%w[1 2]).each do |doc|
114
+ expect(doc.database_collection).to eql([doc1, doc2])
115
+ end
116
+ end
117
+
118
+ it 'does not set database_collection on a document that has no database_collection= method' do
119
+ doc1 = double(:doc1)
120
+ allow(doc1).to receive(:respond_to?) { false }
121
+ allow(couchrest_db).to receive(:bulk_load) do
122
+ { 'rows' => [{ 'doc' => doc1 }] }
123
+ end
124
+
125
+ expect(doc1).not_to receive(:database_collection=)
126
+
127
+ db.load(['1'])
128
+ end
129
+
102
130
  it 'returns an empty array when passing an empty array' do
103
131
  expect(db.load([])).to eq([])
104
132
  end
@@ -336,7 +364,7 @@ describe CouchPotato::Database, 'view' do
336
364
  allow(CouchPotato::View::ViewQuery).to receive_messages(new: double('view query', query_view!: { 'rows' => [@result] }))
337
365
  end
338
366
 
339
- it 'initialzes a view query with map/reduce/list/lib funtions' do
367
+ it 'initializes a view query with map/reduce/list/lib funtions' do
340
368
  allow(@spec).to receive_messages(design_document: 'design_doc', view_name: 'my_view',
341
369
  map_function: '<map_code>', reduce_function: '<reduce_code>',
342
370
  lib: { test: '<test_code>' },
@@ -355,7 +383,7 @@ describe CouchPotato::Database, 'view' do
355
383
  @db.view(@spec)
356
384
  end
357
385
 
358
- it 'initialzes a view query with map/reduce/list funtions' do
386
+ it 'initializes a view query with map/reduce/list funtions' do
359
387
  allow(@spec).to receive_messages(design_document: 'design_doc', view_name: 'my_view',
360
388
  map_function: '<map_code>', reduce_function: '<reduce_code>',
361
389
  lib: nil, list_name: 'my_list', list_function: '<list_code>',
@@ -374,7 +402,7 @@ describe CouchPotato::Database, 'view' do
374
402
  @db.view(@spec)
375
403
  end
376
404
 
377
- it 'initialzes a view query with only map/reduce/lib functions' do
405
+ it 'initializes a view query with only map/reduce/lib functions' do
378
406
  allow(@spec).to receive_messages(design_document: 'design_doc', view_name: 'my_view',
379
407
  map_function: '<map_code>', reduce_function: '<reduce_code>',
380
408
  list_name: nil, list_function: nil,
@@ -390,7 +418,7 @@ describe CouchPotato::Database, 'view' do
390
418
  @db.view(@spec)
391
419
  end
392
420
 
393
- it 'initialzes a view query with only map/reduce functions' do
421
+ it 'initializes a view query with only map/reduce functions' do
394
422
  allow(@spec).to receive_messages(design_document: 'design_doc', view_name: 'my_view',
395
423
  map_function: '<map_code>', reduce_function: '<reduce_code>',
396
424
  lib: nil, list_name: nil, list_function: nil)
@@ -405,18 +433,37 @@ describe CouchPotato::Database, 'view' do
405
433
  @db.view(@spec)
406
434
  end
407
435
 
408
- it 'sets itself on returned results that have an accessor' do
436
+ it 'sets itself on returned docs that have an accessor' do
437
+ allow(@result).to receive(:respond_to?).and_return(false)
409
438
  allow(@result).to receive(:respond_to?).with(:database=).and_return(true)
410
439
  expect(@result).to receive(:database=).with(@db)
411
440
  @db.view(@spec)
412
441
  end
413
442
 
414
- it "does not set itself on returned results that don't have an accessor" do
415
- allow(@result).to receive(:respond_to?).with(:database=).and_return(false)
443
+ it "does not set itself on returned docs that don't have an accessor" do
444
+ allow(@result).to receive(:respond_to?).and_return(false)
416
445
  expect(@result).not_to receive(:database=).with(@db)
417
446
  @db.view(@spec)
418
447
  end
419
448
 
449
+ it 'sets the result of the view call on each returned doc' do
450
+ allow(@result).to receive(:respond_to?).and_return(false)
451
+ allow(@result).to receive(:respond_to?).with(:database_collection=).and_return(true)
452
+ allow(@result).to receive(:database_collection=)
453
+
454
+ @db.view(@spec)
455
+
456
+ expect(@result).to have_received(:database_collection=).with([@result])
457
+ end
458
+
459
+ it "does not set the result of the view call on docs that don't have an accessor" do
460
+ allow(@result).to receive(:respond_to?).and_return(false)
461
+
462
+ @db.view(@spec)
463
+
464
+ expect(@result).not_to receive(:database_collection=).with([@result])
465
+ end
466
+
420
467
  it 'does not try to set itself on result sets that are not collections' do
421
468
  expect do
422
469
  allow(@spec).to receive_messages(process_results: 1)
@@ -15,3 +15,30 @@ RSpec.describe CouchPotato::View::FlexViewSpec::Results, '#reduce_count' do
15
15
  expect(result.reduce_count).to eq(0)
16
16
  end
17
17
  end
18
+
19
+ RSpec.describe CouchPotato::View::FlexViewSpec::Results, '#docs' do
20
+ it 'sets the database on each doc' do
21
+ db = double('db')
22
+ doc = double('doc', 'database=': nil)
23
+
24
+ result = CouchPotato::View::FlexViewSpec::Results.new 'rows' => [{ 'doc' => doc }]
25
+ result.database = db
26
+
27
+ result.docs
28
+
29
+ expect(doc).to have_received(:database=).with(db)
30
+ end
31
+
32
+ it 'sets all docs as database_collection on each doc' do
33
+ doc = double('doc', 'database_collection=': nil)
34
+
35
+ result = CouchPotato::View::FlexViewSpec::Results.new 'rows' => [{ 'doc' => doc }]
36
+
37
+ result.docs
38
+
39
+ expect(doc).to have_received(:database_collection=).with([doc])
40
+ end
41
+
42
+ it 'returns the docs' do
43
+ end
44
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch_potato
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.0
4
+ version: 1.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Lang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-18 00:00:00.000000000 Z
11
+ date: 2024-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel