prosopite 2.0.0 → 2.1.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: 3fac117d558ad73ba2057f45ce8ec278898d8c1b00aec41ec9a169f76c842c02
4
- data.tar.gz: ee3a372e02cb138bdbaf1770b5758693c51bdedc173ff2ca93aed20413b22245
3
+ metadata.gz: 79f272a757baeae3f05f7721b7204130588532ac09fb28d163d09737512f8edd
4
+ data.tar.gz: d45ecc27ba09f2b55447eb82f9fc35773846cf44d924b710a83eaa5ada5260a0
5
5
  SHA512:
6
- metadata.gz: e49619ff0354d91b0ebade2c418ef5a2d189d68ed3f259dca8674e378366d441144a71c0ff1d615baa834c8a00d1aea60ac7be6435fdb1035da63a61fefc3731
7
- data.tar.gz: a6ca678e0052bdc3cdbce19e95c88ca233162bfe45a18f87f70f2ad77e68e2c0264e18e517979356159be90ac22f103f8c4779cd5fa29766a55d72228e5e1d09
6
+ metadata.gz: 60cd8f6dc74fc8a51e7ada04ae53212c8965309ffbe67ad83f6e2537a289bb205a4e626e44fda4b14673aed407cf2bb706306ba0f47051d22da7a16c78f73fcb
7
+ data.tar.gz: 66c392a288ddbdf44bacc433740c9b6c2e7213c714e80c77992581460736300451a0f7add73196d81203a0533900424128754960ffbce6914d20c9e01cfdb8c5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prosopite (2.0.0)
4
+ prosopite (2.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -117,6 +117,9 @@ The preferred type of notifications can be configured with:
117
117
 
118
118
  * `Prosopite.min_n_queries`: Minimum number of N queries to report per N+1 case. Defaults to 2.
119
119
  * `Prosopite.raise = true`: Raise warnings as exceptions. Defaults to `false`.
120
+ * `Prosopite.start_raise`: Raises warnings as exceptions from when this is called. Overrides `Proposite.raise`.
121
+ * `Propsoite.stop_raise`: Disables raising warnings as exceptions if previously enabled with `Proposite.start_raise`.
122
+ * `Prosopite.local_raise?`: Returns `true` if `Prosopite.start_raise` has been called previously.
120
123
  * `Prosopite.rails_logger = true`: Send warnings to the Rails log. Defaults to `false`.
121
124
  * `Prosopite.prosopite_logger = true`: Send warnings to `log/prosopite.log`. Defaults to `false`.
122
125
  * `Prosopite.stderr_logger = true`: Send warnings to STDERR. Defaults to `false`.
@@ -124,29 +127,6 @@ The preferred type of notifications can be configured with:
124
127
  * `Prosopite.custom_logger = my_custom_logger`: Set a custom logger. See the following section for the details. Defaults to `false`.
125
128
  * `Prosopite.enabled = true`: Enables or disables the gem. Defaults to `true`.
126
129
 
127
- ### Custom Logging Configuration
128
-
129
- You can supply a custom logger with the `Prosopite.custom_logger` setting.
130
-
131
- This is useful for circumstances where you don't want your logs to be
132
- highlighted with red, or you want logs sent to a custom location.
133
-
134
- One common scenario is that you may be generating json logs and sending them to
135
- Datadog, ELK stack, or similar, and don't want to have to remove the default red
136
- escaping data from messages sent to the Rails logger, or want to tag them
137
- differently with your own custom logger.
138
-
139
- ```ruby
140
- # Turns off logging with red highlights, but still sends them to the Rails logger
141
- Prosopite.custom_logger = Rails.logger
142
- ```
143
-
144
- ```ruby
145
- # Use a completely custom logging instance
146
- Prosopite.custom_logger = MyLoggerClass.new
147
-
148
- ```
149
-
150
130
  ## Development Environment Usage
151
131
 
152
132
  Prosopite auto-detection can be enabled on all controllers:
@@ -174,7 +154,7 @@ config.after_initialize do
174
154
  Prosopite.rails_logger = true
175
155
  end
176
156
  ```
177
-
157
+ ```
178
158
  ## Test Environment Usage
179
159
 
180
160
  Tests with N+1 queries can be configured to fail with:
@@ -315,6 +295,77 @@ Pauses can be ignored with `Prosopite.ignore_pauses = true` in case you want to
315
295
  An example of when you might use this is if you are [testing Active Jobs inline](https://guides.rubyonrails.org/testing.html#testing-jobs),
316
296
  and don't want to run Prosopite on background job code, just foreground app code. In that case you could write an [Active Job callback](https://edgeguides.rubyonrails.org/active_job_basics.html#callbacks) that pauses the scan while the job is running.
317
297
 
298
+ ## Local Raise
299
+
300
+ In some cases you may want to configure prosopite to not raise by default and only raise in certain scenarios.
301
+ In this example we scan on all controllers but also provide an API to only raise on specific actions.
302
+
303
+ ```ruby
304
+ Proposite.raise = false
305
+ ```
306
+
307
+ ```ruby
308
+ # app/controllers/application_controller.rb
309
+ class ApplicationController < ActionController::Base
310
+ def raise_on_n_plus_ones!(**options)
311
+ return if Rails.env.production?
312
+
313
+ prepend_around_action(:_raise_on_n_plus_ones, **options)
314
+ end
315
+
316
+ unless Rails.env.production?
317
+ around_action :n_plus_one_detection
318
+
319
+ def n_plus_one_detection
320
+ ...
321
+ end
322
+
323
+ def _raise_on_n_plus_ones
324
+ Proposite.start_raise
325
+ yield
326
+ ensure
327
+ Prosopite.stop_raise
328
+ end
329
+ end
330
+ end
331
+ ```
332
+ ```ruby
333
+ # app/controllers/books_controller.rb
334
+ class BooksController < ApplicationController
335
+ raise_on_n_plus_ones!(only: [:index])
336
+
337
+ def index
338
+ @books = Book.all.map(&:author) # This will raise N+1 errors
339
+ end
340
+
341
+ def show
342
+ @book = Book.find(params[:id])
343
+ @book.reviews.map(&:author) # This will not raise N+1 errors
344
+ end
345
+ end
346
+
347
+ ## Custom Logging Configuration
348
+
349
+ You can supply a custom logger with the `Prosopite.custom_logger` setting.
350
+
351
+ This is useful for circumstances where you don't want your logs to be
352
+ highlighted with red, or you want logs sent to a custom location.
353
+
354
+ One common scenario is that you may be generating json logs and sending them to
355
+ Datadog, ELK stack, or similar, and don't want to have to remove the default red
356
+ escaping data from messages sent to the Rails logger, or want to tag them
357
+ differently with your own custom logger.
358
+
359
+ ```ruby
360
+ # Turns off logging with red highlights, but still sends them to the Rails logger
361
+ Prosopite.custom_logger = Rails.logger
362
+ ```
363
+
364
+ ```ruby
365
+ # Use a completely custom logging instance
366
+ Prosopite.custom_logger = MyLoggerClass.new
367
+ ```
368
+
318
369
  ## Contributing
319
370
 
320
371
  Bug reports and pull requests are welcome on GitHub at https://github.com/charkost/prosopite.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prosopite
4
- VERSION = "2.0.0"
4
+ VERSION = "2.1.0"
5
5
  end
data/lib/prosopite.rb CHANGED
@@ -113,6 +113,22 @@ module Prosopite
113
113
  tc[:prosopite_query_caller] = nil
114
114
  end
115
115
 
116
+ def start_raise
117
+ tc[:prosopite_local_raise] = true
118
+ end
119
+
120
+ def stop_raise
121
+ tc[:prosopite_local_raise] = false
122
+ end
123
+
124
+ def local_raise?
125
+ tc[:prosopite_local_raise] == true
126
+ end
127
+
128
+ def raise?
129
+ local_raise? || !!@raise
130
+ end
131
+
116
132
  def create_notifications
117
133
  tc[:prosopite_notifications] = {}
118
134
 
@@ -217,7 +233,6 @@ module Prosopite
217
233
  @rails_logger ||= false
218
234
  @stderr_logger ||= false
219
235
  @prosopite_logger ||= false
220
- @raise ||= false
221
236
 
222
237
  notifications_str = String.new('')
223
238
 
@@ -246,7 +261,7 @@ module Prosopite
246
261
  end
247
262
  end
248
263
 
249
- raise NPlusOneQueriesError.new(notifications_str) if @raise
264
+ raise NPlusOneQueriesError.new(notifications_str) if raise?
250
265
  end
251
266
 
252
267
  def red(str)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prosopite
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mpampis Kostas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-20 00:00:00.000000000 Z
11
+ date: 2025-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry