Module: Whodunit

Defined in:
lib/whodunit.rb,
lib/whodunit/current.rb,
lib/whodunit/railtie.rb,
lib/whodunit/version.rb,
lib/whodunit/generator.rb,
lib/whodunit/stampable.rb,
lib/whodunit/migration_helpers.rb,
lib/whodunit/controller_methods.rb,
lib/whodunit/table_definition_extension.rb

Overview

Lightweight creator/updater/deleter tracking for ActiveRecord models.

Whodunit provides simple auditing by tracking who created, updated, and deleted ActiveRecord models. It features smart soft-delete detection and zero performance overhead.

Examples:

Basic usage

class Post < ApplicationRecord
  include Whodunit::Stampable
end

# In controller
Whodunit::Current.user = current_user
post = Post.create(title: "Hello")
post.creator_id # => current_user.id

Configuration

Whodunit.configure do |config|
  config.user_class = "Account"
  config.creator_column = :created_by_id
  config.column_data_type = :integer
end

Author:

  • Ken C. Demanawa

Since:

  • 0.1.0

Defined Under Namespace

Modules: ControllerMethods, MigrationHelpers, Stampable, TableDefinitionExtension Classes: Current, Error, Generator, Railtie

Constant Summary collapse

VERSION =

Since:

  • 0.1.0

"0.2.0"

Configuration collapse

Data Type Configuration collapse

Data Type Helpers collapse

Class Method Details

.configure {|self| ... } ⇒ void

This method returns an undefined value.

Configure Whodunit settings

Examples:

Whodunit.configure do |config|
  config.user_class = "Account"
  config.creator_column = :created_by_id
  config.column_data_type = :integer
end

Yields:

  • (self)

    configuration block

Raises:

  • (Whodunit::Error)

    if both creator_column and updater_column are set to nil

Since:

  • 0.1.0



98
99
100
101
# File 'lib/whodunit.rb', line 98

def self.configure
  yield self
  validate_column_configuration!
end

.creator_data_typeSymbol

Get the data type for the creator column

Returns:

  • (Symbol)

    the creator column data type

Since:

  • 0.1.0



114
115
116
# File 'lib/whodunit.rb', line 114

def self.creator_data_type
  creator_column_type || column_data_type
end

.creator_enabled?Boolean

Check if creator column is enabled

Returns:

  • (Boolean)

    true if creator_column is not nil

Since:

  • 0.1.0



138
139
140
# File 'lib/whodunit.rb', line 138

def self.creator_enabled?
  !creator_column.nil?
end

.deleter_data_typeSymbol

Get the data type for the deleter column

Returns:

  • (Symbol)

    the deleter column data type

Since:

  • 0.1.0



126
127
128
# File 'lib/whodunit.rb', line 126

def self.deleter_data_type
  deleter_column_type || column_data_type
end

.deleter_enabled?Boolean

Check if deleter column is enabled

Returns:

  • (Boolean)

    true if deleter_column is not nil

Since:

  • 0.1.0



150
151
152
# File 'lib/whodunit.rb', line 150

def self.deleter_enabled?
  !deleter_column.nil?
end

.soft_delete_enabled?Boolean

Check if soft-delete is enabled

Returns:

  • (Boolean)

    true if soft-delete is configured (soft_delete_column is not nil)

Since:

  • 0.1.0



132
133
134
# File 'lib/whodunit.rb', line 132

def self.soft_delete_enabled?
  !soft_delete_column.nil?
end

.updater_data_typeSymbol

Get the data type for the updater column

Returns:

  • (Symbol)

    the updater column data type

Since:

  • 0.1.0



120
121
122
# File 'lib/whodunit.rb', line 120

def self.updater_data_type
  updater_column_type || column_data_type
end

.updater_enabled?Boolean

Check if updater column is enabled

Returns:

  • (Boolean)

    true if updater_column is not nil

Since:

  • 0.1.0



144
145
146
# File 'lib/whodunit.rb', line 144

def self.updater_enabled?
  !updater_column.nil?
end

.user_class_nameString

Get the user class name as a string

Returns:

  • (String)

    the user class name

Since:

  • 0.1.0



106
107
108
# File 'lib/whodunit.rb', line 106

def self.user_class_name
  user_class.to_s
end

.validate_column_configuration!Object

Validate that column configuration is valid

Raises:

Since:

  • 0.1.0



156
157
158
159
160
161
162
# File 'lib/whodunit.rb', line 156

def self.validate_column_configuration!
  return if creator_enabled? || updater_enabled?

  raise Whodunit::Error,
        "At least one of creator_column or updater_column must be configured (not nil). " \
        "Setting both to nil would disable all stamping functionality."
end

Instance Method Details

#auto_inject_whodunit_stampsBoolean

Whether to automatically add whodunit_stamps to create_table migrations (default: true)

Returns:

  • (Boolean)

    auto-injection setting

Since:

  • 0.1.0



66
# File 'lib/whodunit.rb', line 66

mattr_accessor :auto_inject_whodunit_stamps, default: true

#column_data_typeSymbol

The default data type for stamp columns (default: :bigint)

Returns:

  • (Symbol)

    the default column data type

Since:

  • 0.1.0



72
# File 'lib/whodunit.rb', line 72

mattr_accessor :column_data_type, default: :bigint

#creator_columnSymbol

The column name for tracking who created the record (default: :creator_id)

Returns:

  • (Symbol)

    the creator column name

Since:

  • 0.1.0



48
# File 'lib/whodunit.rb', line 48

mattr_accessor :creator_column, default: :creator_id

#creator_column_typeSymbol?

Specific data type for creator column (overrides column_data_type if set)

Returns:

  • (Symbol, nil)

    the creator column data type

Since:

  • 0.1.0



76
# File 'lib/whodunit.rb', line 76

mattr_accessor :creator_column_type, default: nil

#deleter_columnSymbol

The column name for tracking who deleted the record (default: :deleter_id)

Returns:

  • (Symbol)

    the deleter column name

Since:

  • 0.1.0



56
# File 'lib/whodunit.rb', line 56

mattr_accessor :deleter_column, default: :deleter_id

#deleter_column_typeSymbol?

Specific data type for deleter column (overrides column_data_type if set)

Returns:

  • (Symbol, nil)

    the deleter column data type

Since:

  • 0.1.0



84
# File 'lib/whodunit.rb', line 84

mattr_accessor :deleter_column_type, default: nil

#soft_delete_columnSymbol?

The column name used for soft-delete timestamps (default: nil) Set to a column name to enable soft-delete support (e.g., :deleted_at, :discarded_at) Set to nil to disable soft-delete support entirely

Returns:

  • (Symbol, nil)

    the soft-delete column name

Since:

  • 0.1.0



62
# File 'lib/whodunit.rb', line 62

mattr_accessor :soft_delete_column, default: nil

#updater_columnSymbol

The column name for tracking who updated the record (default: :updater_id)

Returns:

  • (Symbol)

    the updater column name

Since:

  • 0.1.0



52
# File 'lib/whodunit.rb', line 52

mattr_accessor :updater_column, default: :updater_id

#updater_column_typeSymbol?

Specific data type for updater column (overrides column_data_type if set)

Returns:

  • (Symbol, nil)

    the updater column data type

Since:

  • 0.1.0



80
# File 'lib/whodunit.rb', line 80

mattr_accessor :updater_column_type, default: nil

#user_classString

The class name of the user model (default: “User”)

Returns:

  • (String)

    the user class name

Since:

  • 0.1.0



44
# File 'lib/whodunit.rb', line 44

mattr_accessor :user_class, default: "User"