Module: ModelsAuditor::Audit::InstanceMethods

Defined in:
lib/models_auditor/audit.rb

Instance Method Summary collapse

Instance Method Details

#do_audit_init_snapshotObject



24
25
26
27
28
29
# File 'lib/models_auditor/audit.rb', line 24

def do_audit_init_snapshot
  return unless ModelsAuditor.config.audit_enabled
  mode = self.class.instance_variable_get(:@audit_mode)
  return unless self.class.instance_variable_get(:@audit_enabled) && AUDIT_SNAPSHOT_MODES.include?(mode)
  ma_store_initial_state(ModelsAuditor.store)
end

#do_audit_processObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/models_auditor/audit.rb', line 31

def do_audit_process
  return unless ModelsAuditor.config.audit_enabled
  return unless self.class.instance_variable_get(:@audit_enabled)
  mode    = self.class.instance_variable_get(:@audit_mode)
  options = self.class.instance_variable_get(:@audit_settings) || {}
  store   = ModelsAuditor.store

  initial_data = ma_get_initial_state(store)
  current_data = ma_auditor_get_data

  action =
    case
      when transaction_include_any_action?([:create])
        ModelsAuditor::AuditRecord::ACTION_CREATE
      when transaction_include_any_action?([:update])
        ModelsAuditor::AuditRecord::ACTION_UPDATE
      when transaction_include_any_action?([:destroy])
        ModelsAuditor::AuditRecord::ACTION_DESTROY
    end

  bridge =
    if options[:bridge]
      options[:bridge].each_with_object({}) { |(key, model_name), o| o[key] = {model_name => __send__(key)} }
    end

  Thread.new do
    begin
      log_anyway = !ModelsAuditor.config.audit_request_changes_only
      if (request = store[:audit_request]) || log_anyway
        body =
          case
            when AUDIT_SNAPSHOT_MODES.include?(mode)
              ma_eliminate_not_changed_keys(initial_data, current_data)
            when AUDIT_CHANGES_MODES.include?(mode)
              current_data
            else
              raise ArgumentError.new('Incorrect value of argument audit_type')
          end

        if request.try(:new_record?) && !request.save
          ModelsAuditor.log_error("Couldn't save request record")
          ModelsAuditor.log_error(request.errors.full_messages)
          return
        end
        record =
          ModelsAuditor::AuditRecord.new(
            request:   request,
            auditable: self,
            content:   body,
            action:    action,
            bridge:    bridge
          )
        unless record.save
          ModelsAuditor.log_error("Couldn't logged changes of #{self.class.name} id: #{self.try(:id)}")
          ModelsAuditor.log_error(record.errors.full_messages)
        end
      end
    rescue StandardError => e
      ModelsAuditor.log_error("Couldn't logged changes of #{self.class.name} id: #{self.try(:id)}")
      ModelsAuditor.log_error(e.message)
      ModelsAuditor.log_error(e.backtrace.take(100).join("\n"))
    end
  end
end