-
-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails/ReflectionClassName doesn't match relations that have an optional scope proc or lambda #332
Comments
I believe this will cause the diff --git a/lib/rubocop/cop/rails/reflection_class_name.rb b/lib/rubocop/cop/rails/reflection_class_name.rb
index 28dbfc1dd..336b95fc2 100644
--- a/lib/rubocop/cop/rails/reflection_class_name.rb
+++ b/lib/rubocop/cop/rails/reflection_class_name.rb
@@ -17,7 +17,7 @@ module RuboCop
MSG = 'Use a string value for `class_name`.'
def_node_matcher :association_with_reflection, <<~PATTERN
- (send nil? {:has_many :has_one :belongs_to} _
+ (send nil? {:has_many :has_one :belongs_to} _ _ ?
(hash <$#reflection_class_name ...>)
)
PATTERN It seems to work on my simple test case in the ticket:
And it also worked on the remaining instances in the larger code base at work. I'd be happy to open up a PR with this change and some additional test cases if you'd like. |
Good catch! I'm looking forward to your pull request! |
koic
added a commit
that referenced
this issue
Aug 21, 2020
…ing_scoped_relations [Fix #332] Fix false negative for `Rails/ReflectionClassName`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At work we're upgrading from Rails 5.0 to 5.1 and I just noticed some deprecation warnings:
This was surprising since I thought we had fixed all of these with the
Rails/ReflectionClassName
rubocop. On closer inspection the ones that weren't flagged by theRails/ReflectionClassName
cop all had an optional second parameter that was a proc or lambda that acts as a scope on the relation. See https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to-label-Scopes for more details on what that scope does.Deleting the proc or lambda caused
Rails/ReflectionClassName
to correctly flag these relations.Expected behavior
Expected
Rails/ReflectionClassName
to warn me about relations in my models that had both:String
for theclass_name
optionActual behavior
Rails/ReflectionClassName
didn't warn about these lines.Steps to reproduce the problem
Files:
Gemfile
gem 'rubocop-rails'
Gemfile.lock
.rubocop.yml
app/models/bar.rb
app/models/foo.rb
rubocop
output:I expected the cop to flag both line 3 and line 5 but it doesn't because it won't match against a relation that has a 2nd pararmeter.
RuboCop version
The text was updated successfully, but these errors were encountered: