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

Add Configurable option for Rails/TransactionExitStatement #1072

Conversation

marocchino
Copy link
Contributor

@marocchino marocchino commented Aug 17, 2023

Hi.

We differentiate transactions by using methods with different names depending on the target DB.
So I would like to be able to leave the transaction on_lock as the default and add another method name to use it.

like..

Rails/TransactionExitStatement:
  TransactionMethods:
    - transaction
    - on_lock
    - some_custom

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • [ ] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • [ ] If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@marocchino marocchino force-pushed the add-configurable-option-to-transaction-exit-statement branch 2 times, most recently from dcb05f6 to ad71894 Compare August 17, 2023 10:29
@koic
Copy link
Member

koic commented Aug 21, 2023

I understand the use case. However built-in transaction methods and custom methods should not be treated the same I think. So, can you update this PR to mixin the AllowedMethods and AllowedPatterns modules in Rails/TransactionExitStatement to specify allowable custom methods.
e.g. rubocop/rubocop#11019

@marocchino marocchino force-pushed the add-configurable-option-to-transaction-exit-statement branch 2 times, most recently from b28c1ff to 5bf66ea Compare August 21, 2023 14:32
@marocchino
Copy link
Contributor Author

Thanks for review.
As requested, I rewrote it using the AllowedMethods, AllowedPatterns modules.
Did you mean treating built-in transaction methods as separated constants not default of AllowedMethods?
If there is something different from your intention, please let me know.

@@ -0,0 +1 @@
* [#1072](https://github.com/rubocop/rubocop-rails/pull/1072): Add `AllowedMethods` and `AllowedPatterns` for `Rails/TransactionExitStatement`. ([@marocchino][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just my two cents. Can you rename this file to changelog/new_add_allow_methods_and_allow_patterns_to_transaction_exit_statement.md?

Comment on lines 1084 to 1085
AllowedMethods: []
AllowedPatterns: []
Enabled: pending
VersionAdded: '2.14'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tweak it?

Suggested change
AllowedMethods: []
AllowedPatterns: []
Enabled: pending
VersionAdded: '2.14'
Enabled: pending
VersionAdded: '2.14'
AllowedMethods: []
AllowedPatterns: []

@@ -51,11 +51,14 @@ module Rails
# next if user.active?
# end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add examples for the new option?

@marocchino marocchino force-pushed the add-configurable-option-to-transaction-exit-statement branch from 5bf66ea to 9616883 Compare August 22, 2023 21:07
@marocchino
Copy link
Contributor Author

I applied reviews.

@marocchino marocchino requested a review from koic August 23, 2023 09:42
#
# @example AllowedMethods: ["custom_transaction"]
# # bad
# ApplicationRecord.custom_transaction do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be CustomModel instead of extending ApplicationRecord, right?

Suggested change
# ApplicationRecord.custom_transaction do
# CustomModel.custom_transaction do

#
# @example AllowedPatterns: ["_transaction$"]
# # bad
# ApplicationRecord.other_transaction do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@marocchino marocchino force-pushed the add-configurable-option-to-transaction-exit-statement branch from 9616883 to 2d3bc1b Compare August 24, 2023 12:21
@marocchino marocchino requested a review from koic August 24, 2023 12:22
@koic koic merged commit 65f8eef into rubocop:master Aug 26, 2023
4 checks passed
koic added a commit to koic/rubocop-rails that referenced this pull request Aug 26, 2023
…nExitStatement`

Follow up rubocop#1072.

This PR renames `AllowedMethods` to `TransactionMethods` for `Rails/TransactionExitStatement`
because it's a configuration for detecting, not for allowing custom transaction methods.
@koic
Copy link
Member

koic commented Aug 26, 2023

@marocchino I made a mistake and merged this. This is a configuration to detect, not to allow custom transaction methods. So I've opened #1088.

@marocchino marocchino deleted the add-configurable-option-to-transaction-exit-statement branch August 27, 2023 06:48
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