File tree Expand file tree Collapse file tree 5 files changed +68
-0
lines changed Expand file tree Collapse file tree 5 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ * [ #1236 ] ( https://github.com/rubocop/rubocop-rails/pull/1236 ) : Add new ` Rails/PrivateTransactionOption ` . ([ @wata727 ] [ ] )
Original file line number Diff line number Diff line change @@ -782,6 +782,12 @@ Rails/Present:
782
782
# Convert usages of `unless blank?` to `if present?`
783
783
UnlessBlank : true
784
784
785
+ Rails/PrivateTransactionOption :
786
+ Description : ' Avoid use of `ActiveRecord::Base.transaction(joinable: _)`.'
787
+ Enabled : pending
788
+ Safe : false
789
+ VersionAdded : ' <<next>>'
790
+
785
791
Rails/RakeEnvironment :
786
792
Description : ' Include `:environment` as a dependency for all Rake tasks.'
787
793
Enabled : true
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks whether `ActiveRecord::Base.transaction(joinable: _)` is used.
7
+ #
8
+ # The `joinable` option is a private API and is not intended to be called
9
+ # from outside Active Record core.
10
+ # https://github.com/rails/rails/issues/39912#issuecomment-665483779
11
+ # https://github.com/rails/rails/issues/46182#issuecomment-1265966330
12
+ #
13
+ # Passing `joinable: false` may cause unexpected behavior such as the
14
+ # `after_commit` callback not firing at the appropriate time.
15
+ #
16
+ # @safety
17
+ # This Cop is unsafe because it cannot accurately identify
18
+ # the `ActiveRecord::Base.transaction` method call.
19
+ #
20
+ # @example
21
+ # # bad
22
+ # ActiveRecord::Base.transaction(requires_new: true, joinable: false)
23
+ #
24
+ # # good
25
+ # ActiveRecord::Base.transaction(requires_new: true)
26
+ #
27
+ class PrivateTransactionOption < Base
28
+ MSG = 'Do not use `ActiveRecord::Base.transaction(joinable: _)`.'
29
+ RESTRICT_ON_SEND = %i[ transaction ] . freeze
30
+
31
+ # @!method match_transaction_with_joinable(node)
32
+ def_node_matcher :match_transaction_with_joinable , <<~PATTERN
33
+ (send _ :transaction (hash <$(pair (sym :joinable) {true false}) ...>))
34
+ PATTERN
35
+
36
+ def on_send ( node )
37
+ match_transaction_with_joinable ( node ) do |option_node |
38
+ add_offense ( option_node )
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
Original file line number Diff line number Diff line change 88
88
require_relative 'rails/pluralization_grammar'
89
89
require_relative 'rails/presence'
90
90
require_relative 'rails/present'
91
+ require_relative 'rails/private_transaction_option'
91
92
require_relative 'rails/rake_environment'
92
93
require_relative 'rails/read_write_attribute'
93
94
require_relative 'rails/redundant_active_record_all_method'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe RuboCop ::Cop ::Rails ::PrivateTransactionOption , :config do
4
+ it 'registers an offense when using `joinable: false`' do
5
+ expect_offense ( <<~RUBY )
6
+ ActiveRecord::Base.transaction(requires_new: true, joinable: false)
7
+ ^^^^^^^^^^^^^^^ Do not use `ActiveRecord::Base.transaction(joinable: _)`.
8
+ RUBY
9
+ end
10
+
11
+ it 'does not register an offense when using only `requires_new: true`' do
12
+ expect_no_offenses ( <<~RUBY )
13
+ ActiveRecord::Base.transaction(requires_new: true)
14
+ RUBY
15
+ end
16
+ end
You can’t perform that action at this time.
0 commit comments