-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Lint/NestedMethodDefinition does not know about ActiveRecord association blocks #8860
Comments
I get the problem, but it's still a problem as you're nesting a definition results in some misleading assumption about it's scope. The fact that you can do something doesn't mean it's a good idea. :-) If you example the "nested" method definition will actually be pulled to the containing class definition. |
Will it really be pulled to the containing class definition? I'm not sure I understand. If that is the case, ActiveSupport Concerns would have the same issue, right? This would mean that foo is really defined as an instance method in the module in the following example?
Or is this just an issue with defs in blocks in a class? I'll note that I don't get a cop complaining about Concerns. But equally, my original example with the Rails association does not appear to result in the method being defined at level of the outer class, but correctly as a method that can be called from the association. |
I think we should have a config for methods that are known to execute the block within a Right now, that list exists but is hardcoded to |
…int/NestedMethodDefinition` Fixes rubocop#11018 and rubocop#8860. This PR adds `AllowedMethods` and `AllowedPatterns` for `Lint/NestedMethodDefinition`. For example, `has_many`, `extending`, and other Rails APIs can be specified in RuboCop Rails config. `class_eval`, `instance_eval`, `module_eval`, `class_exec`, `instance_exec`, and `module_exec` leave hard-coded for core methods.
…tedMethodDefinition` Fixes #11018 and #8860. This PR adds `AllowedMethods` and `AllowedPatterns` for `Lint/NestedMethodDefinition`. For example, `has_many`, `extending`, and other Rails APIs can be specified in RuboCop Rails config. `class_eval`, `instance_eval`, `module_eval`, `class_exec`, `instance_exec`, and `module_exec` leave hard-coded for core methods.
A standard ActiveRecord association can accept a block, such as this example. If the association is applied to a class dynamically, within a method (rather than at the top level on class creation),
Lint/NestedMethodDefinition
is reported.( adapted from https://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many )
I would expect this not to fail. Although I have not tested, I expect all ActiveRecord associations fail in the same way.
Expected behavior
The above should pass.
This appears to be similar to #4185 and possibly others. Is it in fact correct that anything with the following structure should pass?:
Actual behavior
The above fails
RuboCop version
0.92.0 (using Parser 2.7.1.5, rubocop-ast 0.7.1, running on ruby 2.6.6 x86_64-linux)
The text was updated successfully, but these errors were encountered: