|
59 | 59 |
|
60 | 60 | context 'eager loading polymorphic belongs_to associations with multiple concrete types' do |
61 | 61 | before(:context) do |
62 | | - class PolyRelated |
| 62 | + class Main |
63 | 63 | include Mongoid::Document |
64 | | - store_in collection: :poly_relateds |
65 | | - end |
66 | | - |
67 | | - class PolyMain |
68 | | - include Mongoid::Document |
69 | | - store_in collection: :poly_mains |
70 | | - |
71 | 64 | belongs_to :related, polymorphic: true, optional: true |
72 | 65 | end |
73 | 66 |
|
74 | | - class PolyTwo < PolyRelated |
75 | | - has_one :parent, class_name: 'PolyMain', as: :related, inverse_of: :related |
| 67 | + class Related |
| 68 | + include Mongoid::Document |
| 69 | + has_one :parent, as: :related |
76 | 70 | end |
77 | 71 |
|
78 | | - class PolyThree < PolyRelated |
79 | | - has_one :parent, class_name: 'PolyMain', as: :related, inverse_of: :related |
80 | | - end |
| 72 | + class Two < Related; end |
| 73 | + class Three < Related; end |
81 | 74 | end |
82 | 75 |
|
83 | 76 | after(:context) do |
84 | | - %i[PolyMain PolyTwo PolyThree PolyRelated].each do |const| |
| 77 | + %i[Main Related Two Three].each do |const| |
85 | 78 | Object.send(:remove_const, const) if Object.const_defined?(const, false) |
86 | 79 | end |
87 | 80 | end |
88 | 81 |
|
89 | 82 | it 'loads the related documents for each concrete type without raising' do |
90 | | - PolyMain.create!(related: PolyTwo.create!) |
91 | | - PolyMain.create!(related: PolyThree.create!) |
| 83 | + Main.create!(related: Two.create!) |
| 84 | + Main.create!(related: Three.create!) |
92 | 85 |
|
93 | 86 | loaded = nil |
94 | | - expect { |
95 | | - loaded = PolyMain.includes(:related).entries |
96 | | - }.not_to raise_error |
97 | | - |
98 | | - expect(loaded.map { |doc| doc.related.class }).to match_array([PolyTwo, PolyThree]) |
99 | | - |
100 | | - expect { |
101 | | - PolyMain.last.related.id |
102 | | - }.not_to raise_error |
| 87 | + expect { loaded = Main.includes(:related).entries }.not_to raise_error |
| 88 | + expect(loaded.map { |doc| doc.related.class }).to match_array([Two, Three]) |
| 89 | + expect { Main.last.related.id }.not_to raise_error |
103 | 90 | end |
104 | 91 | end |
105 | 92 |
|
@@ -131,15 +118,11 @@ class Main |
131 | 118 |
|
132 | 119 | class Related |
133 | 120 | include Mongoid::Document |
134 | | - end |
135 | | -
|
136 | | - class Two < Related |
137 | 121 | has_one :parent, as: :related |
138 | 122 | end |
139 | 123 |
|
140 | | - class Three < Related |
141 | | - has_one :parent, as: :related |
142 | | - end |
| 124 | + class Two < Related; end |
| 125 | + class Three < Related; end |
143 | 126 | RUBY |
144 | 127 |
|
145 | 128 | init_script = base_script + <<~RUBY |
|
0 commit comments