order_cop 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +1 -1
- data/lib/order_cop/version.rb +1 -1
- data/lib/order_cop.rb +47 -56
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9db33624c2eb202e4a077391905aff8c3d2719ba36743ef86c169a3ebb44eab
|
4
|
+
data.tar.gz: 499e5fa4966beccfcfe5f8dde8db48bff9f9d364bddcc2618b70240e25f76cca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f883d3cd5e17aa8d612542585fe490bbcce29bfd239a039a738ebb22bcc55bf01ee07bcdd816ed650169cbc6b5c8ceeed5cb84530078c2571b8c7275bb502e66
|
7
|
+
data.tar.gz: 9b4a1e48a5de4e49068cb9223dbab2e224453e81a26577bc2b7877800d06a985f0012ae1cacc5800db5e8f7b58d83a0a44b33142f60f776afa503da64a4728fa
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/lib/order_cop/version.rb
CHANGED
data/lib/order_cop.rb
CHANGED
@@ -11,6 +11,42 @@ require "zeitwerk"
|
|
11
11
|
loader = Zeitwerk::Loader.for_gem
|
12
12
|
loader.setup
|
13
13
|
module OrderCop
|
14
|
+
class OrderCopConfig
|
15
|
+
def initialize
|
16
|
+
@enabled = true
|
17
|
+
@raise = true
|
18
|
+
@debug = false
|
19
|
+
@rails_logger = false
|
20
|
+
@whitelist_methods = %i[sum any? none? inspect method_missing not load_target reindex attachment attachments attributes_table with_current_arbre_element]
|
21
|
+
end
|
22
|
+
attr_accessor :enabled, :raise, :debug, :rails_logger, :whitelist_methods
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.config(**options, &block)
|
26
|
+
@config ||= OrderCopConfig.new
|
27
|
+
options.each do |key, value|
|
28
|
+
@config.send("#{key}=", value)
|
29
|
+
end
|
30
|
+
yield @config if block
|
31
|
+
@config
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.disable(&block)
|
35
|
+
old_enabled = @config.enabled
|
36
|
+
@config.enabled = false
|
37
|
+
yield
|
38
|
+
ensure
|
39
|
+
@config.enabled = old_enabled
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.disabled?
|
43
|
+
!@config.enabled
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.enabled?
|
47
|
+
@config.enabled
|
48
|
+
end
|
49
|
+
|
14
50
|
module OrderCopMixin
|
15
51
|
# patch all methods which iterate over the collection and return several records
|
16
52
|
# we don't patch `first` and `last`, take or others because they return a single record)
|
@@ -59,9 +95,9 @@ module OrderCop
|
|
59
95
|
lbinding = binding.of_caller(i)
|
60
96
|
lmethod = lbinding.eval("__method__")
|
61
97
|
next if lmethod.nil?
|
62
|
-
puts "lmethod: #{lmethod}" if OrderCop.debug
|
63
|
-
if OrderCop
|
64
|
-
puts "#{lmethod} is whitelisted, ignoring" if OrderCop.debug
|
98
|
+
puts "lmethod: #{lmethod}" if OrderCop.config.debug
|
99
|
+
if OrderCop.config.whitelist_methods.include?(lmethod)
|
100
|
+
puts "#{lmethod} is whitelisted, ignoring" if OrderCop.config.debug
|
65
101
|
return true
|
66
102
|
end
|
67
103
|
end
|
@@ -70,8 +106,8 @@ module OrderCop
|
|
70
106
|
|
71
107
|
def detect_missing_order(method)
|
72
108
|
return if OrderCop.disabled?
|
73
|
-
puts "missing order, detect if allowed" if OrderCop.debug
|
74
|
-
puts "stack size: #{binding.frame_count}" if OrderCop.debug
|
109
|
+
puts "missing order, detect if allowed" if OrderCop.config.debug
|
110
|
+
puts "stack size: #{binding.frame_count}" if OrderCop.config.debug
|
75
111
|
|
76
112
|
return if stack_is_whitelisted?
|
77
113
|
|
@@ -90,42 +126,22 @@ module OrderCop
|
|
90
126
|
msg = "Missing Order for :#{method}"
|
91
127
|
end
|
92
128
|
|
93
|
-
|
94
|
-
|
129
|
+
red = ->(msg) { ActiveSupport::LogSubscriber.new.send(:color, msg, :red) }
|
130
|
+
|
131
|
+
if OrderCop.config.rails_logger
|
132
|
+
Rails.logger.error red.call(msg)
|
95
133
|
caller.each do |line|
|
96
|
-
Rails.logger.error
|
134
|
+
Rails.logger.error red.call(" #{line}") if line.include?(Rails.root.to_s)
|
97
135
|
end
|
98
136
|
end
|
99
|
-
if OrderCop.raise
|
137
|
+
if OrderCop.config.raise
|
100
138
|
raise OrderCop::Error.new(msg)
|
101
139
|
end
|
102
140
|
end
|
103
|
-
|
104
|
-
def order_cop_red(msg)
|
105
|
-
ActiveSupport::LogSubscriber.new.send(:color, msg, :red)
|
106
|
-
end
|
107
141
|
end
|
108
142
|
|
109
143
|
class Error < StandardError; end
|
110
144
|
|
111
|
-
WHITELIST = %i[sum any? none? inspect method_missing not load_target reindex attachment attachments attributes_table with_current_arbre_element].freeze
|
112
|
-
|
113
|
-
def self.disable(&block)
|
114
|
-
old_enabled = @enabled
|
115
|
-
@enabled = false
|
116
|
-
yield
|
117
|
-
ensure
|
118
|
-
@enabled = old_enabled
|
119
|
-
end
|
120
|
-
|
121
|
-
def self.disabled?
|
122
|
-
@enabled == false
|
123
|
-
end
|
124
|
-
|
125
|
-
def self.enabled?
|
126
|
-
@enabled != false
|
127
|
-
end
|
128
|
-
|
129
145
|
def self.patch_active_record(app)
|
130
146
|
ActiveRecord::Base.descendants.each do |model|
|
131
147
|
model.const_get(:ActiveRecord_Associations_CollectionProxy).class_eval do
|
@@ -140,31 +156,6 @@ module OrderCop
|
|
140
156
|
end
|
141
157
|
end
|
142
158
|
|
143
|
-
def self.rails_logger?
|
144
|
-
config.rails_logger
|
145
|
-
end
|
146
|
-
|
147
|
-
def self.raise?
|
148
|
-
config.raise
|
149
|
-
end
|
150
|
-
|
151
|
-
def self.debug?
|
152
|
-
config.debug
|
153
|
-
end
|
154
|
-
|
155
|
-
def self.config(**options)
|
156
|
-
@config ||= OpenStruct.new(rails_logger: false, raise: true, enabled: true, debug: false)
|
157
|
-
options.each do |k, v|
|
158
|
-
@config[k] = v
|
159
|
-
end
|
160
|
-
@enabled = @config.enabled
|
161
|
-
@config
|
162
|
-
end
|
163
|
-
|
164
|
-
def self.setup(**options)
|
165
|
-
config(**options)
|
166
|
-
end
|
167
|
-
|
168
159
|
def self.apply(app = Rails.application)
|
169
160
|
patch_active_record(app) if enabled?
|
170
161
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: order_cop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gauthier Monserand
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-08-
|
11
|
+
date: 2023-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|