Module: AuditTracker::ModelHelper

Defined in:
lib/audit_tracker/model_helper.rb

Overview

Model helper for setting up trackers in an ActiveRecord model

Instance Method Summary collapse

Instance Method Details

#track_audit_data(**options) ⇒ Object

Helper to apply AuditTracker to a model. This helper is automatically added to ActiveRecord::Base when it loads (@see Railtie)

‘track_audit_data` options are the keys of the trackers defined. Each tracker is enabled by passing `true` to it’s key.

I.e.:

track_audit_data user: true, user_department: true

A hash of options can also be used to override any tracker option:

track_audit_data(
  user: {
    created_by: { class_name: "::ManagerUser" },
    updated_by: { class_name: "::ManagerUser" },
  }
)

And the value block can also be overriden:

track_audit_data(
  user: {
    created_by: { class_name: "::ManagerUser", value: -> { User.current.becomes(ManagerUser) } },
    updated_by: { class_name: "::ManagerUser", value: -> { User.current.becomes(ManagerUser) } },
  }
)

Parameters:

  • options (Hash<Symbol,(Hash, Boolean)>)

    options hash



37
38
39
# File 'lib/audit_tracker/model_helper.rb', line 37

def track_audit_data(**options)
  ::AuditTracker.apply(self, **options)
end