mongoid_includes 1.1.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,11 +20,9 @@ describe Mongoid::Includes::Inclusions do
20
20
  And { result.class == Mongoid::Includes::Inclusions }
21
21
  end
22
22
 
23
- context 'prevents duplicates' do
24
- Given {
25
- inclusions.add(Band.relations['albums'])
26
- }
27
- Then { inclusions.size == 3 }
23
+ it 'prevents duplicates' do
24
+ new_criteria = criteria.includes(:albums)
25
+ expect(new_criteria.inclusions.size).to eq 3
28
26
  end
29
27
  end
30
28
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Includes::Criteria do
4
+
5
+ describe '#includes' do
6
+ Given(:inclusions) { criteria.inclusions }
7
+
8
+ context 'multiple inclusions through polymorphic associations' do
9
+ Given(:pink_floyd) { Band.create!(name: 'Pink Floyd', musician_ids: [nil, '']) }
10
+ Given(:jethro) { Band.create!(name: 'Jethro Tull') }
11
+ Given {
12
+ Artist.create!(name: 'David Gilmour', associated_act: pink_floyd)
13
+ wywh = Album.create!(name: 'Wish You Were Here', release: Date.new(1975), owner: pink_floyd)
14
+ Album.create!(name: 'The Dark Side of the Moon', release: Date.new(1973), owner: pink_floyd)
15
+
16
+ Artist.create!(name: 'Ian Anderson', associated_act: jethro)
17
+ standup = Album.create!(name: 'Stand Up', release: Date.new(1969), owner: jethro)
18
+ Album.create!(name: 'Aqualung', release: Date.new(1971), owner: jethro)
19
+
20
+ Song.create!(name: 'Shine On', album: wywh)
21
+ Song.create!(name: 'We Used to Know', album: standup)
22
+ }
23
+ Given(:criteria) {
24
+ Artist
25
+ .includes(:musicians, from: :associated_act, from_class: Band)
26
+ .includes(:associated_act, with: ->(bands) {
27
+ bands
28
+ .includes(:albums, with: ->(albums) { albums.gt(release: 1970) })
29
+ .includes(:songs, from: :albums, with: ->(songs) { songs })
30
+ })
31
+ }
32
+
33
+ describe ':with inclusions should not be overriden' do
34
+ When(:artists) { expect_query(4) { criteria.entries } } # There are no musicians, so no query should be made.
35
+ Given(:albums) { artists.map(&:associated_act).flat_map(&:albums) }
36
+ Then { artists.size == 2 }
37
+ And {
38
+ expect_no_queries { albums.size == 3 } # gt(release: 1970)
39
+ }
40
+ And {
41
+ expect_no_queries { albums.flat_map(&:songs).size == 1 } # Only "Shine On"
42
+ }
43
+ end
44
+
45
+ describe 'should not replace an includes with an specified modifier with a generic one' do
46
+ Given(:inclusions) { new_criteria.inclusions.to_a }
47
+ When(:new_criteria) { criteria.includes(:musicians, from: :associated_act, from_class: Band) }
48
+ Then { inclusions.size == 2 }
49
+ And { inclusions.first.nested? }
50
+ And { inclusions.last.polymorphic? && inclusions.last.modifier }
51
+ end
52
+
53
+ it 'should fail if a polymorphic association is not disambiguated' do
54
+ expect {
55
+ criteria.includes(:musicians, from: :associated_act)
56
+ }.to raise_error(Mongoid::Includes::Errors::InvalidPolymorphicIncludes)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -306,13 +306,6 @@ describe Mongoid::Includes::Criteria do
306
306
  end
307
307
  end
308
308
 
309
- it 'does not eager load the first document' do
310
- doc = criteria.first
311
- expect_query(1) do
312
- expect(doc.person).to eq(person)
313
- end
314
- end
315
-
316
309
  it 'returns the last document' do
317
310
  expect(document).to eq(post_two)
318
311
  end
