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
295
296
297
298
299
300
301
302
303
304
305
|
# File 'lib/rails_console_ai/safety_guards.rb', line 295
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
367
368
369
370
371
372
373
374
375
|
# File 'lib/rails_console_ai/safety_guards.rb', line 367
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
307
308
309
310
311
312
313
314
315
|
# File 'lib/rails_console_ai/safety_guards.rb', line 307
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
341
342
343
344
345
346
347
348
349
350
351
|
# File 'lib/rails_console_ai/safety_guards.rb', line 341
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
353
354
355
356
357
358
359
360
361
362
363
364
365
|
# File 'lib/rails_console_ai/safety_guards.rb', line 353
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
|