Class: IronAdmin::Generators::InstallAuditGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/iron_admin/install_audit/install_audit_generator.rb

Overview

Generator for setting up database-backed audit logging.

Creates the migration for the audit_entries table, which stores a persistent log of all admin panel actions.

Examples:

Running the generator

rails generate iron_admin:install_audit
rails db:migrate

Enabling database audit storage

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

See Also:

Instance Method Summary collapse

Instance Method Details

#create_migrationvoid

This method returns an undefined value.

Writes the audit entries migration into db/migrate/.

We deliberately avoid Rails::Generators::Migration#migration_template here: that helper goes through ActiveRecord::Migration.current_version which opens a DB connection to read the schema cache. On a clean machine (DB doesn't exist yet) or during a deploy where the DB is briefly unreachable, that crashes the generator with ActiveRecord::ConnectionNotEstablished and the migration file never gets written. Using plain template plus a timestamp-based filename keeps the generator DB-free, so bin/rails db:create db:migrate (the natural bootstrap order) Just Works.



38
39
40
41
42
43
44
45
46
# File 'lib/generators/iron_admin/install_audit/install_audit_generator.rb', line 38

def create_migration
  timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
  target = "db/migrate/#{timestamp}_create_iron_admin_audit_entries.rb"
  if existing_audit_migration?
    say_status :skip, "create_iron_admin_audit_entries migration already exists", :yellow
    return
  end
  template "create_iron_admin_audit_entries.rb.tt", target
end