Class: AuditEvent

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, BulkInsertSafe, CreatedAtFilterable, EachBatch, PartitionedTable
Defined in:
app/models/audit_event.rb

Constant Summary collapse

PARALLEL_PERSISTENCE_COLUMNS =
[
  :author_name,
  :entity_path,
  :target_details,
  :target_type,
  :target_id
].freeze

Constants included from BulkInsertSafe

BulkInsertSafe::ALLOWED_CALLBACKS, BulkInsertSafe::DEFAULT_BATCH_SIZE, BulkInsertSafe::MethodNotAllowedError, BulkInsertSafe::PrimaryKeySetError

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.order_by(method) ⇒ Object



48
49
50
51
52
53
54
55
# File 'app/models/audit_event.rb', line 48

def self.order_by(method)
  case method.to_s
  when 'created_asc'
    order(id: :asc)
  else
    order(id: :desc)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



85
86
87
88
89
# File 'app/models/audit_event.rb', line 85

def as_json(options = {})
  super(options).tap do |json|
    json['ip_address'] = ip_address.to_s
  end
end

#authorObject



73
74
75
# File 'app/models/audit_event.rb', line 73

def author
  lazy_author&.itself.presence || default_author_value
end

#author_nameObject



63
64
65
# File 'app/models/audit_event.rb', line 63

def author_name
  author&.name
end

#formatted_detailsObject



67
68
69
70
71
# File 'app/models/audit_event.rb', line 67

def formatted_details
  details
    .merge(details.slice(:from, :to).transform_values(&:to_s))
    .merge(author_email: author.try(:email))
end

#initialize_detailsObject



57
58
59
60
61
# File 'app/models/audit_event.rb', line 57

def initialize_details
  return unless has_attribute?(:details)

  self.details = {} if details&.nil?
end

#lazy_authorObject



77
78
79
80
81
82
83
# File 'app/models/audit_event.rb', line 77

def lazy_author
  BatchLoader.for(author_id).batch do |author_ids, loader|
    User.select(:id, :name, :username, :email).where(id: author_ids).find_each do |user|
      loader.call(user.id, user)
    end
  end
end

#target_detailsObject



99
100
101
# File 'app/models/audit_event.rb', line 99

def target_details
  super || details[:target_details]
end

#target_idObject



95
96
97
# File 'app/models/audit_event.rb', line 95

def target_id
  details[:target_id]
end

#target_typeObject



91
92
93
# File 'app/models/audit_event.rb', line 91

def target_type
  super || details[:target_type]
end