debian-mirror-gitlab/app/models/audit_event.rb

56 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class AuditEvent < ApplicationRecord
2019-12-21 20:55:43 +05:30
include CreatedAtFilterable
2017-09-10 17:25:29 +05:30
serialize :details, Hash # rubocop:disable Cop/ActiveRecordSerialize
2015-09-11 14:41:01 +05:30
belongs_to :user, foreign_key: :author_id
validates :author_id, presence: true
validates :entity_id, presence: true
validates :entity_type, presence: true
2019-12-21 20:55:43 +05:30
scope :by_entity_type, -> (entity_type) { where(entity_type: entity_type) }
scope :by_entity_id, -> (entity_id) { where(entity_id: entity_id) }
2015-09-11 14:41:01 +05:30
after_initialize :initialize_details
2020-04-22 19:07:51 +05:30
def self.order_by(method)
case method.to_s
when 'created_asc'
order(id: :asc)
else
order(id: :desc)
end
end
2015-09-11 14:41:01 +05:30
def initialize_details
self.details = {} if details.nil?
end
def author_name
2020-04-22 19:07:51 +05:30
lazy_author.name
2015-09-11 14:41:01 +05:30
end
2019-12-21 20:55:43 +05:30
def formatted_details
details.merge(details.slice(:from, :to).transform_values(&:to_s))
end
2020-04-22 19:07:51 +05:30
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-09-11 14:41:01 +05:30
end
2019-12-04 20:38:33 +05:30
AuditEvent.prepend_if_ee('EE::AuditEvent')