Class: AuditLog

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/audit_log.rb

Overview

create_table :audit_logs do |t|

t.string :application
t.string :description
t.integer :party_id
t.text :additional_info
t.references :audit_log_type

#polymorphic columns
t.references :event_record, :polymorphic => true

t.timestamps

end add_index :audit_logs, :party_id add_index :audit_logs, [:event_record_id, :event_record_type], :name => ‘event_record_index’ add_index :audit_logs, :audit_log_type_id, :name => ‘audit_logs_audit_log_type_id_idx’

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

allow items to be looked up by method calls



151
152
153
154
155
156
157
158
# File 'app/models/audit_log.rb', line 151

def method_missing(m, *args, &block)
  if self.respond_to?(m)
    item = get_item_by_item_type_internal_identifier(m.to_s)
    (item.nil?) ? super : (return item.audit_log_item_value)
  else
    super
  end
end

Class Method Details

.apply_filters(filters, statement = nil) ⇒ ActiveRecord::Relation

Filter records

Parameters:

  • filters (Hash)

    a hash of filters to be applied,

  • statement (ActiveRecord::Relation) (defaults to: nil)

    the query being built

Returns:

  • (ActiveRecord::Relation)

    the query being built



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
# File 'app/models/audit_log.rb', line 41

def apply_filters(filters, statement=nil)
  audit_log_tbl = self.arel_table

  unless statement
    statement = self
  end

  # filter by tenant
  unless filters[:tenant].blank?
    statement = statement.by_tenant(filters[:tenant])
  end

  # filter by logged by
  unless filters[:logged_by].blank?
    statement = statement.where(party_id: filters[:logged_by].split(','))
  end

  # filter by start_at
  unless filters[:start_date].blank?
    statement = statement.where(audit_log_tbl[:created_at].gteq(filters[:start_date]))
  end

  # filter by end_at
  unless filters[:end_date].blank?
    statement = statement.where(audit_log_tbl[:created_at].lteq(filters[:end_date]))
  end

  statement
end

.custom_application_log_message(party, msg) ⇒ Object



71
72
73
74
75
76
77
# File 'app/models/audit_log.rb', line 71

def custom_application_log_message(party, msg)
  self.create(
      :party_id => party.id,
      :audit_log_type => AuditLogType.find_by_type_and_subtype_iid('application', 'custom_message'),
      :description => "#{party.description}: #{msg}"
  )
end

.party_access(party, url) ⇒ Object



95
96
97
98
99
100
101
# File 'app/models/audit_log.rb', line 95

def party_access(party, url)
  self.create(
      :party_id => party.id,
      :audit_log_type => AuditLogType.find_by_type_and_subtype_iid('application', 'accessed_area'),
      :description => "#{party.description} has accessed area #{url}"
  )
end

.party_failed_access(party, url) ⇒ Object



103
104
105
106
107
108
109
# File 'app/models/audit_log.rb', line 103

def party_failed_access(party, url)
  self.create(
      :party_id => party.id,
      :audit_log_type => AuditLogType.find_by_type_and_subtype_iid('application', 'accessed_area'),
      :description => "#{party.description} has tried to access a restricted area #{url}"
  )
end

.party_login(party) ⇒ Object



87
88
89
90
91
92
93
# File 'app/models/audit_log.rb', line 87

def (party)
  self.create(
      :party_id => party.id,
      :audit_log_type => AuditLogType.find_by_type_and_subtype_iid('application', 'successful_login'),
      :description => "#{party.description} has logged in"
  )
end

.party_logout(party) ⇒ Object



79
80
81
82
83
84
85
# File 'app/models/audit_log.rb', line 79

def party_logout(party)
  self.create(
      :party_id => party.id,
      :audit_log_type => AuditLogType.find_by_type_and_subtype_iid('application', 'successful_logout'),
      :description => "#{party.description} has logged out"
  )
end

.party_session_timeout(party) ⇒ Object



111
112
113
114
115
116
117
# File 'app/models/audit_log.rb', line 111

def party_session_timeout(party)
  self.create(
      :party_id => party.id,
      :audit_log_type => AuditLogType.find_by_type_and_subtype_iid('application', 'session_timeout'),
      :description => "#{party.description} session has expired"
  )
end

Instance Method Details

#get_item_by_item_type_internal_identifier(item_type_internal_identifier) ⇒ Object



120
121
122
123
# File 'app/models/audit_log.rb', line 120

def get_item_by_item_type_internal_identifier(item_type_internal_identifier)
  self.items.includes(:audit_log_item_type)
      .where(:audit_log_item_types => {:internal_identifier => item_type_internal_identifier}).first
end

#respond_to?(m, include_private_methods = false) ⇒ Boolean

allow items to be looked up by method calls

Returns:

  • (Boolean)


146
147
148
# File 'app/models/audit_log.rb', line 146

def respond_to?(m, include_private_methods = false)
  (super ? true : get_item_by_item_type_internal_identifier(m.to_s)) rescue super
end

#to_data_hashObject

convert to hash of data



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/audit_log.rb', line 127

def to_data_hash
  data = to_hash(only: [:id, :application, :additional_info, :description, :created_at])

  data[:party] = party.to_data_hash

  if event_record.respond_to?(:to_data_hash)
    data[:event_record] = event_record.to_data_hash
  else
    data[:event_record] = {id: event_record.id, type: event_record.class.name}
  end

  if type
    data[:audit_log_type] = audit_log_type.to_data_hash
  end

  data
end