Class: IronAdmin::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_admin/audit_log.rb

Overview

Audit logging system for tracking admin panel actions.

The AuditLog provides built-in tracking of create, update, and delete operations performed through the admin panel. It supports two storage backends: in-memory (default) and database.

Enabling Audit Logging

Enable in your initializer:

IronAdmin.configure do |config|
config.audit_enabled = true
config.audit_storage = :database  # or :memory
end

For database storage, run the generator:

rails generate iron_admin:install_audit
rails db:migrate

Viewing Audit Logs

When enabled, audit logs are viewable at /admin/audit.

Examples:

Query recent audit entries

entries = IronAdmin::AuditLog.query(resource: "users", action: :update)
entries.each do |entry|
  puts "#{entry.user} updated user #{entry.record_id}"
end

See Also:

Defined Under Namespace

Classes: Entry

Class Method Summary collapse

Class Method Details

.clear!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Clears all audit entries.



147
148
149
150
# File 'lib/iron_admin/audit_log.rb', line 147

def clear!
  @entries = []
  AuditEntry.delete_all if database_storage_available?
end

.database_storage_available?Boolean

Checks if database storage is configured and available.

Returns:

  • (Boolean)

    True if database storage can be used



155
156
157
158
159
160
161
# File 'lib/iron_admin/audit_log.rb', line 155

def database_storage_available?
  IronAdmin.configuration.audit_storage == :database &&
    defined?(AuditEntry) &&
    AuditEntry.table_exists?
rescue ActiveRecord::StatementInvalid, ActiveRecord::NoDatabaseError
  false
end

.entriesArray<Entry>

Returns all in-memory audit entries.

Returns:

  • (Array<Entry>)

    In-memory entries



97
98
99
# File 'lib/iron_admin/audit_log.rb', line 97

def entries
  @entries ||= []
end

.log(event) ⇒ Entry, ...

Logs an audit event.

Automatically routes to the configured storage backend (memory or database).

Parameters:

  • event (Object)

    An event object with user, action, resource, record_id, changes, and ip_address attributes

Returns:



109
110
111
112
113
114
115
116
117
# File 'lib/iron_admin/audit_log.rb', line 109

def log(event)
  return unless IronAdmin.configuration.audit_enabled

  if database_storage_available?
    log_to_database(event)
  else
    log_to_memory(event)
  end
end

.query(filters = {}) ⇒ Array<Entry>, ActiveRecord::Relation

Queries audit entries with optional filters.

Examples:

Query by resource

AuditLog.query(resource: "users")

Query by date range

AuditLog.query(from: 1.week.ago, to: Time.current)

Parameters:

  • filters (Hash) (defaults to: {})

    Filter criteria

Options Hash (filters):

  • :resource (String)

    Filter by resource name

  • :action (Symbol, String)

    Filter by action type

  • :record_id (Integer, String)

    Filter by record ID

  • :from (Time, Date)

    Entries after this time

  • :to (Time, Date)

    Entries before this time

Returns:

  • (Array<Entry>, ActiveRecord::Relation)

    Matching entries



135
136
137
138
139
140
141
# File 'lib/iron_admin/audit_log.rb', line 135

def query(filters = {})
  if database_storage_available?
    query_database(filters)
  else
    query_memory(filters)
  end
end