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

Propose a new cop BangPersistence #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aliismayilov
Copy link

This cop enforces the usage of bang versions of the persistence methods during the rspec setup blocks. The problem with non-bang versions is that it only returns true/false in case of success/failure:

before do
  post.update(non_null_column: nil)
  # => false
end

The above before block fails to setup the desired state, but the developer won't get any feedback about this. Instead, the following version:

before do
  post.update!(non_null_column: nil)
  # => raises ActiveRecord::RecordInvalid
end

This cop has been useful in our company codebase. I'm open to improve it and/or come up with better naming :)


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

@aliismayilov aliismayilov requested a review from a team as a code owner February 17, 2025 14:35
@pirj
Copy link
Member

pirj commented Feb 17, 2025

Have you considered making it a more generic cop, targeting rubocop-rails?
It’s not only spec code that suffers from this issue.

@aliismayilov
Copy link
Author

Have you considered making it a more generic cop, targeting rubocop-rails? It’s not only spec code that suffers from this issue.

Can you please elaborate?

I can see it being useful in cases where the return value of the persistence call isn't evaluated:

def save_all
  post.update(title: "invalid")
  comments.save
end

In the above case post.update should probably be better handled, but it won't be easy to cover block calls:

save_all do
  post.update(title: "invalid")
end

It's probably hard to tell if the above is properly handled or not, while the code structure is exactly the same as before 🤷

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

Successfully merging this pull request may close these issues.

2 participants