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

Add new `Rails/ValidationsGrouping` cop

Open fatkodima opened this issue 2 years ago • 4 comments

Closes #555.

Autocorrection is more involved, so I suggest to leave it as a future improvement.

Tested on gitlab's codebase: a bunch of offenses, no errors.

fatkodima avatar Mar 22 '22 11:03 fatkodima

@fatkodima A suggestion, to simplify the autocorrection, maybe the config SingleAttributeValidations could be instead an other cop that can be disable if not wanted.

A cop with check and autocorrect

# bad
validates :email, format: { with: /@/ }
validates :email, presence: true
# good
validates :email, presence: true, format: { with: /@/ }

and the other

# bad
validates :name, :bio, presence: true
# good
validates :name, presence: true
validates :bio, presence: true

The combination of both cops will help to follow the style guide.

What do you think?

Bhacaz avatar May 17 '22 12:05 Bhacaz