Skip to content

Commit 55dc144

Browse files
DanielGilchristcharkost
authored andcommitted
Document Prosopite.start_raise / Prosopite.stop_raise usage
1 parent 1de30ed commit 55dc144

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ The preferred type of notifications can be configured with:
117117

118118
* `Prosopite.min_n_queries`: Minimum number of N queries to report per N+1 case. Defaults to 2.
119119
* `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.
120123
* `Prosopite.rails_logger = true`: Send warnings to the Rails log. Defaults to `false`.
121124
* `Prosopite.prosopite_logger = true`: Send warnings to `log/prosopite.log`. Defaults to `false`.
122125
* `Prosopite.stderr_logger = true`: Send warnings to STDERR. Defaults to `false`.
@@ -174,7 +177,52 @@ config.after_initialize do
174177
Prosopite.rails_logger = true
175178
end
176179
```
180+
In some cases you may want to configure prosopite to not raise by default and only raise in certain scenarios. In this example we scan on all controllers but also provide an API to only raise on specific actions.
177181

182+
```ruby
183+
Proposite.raise = false
184+
```
185+
186+
```ruby
187+
# app/controllers/application_controller.rb
188+
class ApplicationController < ActionController::Base
189+
def raise_on_n_plus_ones!(**options)
190+
return if Rails.env.production?
191+
192+
prepend_around_action(:_raise_on_n_plus_ones, **options)
193+
end
194+
195+
unless Rails.env.production?
196+
around_action :n_plus_one_detection
197+
198+
def n_plus_one_detection
199+
...
200+
end
201+
202+
def _raise_on_n_plus_ones
203+
Proposite.start_raise
204+
yield
205+
ensure
206+
Prosopite.stop_raise
207+
end
208+
end
209+
end
210+
```
211+
```ruby
212+
# app/controllers/books_controller.rb
213+
class BooksController < ApplicationController
214+
raise_on_n_plus_ones!(only: [:index])
215+
216+
def index
217+
@books = Book.all.map(&:author) # This will raise N+1 errors
218+
end
219+
220+
def show
221+
@book = Book.find(params[:id])
222+
@book.reviews.map(&:author) # This will not raise N+1 errors
223+
end
224+
end
225+
```
178226
## Test Environment Usage
179227

180228
Tests with N+1 queries can be configured to fail with:

0 commit comments

Comments
 (0)