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
# 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

  begin
    if (request_data = store[:audit_request_data]).present?
      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

      request_data[:records_attributes] << {
        object_id:    self.id,
        object_type:  self.class.name,
        content:      body,
        action:       action,
        bridge:       bridge
      }
    end
  rescue StandardError => e
    ModelsAuditor.log_error("Couldn't generate log 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

#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