Skip to content

Commit b62ca29

Browse files
committed
Fix bug where ignores the option if the association is polymorphic.
1 parent 4666c6d commit b62ca29

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Mongoid::Includes 2.1.0 (2017-07-03) ##
2+
3+
* Fix bug where `includes` ignores the `:with` option if the association is polymorphic. Thanks @nickcherry for the bug report!
4+
15
## Mongoid::Includes 2.0.0 (2017-01-10) ##
26

37
* Support Mongoid 6.0.1, fixes related to [changes in Mongoid internals](https://github.com/mongodb/mongoid/pull/4326). Thanks @mityakoval and @forumd for the bug reports!

lib/mongoid/includes/inclusion.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def modifier
4343
@modifier ||= @options[:with]
4444
end
4545

46-
# Public: Preloads the documents for the relation. Users a custom block
46+
# Public: Preloads the documents for the relation. Uses a custom block
4747
# if one was provided, or fetches them using the class and the foreign key.
4848
def load_documents_for(foreign_key, foreign_key_values)
4949
if loader
@@ -63,7 +63,7 @@ def for_class_name(class_name)
6363
self[:class_name] = @class_name = class_name
6464
self[:polymorphic], self[:as], @polymorphic, @klass = nil
6565
self
66-
}
66+
}, with: @modifier, loader: @loader
6767
end
6868
end
6969
end

lib/mongoid/includes/inclusions.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ def initialize(object = [])
1616
# Returns the added inclusion.
1717
def push(metadata, options = {})
1818
metadata = Inclusion.new(metadata, options) unless metadata.is_a?(Inclusion)
19+
20+
# Ensure that an inclusion with a specified loader or modifier replaces
21+
# a previously specified loader.
22+
delete(metadata) if metadata.loader || metadata.modifier
23+
24+
# Internally it's a set so it won't add it twice.
1925
add(metadata)
26+
2027
metadata
2128
end
2229

lib/mongoid/includes/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module Mongoid
55
module Includes
66

77
# Public: This library will attempt to follow semantic versioning (whatever that's supposed to be).
8-
VERSION = '2.0.0'
8+
VERSION = '2.1.0'
99
end
1010
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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') }
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(5) { criteria.entries } }
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+
end
53+
end
54+
end

spec/support/models/artist.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ class Artist
55

66
has_many :albums
77
has_one :musician
8+
9+
belongs_to :associated_act, polymorphic: true
810
end

0 commit comments

Comments
 (0)