44 let ( :config ) { RuboCop ::Config . new }
55
66 context "when in a serializer" do
7- it "registers an offense when using `has_many`" do
7+ it "registers an offense when using `has_many` with embed " do
88 expect_offense ( <<~RUBY )
99 class FooSerializer < ActiveModel::Serializer
1010 has_many :locations, embed: :ids
@@ -13,7 +13,16 @@ class FooSerializer < ActiveModel::Serializer
1313 RUBY
1414 end
1515
16- it "registers an offense when using `has_one`" do
16+ it "registers an offense when using `has_many` with embed_in_root" do
17+ expect_offense ( <<~RUBY )
18+ class FooSerializer < ActiveModel::Serializer
19+ has_many :locations, embed_in_root: true
20+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
21+ end
22+ RUBY
23+ end
24+
25+ it "registers an offense when using `has_one` with embed_in_root" do
1726 expect_offense ( <<~RUBY )
1827 class FooSerializer < ActiveModel::Serializer
1928 has_one :location, embed_in_root: true
@@ -22,6 +31,15 @@ class FooSerializer < ActiveModel::Serializer
2231 RUBY
2332 end
2433
34+ it "registers an offense when using `has_one` with embed" do
35+ expect_offense ( <<~RUBY )
36+ class FooSerializer < ActiveModel::Serializer
37+ has_one :location, embed: :ids
38+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
39+ end
40+ RUBY
41+ end
42+
2543 it "registers offenses for all `has_many`" do
2644 expect_offense ( <<~RUBY )
2745 class FooSerializer < ActiveModel::Serializer
@@ -33,7 +51,7 @@ class FooSerializer < ActiveModel::Serializer
3351 RUBY
3452 end
3553
36- it "registers offenses for both `has_many` and `has_one`" do
54+ it "registers offenses for both `has_many` and `has_one` with embed " do
3755 expect_offense ( <<~RUBY )
3856 class FooSerializer < ActiveModel::Serializer
3957 has_many :locations, embed: :ids
@@ -51,6 +69,15 @@ class FooSerializer < ActiveModel::Serializer
5169 end
5270 RUBY
5371 end
72+
73+ it "does not register an offence when no has_one/has_many without embedding" do
74+ expect_no_offenses ( <<~RUBY )
75+ class FooSerializer < OjSerializer
76+ has_many :locations, serializer: LocationSerializer
77+ has_one :candidate, serializer: CandidateSerializer
78+ end
79+ RUBY
80+ end
5481 end
5582
5683 context "when not in a serializer" do
0 commit comments