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
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/rails_console_ai/safety_guards.rb', line 140
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
208
209
210
211
212
213
214
215
216
|
# File 'lib/rails_console_ai/safety_guards.rb', line 208
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
152
153
154
155
156
157
158
159
160
|
# File 'lib/rails_console_ai/safety_guards.rb', line 152
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
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/rails_console_ai/safety_guards.rb', line 184
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
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/rails_console_ai/safety_guards.rb', line 196
def self.mailers
->(&block) {
old_value = ActionMailer::Base.perform_deliveries
ActionMailer::Base.perform_deliveries = false
begin
block.call
ensure
ActionMailer::Base.perform_deliveries = old_value
end
}
end
|