Skip to content

Commit

Permalink
Add cop for has_one as well
Browse files Browse the repository at this point in the history
  • Loading branch information
himynameisjonas committed Nov 12, 2024
1 parent d7fcddb commit 6568869
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 49 deletions.
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Write it!

Teamtailor/NoHasManyInActiveModelSerializer:
Teamtailor/NoEmbeddingsInActiveModelSerializer:
Description: "TODO: Write a description of the cop."
Enabled: true
VersionAdded: "<<next>>"
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module RuboCop
module Cop
module Teamtailor
class NoHasManyInActiveModelSerializer < Base
class NoEmbeddingsInActiveModelSerializer < Base
MSG = "No embedding of records"

def_node_matcher :no_has_many?, <<~PATTERN
(send nil? :has_many ...)
(send nil? {:has_many | :has_one} ...)
PATTERN

def on_class(class_node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/teamtailor_cops.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# frozen_string_literal: true

require_relative "teamtailor/no_has_many_in_active_model_serializer"
require_relative "teamtailor/no_embeddings_in_active_model_serializer"
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Teamtailor::NoEmbeddingsInActiveModelSerializer, :config do
let(:config) { RuboCop::Config.new }

context "when in a serializer" do
it "registers an offense when using `has_many`" do
expect_offense(<<~RUBY)
class FooSerializer < ActiveModel::Serializer
has_many :locations, embed: :ids
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
end
RUBY
end

it "registers an offense when using `has_one`" do
expect_offense(<<~RUBY)
class FooSerializer < ActiveModel::Serializer
has_one :location, embed_in_root: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
end
RUBY
end

it "registers offenses for all `has_many`" do
expect_offense(<<~RUBY)
class FooSerializer < ActiveModel::Serializer
has_many :locations, embed: :ids
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
has_many :users, embed: :ids
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
end
RUBY
end

it "registers offenses for both `has_many` and `has_one`" do
expect_offense(<<~RUBY)
class FooSerializer < ActiveModel::Serializer
has_many :locations, embed: :ids
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
has_one :user, embed: :id
^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoEmbeddingsInActiveModelSerializer: No embedding of records
end
RUBY
end

it "does not register an offence when no has_one/has_many" do
expect_no_offenses(<<~RUBY)
class FooSerializer < ActiveModel::Serializer
attributes :location_ids
end
RUBY
end
end

context "when not in a serializer" do
it "does not register an offense when using has_many" do
expect_no_offenses(<<~RUBY)
class Foo
has_many :locations
has_one :user
end
RUBY
end
end
end

This file was deleted.

0 comments on commit 6568869

Please sign in to comment.