Skip to content

Commit 2b1b67a

Browse files
authored
Merge pull request #1088 from koic/rename_allowed_methods_to_transaction_methods
Rename `AllowedMethods` to `TransactionMethods` for `Rails/TransactionExitStatement`
2 parents 65f8eef + 106ae0b commit 2b1b67a

File tree

5 files changed

+17
-34
lines changed

5 files changed

+17
-34
lines changed

changelog/new_add_allow_methods_and_allow_patterns_to_transaction_exit_statement.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1072](https://github.com/rubocop/rubocop-rails/pull/1072): Add `TransactionMethods` config for `Rails/TransactionExitStatement` to detect custom transaction methods. ([@marocchino][])

config/default.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,7 @@ Rails/TransactionExitStatement:
11041104
- https://github.com/rails/rails/commit/15aa4200e083
11051105
Enabled: pending
11061106
VersionAdded: '2.14'
1107-
AllowedMethods: []
1108-
AllowedPatterns: []
1107+
TransactionMethods: []
11091108

11101109
Rails/UniqBeforePluck:
11111110
Description: 'Prefer the use of uniq or distinct before pluck.'

lib/rubocop/cop/rails/transaction_exit_statement.rb

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module Rails
1313
# error when rollback is desired, and to use `next` when commit is
1414
# desired.
1515
#
16+
# If you are defining custom transaction methods, you can configure it with `TransactionMethods`.
17+
#
1618
# @example
1719
# # bad
1820
# ApplicationRecord.transaction do
@@ -51,27 +53,15 @@ module Rails
5153
# next if user.active?
5254
# end
5355
#
54-
# @example AllowedMethods: ["custom_transaction"]
56+
# @example TransactionMethods: ["custom_transaction"]
5557
# # bad
5658
# CustomModel.custom_transaction do
5759
# return if user.active?
5860
# end
5961
#
60-
# @example AllowedPatterns: ["_transaction$"]
61-
# # bad
62-
# CustomModel.other_transaction do
63-
# return if user.active?
64-
# end
65-
#
6662
class TransactionExitStatement < Base
67-
include AllowedMethods
68-
include AllowedPattern
69-
70-
MSG = <<~MSG.chomp
71-
Exit statement `%<statement>s` is not allowed. Use `raise` (rollback) or `next` (commit).
72-
MSG
73-
74-
TRANSACTION_METHODS = %i[transaction with_lock].freeze
63+
MSG = 'Exit statement `%<statement>s` is not allowed. Use `raise` (rollback) or `next` (commit).'
64+
BUILT_IN_TRANSACTION_METHODS = %i[transaction with_lock].freeze
7565

7666
def_node_search :exit_statements, <<~PATTERN
7767
({return | break | send nil? :throw} ...)
@@ -85,10 +75,6 @@ class TransactionExitStatement < Base
8575
)
8676
PATTERN
8777

88-
def_node_matcher :transaction_method?, <<~PATTERN
89-
(send _ {#allowed_method_name?} ...)
90-
PATTERN
91-
9278
def on_send(node)
9379
return unless in_transaction_block?(node)
9480

@@ -105,7 +91,7 @@ def on_send(node)
10591
private
10692

10793
def in_transaction_block?(node)
108-
return false unless transaction_method?(node)
94+
return false unless transaction_method_name?(node.method_name)
10995
return false unless (parent = node.parent)
11096

11197
parent.block_type? && parent.body
@@ -123,11 +109,15 @@ def statement(statement_node)
123109

124110
def nested_block?(statement_node)
125111
name = statement_node.ancestors.find(&:block_type?).children.first.method_name
126-
!allowed_method_name?(name)
112+
!transaction_method_name?(name)
113+
end
114+
115+
def transaction_method_name?(method_name)
116+
BUILT_IN_TRANSACTION_METHODS.include?(method_name) || transaction_method?(method_name)
127117
end
128118

129-
def allowed_method_name?(name)
130-
TRANSACTION_METHODS.include?(name) || allowed_method?(name) || matches_allowed_pattern?(name)
119+
def transaction_method?(method_name)
120+
cop_config.fetch('TransactionMethods', []).include?(method_name.to_s)
131121
end
132122
end
133123
end

spec/rubocop/cop/rails/transaction_exit_statement_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,9 @@
109109
it_behaves_like 'flags transaction exit statements', :transaction
110110
it_behaves_like 'flags transaction exit statements', :with_lock
111111

112-
context 'when `AllowedMethods: [writable_transaction]`' do
113-
let(:cop_config) { { 'AllowedMethods' => %w[writable_transaction] } }
112+
context 'when `TransactionMethods: [writable_transaction]`' do
113+
let(:cop_config) { { 'TransactionMethods' => %w[writable_transaction] } }
114114

115115
it_behaves_like 'flags transaction exit statements', :writable_transaction
116116
end
117-
118-
context 'when `AllowedPatterns: [transaction]`' do
119-
let(:cop_config) { { 'AllowedPatterns' => %w[transaction] } }
120-
121-
it_behaves_like 'flags transaction exit statements', :foo_transaction
122-
end
123117
end

0 commit comments

Comments
 (0)