|
| 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 |
0 commit comments