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

#_whodunit_stamps_addedObject

Since:

  • 0.1.0



31
32
33
# File 'lib/whodunit/table_definition_extension.rb', line 31

def _whodunit_stamps_added
  @_whodunit_stamps_added
end

#_whodunit_stamps_added=(value) ⇒ Object

Since:

  • 0.1.0



35
36
37
# File 'lib/whodunit/table_definition_extension.rb', line 35

def _whodunit_stamps_added=(value)
  @_whodunit_stamps_added = value
end

#timestamps(**options) ⇒ Object

Override timestamps to trigger automatic whodunit_stamps injection

Since:

  • 0.1.0



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

def timestamps(**options)
  options = options.dup
  skip = options.delete(:skip_whodunit_stamps)
  result = super

  if Whodunit.auto_inject_whodunit_stamps && !_whodunit_stamps_added && !skip
    whodunit_stamps(include_deleter: :auto)
  end

  result
end

#whodunit_stamps(include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil) ⇒ Object

assign true tp the tracker @_whodunit_stamps_added before the reuse/call of the whodunit_stamps flow from the Whodunit::MigrationHelpers module (see railtie.rb)

Since:

  • 0.1.0



55
56
57
58
# File 'lib/whodunit/table_definition_extension.rb', line 55

def whodunit_stamps(include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil)
  self._whodunit_stamps_added = true
  self.class.whodunit_stamps(self, include_deleter:, creator_type:, updater_type:, deleter_type:)
end