File tree Expand file tree Collapse file tree 5 files changed +80
-3
lines changed
spec/integration/associations Expand file tree Collapse file tree 5 files changed +80
-3
lines changed Original file line number Diff line number Diff line change @@ -411,10 +411,15 @@ def default_inverse
411
411
def namespace_hierarchy ( mod )
412
412
parent = Object
413
413
hier = [ parent ]
414
- mod . name . split ( '::' ) . each do |part |
415
- parent = parent . const_get ( part )
416
- hier << parent
414
+
415
+ # name is not present on anonymous modules
416
+ if mod . name
417
+ mod . name . split ( '::' ) . each do |part |
418
+ parent = parent . const_get ( part )
419
+ hier << parent
420
+ end
417
421
end
422
+
418
423
hier . reverse
419
424
end
420
425
Original file line number Diff line number Diff line change 12
12
expect ( child ) . to be_valid
13
13
end
14
14
end
15
+
16
+ context 'when an anonymous class defines a belongs_to association' do
17
+ let ( :klass ) do
18
+ Class . new do
19
+ include Mongoid ::Document
20
+ belongs_to :movie
21
+ end
22
+ end
23
+
24
+ it 'loads the association correctly' do
25
+ expect { klass } . to_not raise_error
26
+ expect { klass . new . movie } . to_not raise_error
27
+ instance = klass . new
28
+ movie = Movie . new
29
+ instance . movie = movie
30
+ expect ( instance . movie ) . to eq movie
31
+ end
32
+ end
15
33
end
Original file line number Diff line number Diff line change 203
203
end
204
204
end
205
205
end
206
+
207
+ context 'when an anonymous class defines an embeds_many association' do
208
+ let ( :klass ) do
209
+ Class . new do
210
+ include Mongoid ::Document
211
+ embeds_many :addresses
212
+ end
213
+ end
214
+
215
+ it 'loads the association correctly' do
216
+ expect { klass } . to_not raise_error
217
+ expect { klass . new . addresses } . to_not raise_error
218
+ expect ( klass . new . addresses . build ) . to be_a Address
219
+ end
220
+ end
206
221
end
Original file line number Diff line number Diff line change 20
20
end
21
21
end
22
22
end
23
+
24
+ context 'when an anonymous class defines an embeds_one association' do
25
+ let ( :klass ) do
26
+ Class . new do
27
+ include Mongoid ::Document
28
+ embeds_one :address
29
+ end
30
+ end
31
+
32
+ it 'loads the association correctly' do
33
+ expect { klass } . to_not raise_error
34
+ expect { klass . new . address } . to_not raise_error
35
+ instance = klass . new
36
+ address = Address . new
37
+ instance . address = address
38
+ expect ( instance . address ) . to eq address
39
+ end
40
+ end
23
41
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'has_and_belongs_to_many associations' do
6
+
7
+ context 'when an anonymous class defines a has_and_belongs_to_many association' do
8
+ let ( :klass ) do
9
+ Class . new do
10
+ include Mongoid ::Document
11
+ has_and_belongs_to_many :movies , inverse_of : nil
12
+ end
13
+ end
14
+
15
+ it 'loads the association correctly' do
16
+ expect { klass } . to_not raise_error
17
+ expect { klass . new . movies } . to_not raise_error
18
+ expect ( klass . new . movies . build ) . to be_a Movie
19
+ end
20
+ end
21
+ end
You can’t perform that action at this time.
0 commit comments