2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class AuditEvent < ApplicationRecord
|
2019-09-26 09:06:04 +00:00
|
|
|
include CreatedAtFilterable
|
2020-06-11 12:08:54 +00:00
|
|
|
include IgnorableColumns
|
|
|
|
|
|
|
|
ignore_column :updated_at, remove_with: '13.3', remove_after: '2020-08-22'
|
2019-09-26 09:06:04 +00:00
|
|
|
|
2017-07-03 14:01:41 +00:00
|
|
|
serialize :details, Hash # rubocop:disable Cop/ActiveRecordSerialize
|
2015-07-03 11:54:50 +00:00
|
|
|
|
|
|
|
belongs_to :user, foreign_key: :author_id
|
|
|
|
|
|
|
|
validates :author_id, presence: true
|
|
|
|
validates :entity_id, presence: true
|
|
|
|
validates :entity_type, presence: true
|
|
|
|
|
2019-09-26 09:06:04 +00:00
|
|
|
scope :by_entity_type, -> (entity_type) { where(entity_type: entity_type) }
|
|
|
|
scope :by_entity_id, -> (entity_id) { where(entity_id: entity_id) }
|
|
|
|
|
2015-07-03 11:54:50 +00:00
|
|
|
after_initialize :initialize_details
|
|
|
|
|
2020-03-18 18:09:35 +00:00
|
|
|
def self.order_by(method)
|
|
|
|
case method.to_s
|
|
|
|
when 'created_asc'
|
|
|
|
order(id: :asc)
|
|
|
|
else
|
|
|
|
order(id: :desc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-03 11:54:50 +00:00
|
|
|
def initialize_details
|
|
|
|
self.details = {} if details.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
2020-04-03 12:09:52 +00:00
|
|
|
lazy_author.name
|
2015-07-03 11:54:50 +00:00
|
|
|
end
|
2019-09-26 09:06:04 +00:00
|
|
|
|
|
|
|
def formatted_details
|
|
|
|
details.merge(details.slice(:from, :to).transform_values(&:to_s))
|
|
|
|
end
|
2020-04-03 12:09:52 +00:00
|
|
|
|
|
|
|
def lazy_author
|
|
|
|
BatchLoader.for(author_id).batch(default_value: default_author_value) do |author_ids, loader|
|
|
|
|
User.where(id: author_ids).find_each do |user|
|
|
|
|
loader.call(user.id, user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def default_author_value
|
|
|
|
::Gitlab::Audit::NullAuthor.for(author_id, details[:author_name])
|
|
|
|
end
|
2015-07-03 11:54:50 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
AuditEvent.prepend_if_ee('EE::AuditEvent')
|