Class: AuditLogger::RecordAuditEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/audit_logger/record_audit_entry.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, action:) ⇒ RecordAuditEntry

Returns a new instance of RecordAuditEntry.



9
10
11
12
13
14
# File 'lib/audit_logger/record_audit_entry.rb', line 9

def initialize(record, action:)
  @record = record
  @action = action.to_s
  @configuration = AuditLogger.configuration
  @model_config = record.class.audit_logger_model_config
end

Class Method Details

.call(record, action:) ⇒ Object



5
6
7
# File 'lib/audit_logger/record_audit_entry.rb', line 5

def self.call(record, action:)
  new(record, action: action).call
end

Instance Method Details

#callObject

Orquestra toda a montagem do log e persiste o resultado final.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/audit_logger/record_audit_entry.rb', line 17

def call
  payload = ChangeExtractor.new(
    record: record,
    action: action,
    model_config: model_config,
    configuration: configuration
  ).call

  return if payload.fetch("fields", {}).empty?

  # Resolve o contexto do ator e gera a versao amigavel do payload antes de persistir.
  actor_context = ActorContextResolver.call(configuration)
  humanized_payload = Humanizer.new(
    record: record,
    payload: payload,
    model_config: model_config,
    configuration: configuration
  ).call

  AuditLog.create!(
    model_class_name: record.class.name,
    id_object: resolve_record_id,
    action: action,
    uuid: actor_context[:uuid].to_s,
    changed_by_id: actor_context[:changed_by_id],
    changed_by_type: actor_context[:changed_by_type],
    changed_by_other: actor_context[:changed_by_other],
    audited_changes: payload,
    audited_changes_humanize: humanized_payload,
    ip_remote: actor_context[:ip_remote],
    created_at: Time.current,
    updated_at: Time.current
  )
end