Module: Whodunit::TableDefinitionExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/whodunit/table_definition_extension.rb

Overview

Extension for ActiveRecord::ConnectionAdapters::TableDefinition to automatically inject whodunit_stamps when creating tables.

This module monkey-patches the TableDefinition’s column creation methods to automatically add whodunit stamp columns when auto-injection is enabled.

Examples:

Enabling auto-injection

Whodunit.configure do |config|
  config.auto_inject_whodunit_stamps = true
end

# Now this migration will automatically include whodunit stamps:
class CreatePosts < ActiveRecord::Migration[8.0]
  def change
    create_table :posts do |t|
      t.string :title
      t.text :body
      t.timestamps
      # t.whodunit_stamps automatically added at the end!
    end
  end
end

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#timestamps(**options) ⇒ Object

Override timestamps to trigger automatic whodunit_stamps injection

Since:

  • 0.1.0



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/whodunit/table_definition_extension.rb', line 37

def timestamps(**options)
  result = super

  # Auto-inject whodunit_stamps after timestamps if enabled and not already added
  if Whodunit.auto_inject_whodunit_stamps &&
     !@_whodunit_stamps_added &&
     !options[:skip_whodunit_stamps]
    whodunit_stamps(include_deleter: :auto)
    @_whodunit_stamps_added = true
  end

  result
end

#whodunit_stamps(**options) ⇒ Object

Also override whodunit_stamps to track that they’ve been added

Since:

  • 0.1.0



52
53
54
55
# File 'lib/whodunit/table_definition_extension.rb', line 52

def whodunit_stamps(**options)
  @_whodunit_stamps_added = true
  super
end