-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f85f20
commit eef1d1f
Showing
13 changed files
with
114 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Naming/FileName: | ||
Exclude: | ||
- lib/rubocop-teamtailor.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Write it! | ||
|
||
Teamtailor/NoHasManyInActiveModelSerializer: | ||
Description: "TODO: Write a description of the cop." | ||
Enabled: true | ||
VersionAdded: "<<next>>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rubocop' | ||
require "rubocop" | ||
|
||
require_relative 'rubocop/teamtailor' | ||
require_relative 'rubocop/teamtailor/version' | ||
require_relative 'rubocop/teamtailor/inject' | ||
require_relative "rubocop/teamtailor" | ||
require_relative "rubocop/teamtailor/version" | ||
require_relative "rubocop/teamtailor/inject" | ||
|
||
RuboCop::Teamtailor::Inject.defaults! | ||
|
||
require_relative 'rubocop/cop/teamtailor_cops' | ||
require_relative "rubocop/cop/teamtailor_cops" |
32 changes: 32 additions & 0 deletions
32
lib/rubocop/cop/teamtailor/no_has_many_in_active_model_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Teamtailor | ||
class NoHasManyInActiveModelSerializer < Base | ||
MSG = "No embedding of records" | ||
|
||
def_node_matcher :no_has_many?, <<~PATTERN | ||
(send nil? :has_many ...) | ||
PATTERN | ||
|
||
def on_class(class_node) | ||
return unless class_node.defined_module_name.end_with?("Serializer") | ||
check_children(class_node) | ||
end | ||
|
||
private | ||
|
||
def check_children(node) | ||
if no_has_many?(node) | ||
add_offense(node) | ||
end | ||
|
||
if node.respond_to?(:children) | ||
node.children.each { |child| check_children(child) } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "teamtailor/no_has_many_in_active_model_serializer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "teamtailor/version" | ||
require "yaml" | ||
|
||
module RuboCop | ||
module Teamtailor | ||
class Error < StandardError; end | ||
# Your code goes here... | ||
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze | ||
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze | ||
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze | ||
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze | ||
CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze | ||
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze | ||
|
||
private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
spec/rubocop/cop/teamtailor/no_has_many_in_active_model_serializer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Teamtailor::NoHasManyInActiveModelSerializer, :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/NoHasManyInActiveModelSerializer: No embedding of records | ||
end | ||
RUBY | ||
end | ||
|
||
it "registers an offense for all `has_many`" do | ||
expect_offense(<<~RUBY) | ||
class FooSerializer < ActiveModel::Serializer | ||
has_many :locations, embed: :ids | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoHasManyInActiveModelSerializer: No embedding of records | ||
has_many :users, embed: :ids | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Teamtailor/NoHasManyInActiveModelSerializer: No embedding of records | ||
end | ||
RUBY | ||
end | ||
|
||
it "does not register an offense `has_one`" 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 | ||
end | ||
RUBY | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Rubocop::Teamtailor do | ||
RSpec.describe RuboCop::Teamtailor do | ||
it "has a version number" do | ||
expect(Rubocop::Teamtailor::VERSION).not_to be nil | ||
end | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
expect(RuboCop::Teamtailor::VERSION).not_to be nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters