rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

Cop idea: Macro Style Methods and Callbacks Order for models

Open dbackeus opened this issue 3 years ago • 5 comments

Is your feature request related to a problem?

The Rails Style Guide offers guidance on the ordering of macro methods inside ActiveRecord models.

Macro Style Methods: https://rails.rubystyle.guide/#macro-style-methods Callbacks Order: https://rails.rubystyle.guide/#callbacks-order

There are currently no cops available to enforce this styling.

Describe the solution you'd like

A cop (or two) that verify correct ordering according to the style guide.

Describe alternatives you've considered

There is an unmaintained rubocop extension called rubocop-rails-order_model_macros that adds this functionality but it has not been updated since 2018 and has an open issue that indicates it doesn't work with the latest Rubocop version.

dbackeus avatar Jan 11 '22 13:01 dbackeus

That cop is licensed with MIT, so my guess is that we can just borrow it.

The incompatibility seems to be due to the hard version requirement and may not be actually a blocker to run it with the latest RuboCop. In any case, there's a nice ]official cop migration guide](https://docs.rubocop.org/rubocop/1.24/v1_upgrade_notes.html).

Related issue that this cop could fix: #412

Just for the reference: Layout/ClassStructure cop and its docs.

Would you like to submit a PR?

pirj avatar Jan 11 '22 14:01 pirj

I'll attempt a PR sometime this week 👍

dbackeus avatar Jan 11 '22 15:01 dbackeus

In fact, Layout/ClassStructure cop leaves a lot of problems. https://github.com/rubocop/rubocop/issues?q=is%3Aissue+is%3Aopen+Layout%2FClassStructure

In addition, I'm worried about the conflict between rubocop-rails-order_model_macros gem and Layout/ClassStructure cop, so even whether the PR is opened, it may not be possible to import it into RuboCop Rails at this time. So, there are too many Layout/ClassStructure cop issues left...

Perhaps one approach is to contact the author of rubocop-rails-order_model_macros and maintain the 3rd party gem.

koic avatar Jan 12 '22 03:01 koic

For callbacks order I implemented one a while ago - https://github.com/rubocop/rubocop-rails/blob/master/lib/rubocop/cop/rails/active_record_callbacks_order.rb

There are so many cops in rubocop and rubocop-rails no one remembers it already exists 😄

fatkodima avatar Feb 05 '22 13:02 fatkodima

For the record: The existing issues with Layout/ClassStructure made me reconsider attempting a PR for this 😬

dbackeus avatar Feb 06 '22 14:02 dbackeus