@@ -334,7 +327,7 @@ describe Mongoid::Includes::Criteria do
334
327
  end
335
328
 
336
329
  let!(:depeche) do
337
- Band.create!(name: 'Depeche Mode')
330
+ Band.create!(name: 'Depeche Mode', records: [Record.new(name: 'Music for the Masses')])
338
331
  end
339
332
 
340
333
  let!(:tool) do
@@ -354,10 +347,6 @@ describe Mongoid::Includes::Criteria do
354
347
  peep.reload.addresses.includes(:band)
355
348
  end
356
349
 
357
- let(:context) do
358
- criteria.context
359
- end
360
-
361
350
  let!(:document) do
362
351
  criteria.first
363
352
  end
@@ -368,13 +357,6 @@ describe Mongoid::Includes::Criteria do
368
357
  end
369
358
  end
370
359
 
371
- it 'does not eager load the last document' do
372
- doc = criteria.last
373
- expect_query(1) do
374
- expect(doc.band).to eq(tool)
375
- end
376
- end
377
-
378
360
  it 'returns the document' do
379
361
  expect(document).to eq(address_one)
380
362
  end
@@ -386,10 +368,6 @@ describe Mongoid::Includes::Criteria do
386
368
  peep.reload.addresses.includes(:band)
387
369
  end
388
370
 
389
- let(:context) do
390
- criteria.context
391
- end
392
-
393
371
  let!(:document) do
394
372
  criteria.last
395
373
  end
@@ -400,13 +378,6 @@ describe Mongoid::Includes::Criteria do
400
378
  end
401
379
  end
402
380
 
403
- it 'does not eager load the first document' do
404
- doc = criteria.first
405
- expect_query(1) do
406
- expect(doc.band).to eq(depeche)
407
- end
408
- end
409
-
410
381
  it 'returns the document' do
411
382
  expect(document).to eq(address_two)
412
383
  end
@@ -418,10 +389,6 @@ describe Mongoid::Includes::Criteria do
418
389
  peep.reload.addresses.includes(:band)
419
390
  end
420
391
 
421
- let(:context) do
422
- criteria.context
423
- end
424
-
425
392
  let!(:documents) do
426
393
  criteria.to_a
427
394
  end
@@ -500,7 +467,7 @@ describe Mongoid::Includes::Criteria do
500
467
  end
501
468
 
502
469
  before do
503
- expect(new_context).to receive(:eager_load_one).with(person).once.and_call_original
470
+ expect(new_context).to receive(:eager_load).with([person]).once.and_call_original
504
471
  end
505
472
 
506
473
  let!(:from_db) do
@@ -551,7 +518,7 @@ describe Mongoid::Includes::Criteria do
551
518
  end
552
519
 
553
520
  before do
554
- expect(context).to receive(:eager_load_one).with(person).once.and_call_original
521
+ expect(context).to receive(:eager_load).with([person]).once.and_call_original
555
522
  end
556
523
 
557
524
  let!(:from_db) do
@@ -914,7 +881,7 @@ describe Mongoid::Includes::Criteria do
914
881
  end
915
882
 
916
883
  before do
917
- expect(context).to receive(:eager_load).with([ game_one, game_two ]).once.and_call_original
884
+ expect(context).to receive(:preload).twice.and_call_original
918
885
  end
919
886
 
920
887
  let!(:documents) do
@@ -977,7 +944,7 @@ describe Mongoid::Includes::Criteria do
977
944
  end
978
945
 
979
946
  before do
980
- expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
947
+ expect(context).to receive(:preload).twice.and_call_original
981
948
  end
982
949
 
983
950
  let!(:documents) do
@@ -1,7 +1,5 @@
1
- if ENV['CODECLIMATE_REPO_TOKEN']
2
- require 'codeclimate-test-reporter'
3
- CodeClimate::TestReporter.start
4
- end
1
+ require 'simplecov'
2
+ SimpleCov.start
5
3
 
6
4
  require 'rspec/given'
7
5
  require 'pry'
@@ -45,8 +43,11 @@ Mongoid.configure do |config|
45
43
  )
46
44
  end
47
45
 
48
- if defined?(Mongo)
49
- Mongo::Logger.logger = Logger.new("./spec/mongo.log")
46
+ # Logging to the console is useful for debugging queries.
47
+ Mongo::Logger.logger = Logger.new('./spec/mongo.log')
48
+
49
+ def log_mongo_to_console
50
+ Mongo::Logger.logger = Logger.new($stdout)
50
51
  end
51
52
 
52
53
  RSpec.configure do |config|
@@ -1,43 +1,20 @@
1
1
  module Mongoid
2
- class QueryCounter
3
- attr_reader :events
4
-
5
- def initialize
6
- @events = []
7
- end
8
-
9
- def instrument
10
- subscriber = ActiveSupport::Notifications.subscribe('query.moped') do |*args|
11
- @events << ActiveSupport::Notifications::Event.new(*args)
12
- end
13
- yield
14
- ensure
15
- ActiveSupport::Notifications.unsubscribe(subscriber)
16
- end
17
-
18
- def inspect
19
- @events.map { |e| e.payload[:ops].map(&:log_inspect) }.join("\n")
2
+ module SpecHelpers
3
+ def connection_class
4
+ Mongo::Server::ConnectionBase
20
5
  end
21
- end
22
- end
23
6
 
24
- module Mongoid
25
- module SpecHelpers
26
- if defined?(Mongo::Logger.logger)
27
- def expect_query(number)
28
- # There are both start and complete events for each query.
29
- expect(Mongo::Logger.logger).to receive(:debug?).exactly(number * 4).times.and_call_original
30
- yield
31
- end
32
- else
33
- def expect_query(number, &block)
34
- query_counter = Mongoid::QueryCounter.new
35
- query_counter.instrument(&block)
36
- expect(query_counter.events.size).to(eq(number), %[
37
- Expected to receive #{number} queries, it received #{query_counter.events.size}
38
- #{query_counter.inspect}
39
- ])
7
+ def expect_query(number)
8
+ rv = nil
9
+ RSpec::Mocks.with_temporary_scope do
10
+ if number > 0
11
+ expect_any_instance_of(connection_class).to receive(:command_started).exactly(number).times.and_call_original
12
+ else
13
+ expect_any_instance_of(connection_class).not_to receive(:command_started)
14
+ end
15
+ rv = yield
40
16
  end
17
+ rv
41
18
  end
42
19
 
43
20
  def expect_no_queries(&block)
@@ -26,44 +26,8 @@ class Address
26
26
 
27
27
  belongs_to :band
28
28
 
29
- embedded_in :addressable, polymorphic: true do
30
- def extension
31
- "Testing"
32
- end
33
- def doctor?
34
- title == "Dr"
35
- end
36
- end
37
-
38
- scope :without_postcode, ->{ where(postcode: nil) }
39
- scope :rodeo, ->{ where(street: "Rodeo Dr") } do
40
- def mansion?
41
- all? { |address| address.street == "Rodeo Dr" }
42
- end
43
- end
29
+ embedded_in :addressable, polymorphic: true
44
30
 
45
31
  validates_presence_of :street, on: :update
46
32
  validates_format_of :street, with: /\D/, allow_nil: true
47
-
48
- def set_parent=(set = false)
49
- self.parent_title = addressable.title if set
50
- end
51
-
52
- def <=>(other)
53
- street <=> other.street
54
- end
55
-
56
- class << self
57
- def california
58
- where(state: "CA")
59
- end
60
-
61
- def homes
62
- where(address_type: "Home")
63
- end
64
-
65
- def streets
66
- all.map(&:street)
67
- end
68
- end
69
33
  end
@@ -5,4 +5,6 @@ class Artist
5
5
 
6
6
  has_many :albums
7
7
  has_one :musician
8
+
9
+ belongs_to :associated_act, polymorphic: true
8
10
  end
@@ -7,11 +7,7 @@ class Game
7
7
 
8
8
  belongs_to :person, index: true, validate: true
9
9
 
10
- if Mongoid::VERSION >= "6.0.0"
11
- belongs_to :parent, class_name: "Game", foreign_key: "parent-id", optional: true
12
- else
13
- belongs_to :parent, class_name: "Game", foreign_key: "parent-id"
14
- end
10
+ belongs_to :parent, class_name: "Game", foreign_key: "parent-id", optional: true
15
11
 
16
12
  accepts_nested_attributes_for :person
17
13
 
@@ -3,8 +3,4 @@ require './spec/support/models/person'
3
3
  class Musician < Person
4
4
 
5
5
  has_and_belongs_to_many :bands
6
-
7
- def musician?
8
- true
9
- end
10
6
  end
@@ -47,26 +47,11 @@ class Person
47
47
 
48
48
  attr_reader :rescored
49
49
 
50
- embeds_many :addresses, as: :addressable, validate: false do
51
- def extension
52
- "Testing"
53
- end
54
- def find_by_street(street)
55
- where(street: street).first
56
- end
57
- end
50
+ embeds_many :addresses, as: :addressable, validate: false
58
51
 
59
- has_one :game, dependent: :destroy, validate: false do
60
- def extension
61
- "Testing"
62
- end
63
- end
52
+ has_one :game, dependent: :destroy, validate: false
64
53
 
65
- has_many :posts, dependent: :delete, validate: false do
66
- def extension
67
- "Testing"
68
- end
69
- end
54
+ has_many :posts, dependent: :delete_all, validate: false
70
55
 
71
56
  has_and_belongs_to_many :preferences, index: true, dependent: :nullify, validate: false
72
57
  end
@@ -1,3 +1,5 @@
1
+ require_relative './person'
2
+
1
3
  class Post
2
4
  include Mongoid::Document
3
5
  include Mongoid::Attributes::Dynamic
@@ -3,48 +3,5 @@ class Record
3
3
 
4
4
  field :name, type: String
5
5
 
6
- field :before_create_called, type: Mongoid::Boolean, default: false
7
- field :before_save_called, type: Mongoid::Boolean, default: false
8
- field :before_update_called, type: Mongoid::Boolean, default: false
9
- field :before_validation_called, type: Mongoid::Boolean, default: false
10
- field :before_destroy_called, type: Mongoid::Boolean, default: false
11
-
12
6
  embedded_in :band
13
-
14
- before_create :before_create_stub
15
- before_save :before_save_stub
16
- before_update :before_update_stub
17
- before_validation :before_validation_stub
18
- before_destroy :before_destroy_stub
19
-
20
- after_destroy :access_band
21
-
22
- def before_create_stub
23
- self.before_create_called = true
24
- end
25
-
26
- def before_save_stub
27
- self.before_save_called = true
28
- end
29
-
30
- def before_update_stub
31
- self.before_update_called = true
32
- end
33
-
34
- def before_validation_stub
35
- self.before_validation_called = true
36
- end
37
-
38
- def before_destroy_stub
39
- self.before_destroy_called = true
40
- end
41
-
42
- def access_band
43
- band.name
44
- end
45
-
46
- def dont_call_me_twice
47
- end
48
-
49
- validate { dont_call_me_twice }
50
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_includes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-17 00:00:00.000000000 Z
11
+ date: 2020-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: 7.0.10
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: 7.0.0
22
+ version: 8.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 4.0.0
29
+ version: 7.0.10
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: 7.0.0
32
+ version: 8.0.0
33
33
  description: Mongoid::Includes improves eager loading in Mongoid, supporting polymorphic
34
34
  associations, and up to two-levels of eager loading.
35
35
  email:
@@ -44,6 +44,7 @@ files:
44
44
  - Rakefile
45
45
  - lib/config/locales/en.yml
46
46
  - lib/mongoid/includes.rb
47
+ - lib/mongoid/includes/association/eager.rb
47
48
  - lib/mongoid/includes/criteria.rb
48
49
  - lib/mongoid/includes/eager_load.rb
49
50
  - lib/mongoid/includes/errors.rb
@@ -51,7 +52,6 @@ files:
51
52
  - lib/mongoid/includes/errors/invalid_polymorphic_includes.rb
52
53
  - lib/mongoid/includes/inclusion.rb
53
54
  - lib/mongoid/includes/inclusions.rb
54
- - lib/mongoid/includes/relations/eager.rb
55
55
  - lib/mongoid/includes/version.rb
56
56
  - lib/mongoid_includes.rb
57
57
  - spec/mongo.log
@@ -60,6 +60,7 @@ files:
60
60
  - spec/mongoid/includes/errors/invalid_polymorphic_includes_spec.rb
61
61
  - spec/mongoid/includes/inclusions_spec.rb
62
62
  - spec/mongoid/includes/nested_inclusions_spec.rb
63
+ - spec/mongoid/includes/polymorphic_includes_spec.rb
63
64
  - spec/mongoid/includes/simple_inclusions_spec.rb
64
65
  - spec/spec_helper.rb
65
66
  - spec/support/config/mongoid.yml
@@ -80,7 +81,7 @@ homepage: https://github.com/ElMassimo/mongoid_includes
80
81
  licenses:
81
82
  - MIT
82
83
  metadata: {}
83
- post_install_message:
84
+ post_install_message:
84
85
  rdoc_options:
85
86
  - "--charset=UTF-8"
86
87
  require_paths:
@@ -96,32 +97,31 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
99
  requirements: []
99
- rubyforge_project:
100
- rubygems_version: 2.5.1
101
- signing_key:
100
+ rubygems_version: 3.1.2
101
+ signing_key:
102
102
  specification_version: 4
103
103
  summary: Improved eager loading support for Mongoid.
104
104
  test_files:
105
- - spec/mongo.log
106
- - spec/mongoid/includes/criteria_spec.rb
107
- - spec/mongoid/includes/errors/invalid_includes_spec.rb
108
- - spec/mongoid/includes/errors/invalid_polymorphic_includes_spec.rb
109
105
  - spec/mongoid/includes/inclusions_spec.rb
110
- - spec/mongoid/includes/nested_inclusions_spec.rb
106
+ - spec/mongoid/includes/polymorphic_includes_spec.rb
111
107
  - spec/mongoid/includes/simple_inclusions_spec.rb
108
+ - spec/mongoid/includes/nested_inclusions_spec.rb
109
+ - spec/mongoid/includes/criteria_spec.rb
110
+ - spec/mongoid/includes/errors/invalid_polymorphic_includes_spec.rb
111
+ - spec/mongoid/includes/errors/invalid_includes_spec.rb
112
112
  - spec/spec_helper.rb
113
- - spec/support/config/mongoid.yml
114
113
  - spec/support/helpers.rb
115
- - spec/support/models/address.rb
114
+ - spec/support/config/mongoid.yml
115
+ - spec/support/models/song.rb
116
+ - spec/support/models/node.rb
117
+ - spec/support/models/musician.rb
118
+ - spec/support/models/record.rb
116
119
  - spec/support/models/album.rb
117
120
  - spec/support/models/artist.rb
118
- - spec/support/models/band.rb
119
121
  - spec/support/models/game.rb
120
- - spec/support/models/musician.rb
121
- - spec/support/models/node.rb
122
122
  - spec/support/models/person.rb
123
+ - spec/support/models/band.rb
123
124
  - spec/support/models/post.rb
125
+ - spec/support/models/address.rb
124
126
  - spec/support/models/preference.rb
125
- - spec/support/models/record.rb
126
- - spec/support/models/song.rb
127
- has_rdoc:
127
+ - spec/mongo.log