Module: ModelsAuditor::Audit::InstanceMethods

Defined in:
lib/models_auditor/audit.rb

Instance Method Summary collapse

Instance Method Details

#do_audit_init_snapshotObject



54
55
56
57
58
59
60
# File 'lib/models_auditor/audit.rb', line 54

def do_audit_init_snapshot
  return unless ModelsAuditor.config.audit_enabled
  return unless is_audit_enabled?
  mode = get_audit_mode
  return unless AUDIT_SNAPSHOT_MODES.include?(mode)
  ma_store_initial_state(ModelsAuditor.store)
end

#do_audit_processObject



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/models_auditor/audit.rb', line 62

def do_audit_process
  return unless ModelsAuditor.config.audit_enabled
  return unless is_audit_enabled?
  mode    = get_audit_mode
  options = get_audit_options
  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

#get_audit_modeObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/models_auditor/audit.rb', line 30

def get_audit_mode
  mode = self.class.instance_variable_get(:@audit_mode)
  if mode.nil?
    self.class.column_names.include?(self.class.inheritance_column) &&
      self.class.superclass.instance_variable_get(:@audit_enabled) ?
      self.class.superclass.instance_variable_get(:@audit_mode) :
      nil
  else
    mode
  end
end

#get_audit_optionsObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/models_auditor/audit.rb', line 42

def get_audit_options
  settings = self.class.instance_variable_get(:@audit_settings)
  if settings.nil?
    self.class.column_names.include?(self.class.inheritance_column) &&
      self.class.superclass.instance_variable_get(:@audit_enabled) ?
      self.class.superclass.instance_variable_get(:@audit_settings) :
      {}
  else
    settings
  end
end

#is_audit_enabled?Boolean

Returns:

  • (Boolean)


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

def is_audit_enabled?
  self.class.instance_variable_get(:@audit_enabled) ||
    (self.class.column_names.include?(self.class.inheritance_column) &&
      self.class.superclass.instance_variable_get(:@audit_enabled))
end