Class: PgSqlTriggers::AuditLog

Inherits:
ApplicationRecord show all
Defined in:
app/models/pg_sql_triggers/audit_log.rb

Overview

Audit log model for tracking all trigger operations

Constant Summary collapse

SUCCESS_ATTRS =

Known keyword options accepted by log_success / log_failure, in addition to operation (required for both) and error_message (required for log_failure).

%i[trigger_name actor environment reason confirmation_text
before_state after_state diff].freeze
FAILURE_ATTRS =
%i[trigger_name actor environment reason confirmation_text before_state].freeze

Class Method Summary collapse

Class Method Details

.for_trigger_name(trigger_name) ⇒ ActiveRecord::Relation

Get audit log entries for a specific trigger

Parameters:

  • trigger_name (String)

    The trigger name

Returns:

  • (ActiveRecord::Relation)

    Audit log entries for the trigger



71
72
73
# File 'app/models/pg_sql_triggers/audit_log.rb', line 71

def for_trigger_name(trigger_name)
  for_trigger(trigger_name).recent
end

.log_failure(operation:, error_message:, **options) ⇒ Object

Log a failed operation.

Required: operation: (Symbol/String) and error_message: (String). Optional (all via keyword args): trigger_name, actor, environment, reason, confirmation_text, before_state.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/pg_sql_triggers/audit_log.rb', line 52

def log_failure(operation:, error_message:, **options)
  attrs = options.slice(*FAILURE_ATTRS)
  create!(
    attrs.merge(
      operation: operation.to_s,
      actor: serialize_actor(attrs[:actor]),
      status: "failure",
      error_message: error_message
    )
  )
rescue StandardError => e
  Rails.logger.error("Failed to log audit entry: #{e.message}") if defined?(Rails.logger)
  nil
end

.log_success(operation:, **options) ⇒ Object

Log a successful operation.

Required: operation: (Symbol/String). Optional (all via keyword args): trigger_name, actor, environment, reason, confirmation_text, before_state, after_state, diff.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/pg_sql_triggers/audit_log.rb', line 33

def log_success(operation:, **options)
  attrs = options.slice(*SUCCESS_ATTRS)
  create!(
    attrs.merge(
      operation: operation.to_s,
      actor: serialize_actor(attrs[:actor]),
      status: "success"
    )
  )
rescue StandardError => e
  Rails.logger.error("Failed to log audit entry: #{e.message}") if defined?(Rails.logger)
  nil
end