-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Rails/StrictLoadingAssociations cop
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 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,2 @@ | ||
- [#876](https://github.com/rubocop/rubocop-rails/pull/876): Add new | ||
Rails/StrictLoadingAssociations cop. ([@drenmi][]) |
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Declare a `strict_loading` option on `has_many` associations. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# has_many :posts | ||
# | ||
# # good | ||
# has_many :posts, strict_loading: true | ||
# | ||
# # good | ||
# has_many :posts, strict_loading: false | ||
# | ||
class StrictLoadingAssociations < Base | ||
extend TargetRailsVersion | ||
|
||
minimum_target_rails_version 6.1 | ||
|
||
MSG = 'Declare a `strict_loading` option on `has_many` associations.' | ||
|
||
RESTRICT_ON_SEND = %i[has_many].freeze | ||
|
||
# @!method has_many(node) | ||
def_node_matcher :has_many, <<~PATTERN | ||
(send nil? :has_many _ $hash ?) | ||
PATTERN | ||
|
||
def on_send(node) | ||
has_many(node) do |options| | ||
add_offense(node) unless strict_loading_declared?(options.first) | ||
end | ||
end | ||
|
||
private | ||
|
||
def strict_loading_declared?(options) | ||
return false if options.nil? | ||
|
||
options.each_key.any? { |k| k.value.to_s == "strict_loading" } | ||
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
32 changes: 32 additions & 0 deletions
32
spec/rubocop/cop/rails/strict_loading_associations_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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::StrictLoadingAssociations, :config do | ||
context "when using Rails 6.1 or newer", :rails61 do | ||
it 'registers an offense when not declaring `strict_loading`' do | ||
expect_offense(<<~RUBY) | ||
class Author < ApplicationRecord | ||
has_many :posts | ||
^^^^^^^^^^^^^^^ Declare a `strict_loading` option on `has_many` associations. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when declaring `strict_loading`' do | ||
expect_no_offenses(<<~RUBY) | ||
class Author < ApplicationRecord | ||
has_many :posts, strict_loading: true | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when using Rails 6.0 or older", :rails60 do | ||
it 'does not register an offense when not declaring `strict_loading`' do | ||
expect_no_offenses(<<~RUBY) | ||
class Author < ApplicationRecord | ||
has_many :posts | ||
end | ||
RUBY | ||
end | ||
end | ||
end |