Skip to content
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 shouldn't flag method definition in Rails #extending method #11018

Closed
inkstak opened this issue Sep 26, 2022 · 2 comments · Fixed by #11019
Closed

Comments

@inkstak
Copy link

inkstak commented Sep 26, 2022

Rails is providing extending method to add methods to an ActiveRecord relation :

https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-extending

scope = Model.all.extending do
  def page(number)
    # pagination code goes here
  end
end

It'll be flagged with the following offense:

W: Lint/NestedMethodDefinition: Method definitions must not be nested. Use lambda instead.
  def page(number) ...
  ^^^^^^^^^^^^^^^^^

Expected behavior

The cop should ignore extending method block.
If this is considered too specific to Rails, the cop should provide a configuration option to ignore given methods.

RuboCop version

$ [bundle exec] rubocop -V
1.35.1 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 3.1.2 arm64-darwin21)
  - rubocop-performance 1.14.3
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.13.1
@inkstak
Copy link
Author

inkstak commented Sep 26, 2022

koic added a commit to koic/rubocop that referenced this issue Sep 26, 2022
…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.
bbatsov pushed a commit that referenced this issue Sep 28, 2022
…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.
@inkstak
Copy link
Author

inkstak commented Sep 28, 2022

Thanks for your diligence and your awesome work !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants