Module: RailsConsoleAi::BuiltinGuards
- Defined in:
- lib/rails_console_ai/safety_guards.rb
Overview
Built-in guard: database write prevention Works on all Rails versions (5+) and all database adapters. Prepends a write-intercepting module once, controlled by a thread-local flag.
Defined Under Namespace
Modules: HttpBlocker, WriteBlocker
Class Method Summary
collapse
Class Method Details
.database_writes ⇒ Object
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/rails_console_ai/safety_guards.rb', line 259
def self.database_writes
->(& block) {
ensure_write_blocker_installed!
Thread.current[:rails_console_ai_block_writes] = true
begin
block.call
ensure
Thread.current[:rails_console_ai_block_writes] = false
end
}
end
|
.ensure_http_blocker_installed! ⇒ Object
331
332
333
334
335
336
337
338
339
|
# File 'lib/rails_console_ai/safety_guards.rb', line 331
def self.ensure_http_blocker_installed!
return if @http_blocker_installed
require 'net/http'
unless Net::HTTP.ancestors.include?(HttpBlocker)
Net::HTTP.prepend(HttpBlocker)
end
@http_blocker_installed = true
end
|
.ensure_write_blocker_installed! ⇒ Object
271
272
273
274
275
276
277
278
279
|
# File 'lib/rails_console_ai/safety_guards.rb', line 271
def self.ensure_write_blocker_installed!
return if @write_blocker_installed
connection = ActiveRecord::Base.connection
unless connection.class.ancestors.include?(WriteBlocker)
connection.class.prepend(WriteBlocker)
end
@write_blocker_installed = true
end
|
.http_mutations ⇒ Object
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/rails_console_ai/safety_guards.rb', line 305
def self.http_mutations
->(&block) {
ensure_http_blocker_installed!
Thread.current[:rails_console_ai_block_http] = true
begin
block.call
ensure
Thread.current[:rails_console_ai_block_http] = false
end
}
end
|
.mailers ⇒ Object
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/rails_console_ai/safety_guards.rb', line 317
def self.mailers
->(&block) {
return block.call if Thread.current[:rails_console_ai_bypass_guards]
old_value = ActionMailer::Base.perform_deliveries
ActionMailer::Base.perform_deliveries = false
begin
block.call
ensure
ActionMailer::Base.perform_deliveries = old_value
end
}
end
|