Skip to content

Commit be9154b

Browse files
committed
Update rules to allow without embedding
1 parent b693658 commit be9154b

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

lib/rubocop/cop/teamtailor/no_embeddings_in_active_model_serializer.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@ class NoEmbeddingsInActiveModelSerializer < Base
1010
(send nil? {:has_many | :has_one} ...)
1111
PATTERN
1212

13+
def_node_matcher :no_embed?, <<~PATTERN
14+
(sym {:embed | :embed_in_root})
15+
PATTERN
16+
1317
def on_class(class_node)
1418
return unless class_node.defined_module_name.end_with?("Serializer")
1519
check_children(class_node)
1620
end
1721

1822
private
1923

24+
def is_embedding?(node)
25+
return true if no_embed?(node)
26+
27+
if node.respond_to?(:children)
28+
return node.children.any? { |child| is_embedding?(child) }
29+
end
30+
false
31+
end
32+
2033
def check_children(node)
21-
if no_has_many?(node)
34+
if no_has_many?(node) && is_embedding?(node)
2235
add_offense(node)
2336
end
2437

spec/rubocop/cop/teamtailor/no_embeddings_in_active_model_serializer_spec.rb

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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

Comments
 (0)