rubocop-vendor 0.12.2 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f75f2d9359b745fe58cb30a7314ac94d01f6603efd77cfcd8fd653aeea1b115
4
- data.tar.gz: 6207bdf6793f485c5ecb6b7a11d26d10d9344d5130d6449736525009a742c559
3
+ metadata.gz: 8824e0593901b2f7a580ee710ff54e02d1c25e44b7898fd6a9520645c8ec0280
4
+ data.tar.gz: 95c6fa1afeae6027be9771322d5406e429548770a9cd388a75dd81a115687580
5
5
  SHA512:
6
- metadata.gz: 690945dcd9783af17dfec0e4b326ed8ca46612bb45c6a98a5b3aa7b67f8fe9f983c1cd2495e9491e8d3d9294c1eb37bf46cba0d6e2f8752dcd1e5d4be58a8833
7
- data.tar.gz: bd5e907e32893aae0389412ff196eed02a58af2c63c0c30694155ef954ace00ed6cb6781a327af92c51fb725c09023ad39c3639894e868f39641a0d884900f75
6
+ metadata.gz: a7b9ad5f24cacc268c7bbcc04986211bdb5ca23afe41a3b6b4e8b5c2972b19e157d238e9d737de981a5f5e3d946f0465ff8a3f182b88e3c09d7652210819c83d
7
+ data.tar.gz: 85ab88216ac6f67068657f0b149c84c27b6c2f4e6914943b1bf64a951749da339baafcda92fe50078b6d4c8a21bac9a10abc1389bc5ae9e19390609135117287
data/config/default.yml CHANGED
@@ -59,3 +59,8 @@ Vendor/WsSdkPathInjection:
59
59
  Description: 'Avoid using `ws_sdk` with path injection.'
60
60
  Enabled: true
61
61
  VersionAdded: '0.12.0'
62
+
63
+ Vendor/ActiveRecordBaseTransactionUse:
64
+ Description: 'Avoid using ActiveRecord::Base.transaction.'
65
+ Enabled: true
66
+ VersionAdded: '0.13.0'
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vendor
6
+ # Flags uses of ActiveRecord::Base.transaction,
7
+ # as subclasses of ActiveRecord::Base may use a different
8
+ # database connection.
9
+ #
10
+ # This becomes relevant if, for instance, your application
11
+ # defines models or any subclass of ActiveRecord::Base
12
+ # specifying connection configurations, e.g. using `connects_to`.
13
+ #
14
+ # The guarantee that transaction connection matches the
15
+ # model connection is strongest when `MyModelClass.transaction`
16
+ # wraps database operations on instances of MyModelClass only.
17
+ #
18
+ # If multiple model classes are involved in a .transaction
19
+ # call, `.transaction` only needs to be called on one of them,
20
+ # or a common ancestor sharing the same connection
21
+ # if both models share the same underlying connection.
22
+ #
23
+ # If not, a workaround would be to open a transaction on both
24
+ # model classes.
25
+ #
26
+ # @example
27
+ #
28
+ # # bad
29
+ # ActiveRecord::Base.transaction do
30
+ # ... database operations
31
+ # end
32
+ #
33
+ # # good
34
+ # MyModelClass.transaction do
35
+ # ... database operations on instances of MyModelClass
36
+ # end
37
+ #
38
+ # # also good
39
+ # my_model_instance.with_lock do
40
+ # ... database operations on my_model_instance
41
+ # end
42
+ #
43
+ # # good if and only if both models share a database connection
44
+ # MyModelClass.transaction do
45
+ # ... database operations on instances of MyModelClass
46
+ # ... database operations on instances of MyOtherModelClass
47
+ # end
48
+ #
49
+ # # good if and only if ApplicationRecord shares a database
50
+ # # connection with all models involved
51
+ # ApplicationRecord.transaction do
52
+ # ... database operations on instances of MyModelClass
53
+ # ... database operations on instances of MyOtherModelClass
54
+ # end
55
+ #
56
+ # # good if the models do not share a database connection
57
+ # MyModelClass.transaction do
58
+ # MyOtherModelClass.transaction do
59
+ # ... database operations on instances of MyModelClass
60
+ # ... database operations on instances of MyOtherModelClass
61
+ # end
62
+ # end
63
+ #
64
+ class ActiveRecordBaseTransactionUse < Base
65
+ MSG = 'Avoid using `ActiveRecord::Base.transaction, as models inheriting a subclass of ActiveRecord::Base may use a different database connection from ActiveRecord::Base.connection.'
66
+
67
+ # @!method uses_active_record_base?(node)
68
+ def_node_matcher :uses_active_record_base?, <<-PATTERN
69
+ (const (const {nil? cbase} :ActiveRecord) :Base)
70
+ PATTERN
71
+
72
+ def on_send(node)
73
+ receiver_node, method_name = *node
74
+
75
+ return unless uses_active_record_base?(receiver_node) && method_name == :transaction
76
+
77
+ add_offense(node)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -3,6 +3,7 @@
3
3
  module RuboCop
4
4
  end
5
5
 
6
+ require_relative 'vendor/active_record_base_transaction_use'
6
7
  require_relative 'vendor/active_record_connection_execute'
7
8
  require_relative 'vendor/recursive_open_struct_gem'
8
9
  require_relative 'vendor/sidekiq_throttled_gem'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Vendor
5
- VERSION = '0.12.2'
5
+ VERSION = '0.13.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-vendor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.2
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danilo Cabello
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-01-03 00:00:00.000000000 Z
13
+ date: 2024-01-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -96,6 +96,7 @@ files:
96
96
  - README.md
97
97
  - config/default.yml
98
98
  - lib/rubocop-vendor.rb
99
+ - lib/rubocop/cop/vendor/active_record_base_transaction_use.rb
99
100
  - lib/rubocop/cop/vendor/active_record_connection_execute.rb
100
101
  - lib/rubocop/cop/vendor/base.rb
101
102
  - lib/rubocop/cop/vendor/recursive_open_struct_gem.rb