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 HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::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, nullable_column?, 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 ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.order_by(method) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/models/audit_event.rb', line 52

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

.supported_keyset_orderingsObject



48
49
50
# File 'app/models/audit_event.rb', line 48

def self.supported_keyset_orderings
  { id: [:desc] }
end

Instance Method Details

#as_json(options = {}) ⇒ Object



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

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

#authorObject



77
78
79
# File 'app/models/audit_event.rb', line 77

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

#author_nameObject



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

def author_name
  author&.name
end

#formatted_detailsObject



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

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

#initialize_detailsObject



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

def initialize_details
  return unless has_attribute?(:details)

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

#lazy_authorObject



81
82
83
84
85
86
87
# File 'app/models/audit_event.rb', line 81

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



103
104
105
# File 'app/models/audit_event.rb', line 103

def target_details
  super || details[:target_details]
end

#target_idObject



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

def target_id
  details[:target_id]
end

#target_typeObject



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

def target_type
  super || details[:target_type]
end