rubocop-vendor 0.12.2 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8824e0593901b2f7a580ee710ff54e02d1c25e44b7898fd6a9520645c8ec0280
|
4
|
+
data.tar.gz: 95c6fa1afeae6027be9771322d5406e429548770a9cd388a75dd81a115687580
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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.
|
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-
|
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
|