Module: AuditEvents::CommonModel

Extended by:
ActiveSupport::Concern
Included in:
GroupAuditEvent, InstanceAuditEvent, ProjectAuditEvent, UserAuditEvent
Defined in:
app/models/concerns/audit_events/common_model.rb

Constant Summary collapse

PARALLEL_PERSISTENCE_COLUMNS =
[
  :author_name,
  :entity_path,
  :target_details,
  :target_type,
  :target_id
].freeze
TRUNCATED_FIELDS =
{
  entity_path: 5_500,
  target_details: 5_500
}.freeze

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



97
98
99
100
101
# File 'app/models/concerns/audit_events/common_model.rb', line 97

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

#authorObject



85
86
87
# File 'app/models/concerns/audit_events/common_model.rb', line 85

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

#author_nameObject



75
76
77
# File 'app/models/concerns/audit_events/common_model.rb', line 75

def author_name
  author&.name
end

#entity_pathObject



115
116
117
# File 'app/models/concerns/audit_events/common_model.rb', line 115

def entity_path
  super || details[:entity_path]
end

#formatted_detailsObject



79
80
81
82
83
# File 'app/models/concerns/audit_events/common_model.rb', line 79

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

#initialize_detailsObject



69
70
71
72
73
# File 'app/models/concerns/audit_events/common_model.rb', line 69

def initialize_details
  return unless has_attribute?(:details)

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

#ip_addressObject



119
120
121
# File 'app/models/concerns/audit_events/common_model.rb', line 119

def ip_address
  super&.to_s || details[:ip_address]
end

#lazy_authorObject



89
90
91
92
93
94
95
# File 'app/models/concerns/audit_events/common_model.rb', line 89

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



111
112
113
# File 'app/models/concerns/audit_events/common_model.rb', line 111

def target_details
  super || details[:target_details]
end

#target_idObject



107
108
109
# File 'app/models/concerns/audit_events/common_model.rb', line 107

def target_id
  details[:target_id]
end

#target_typeObject



103
104
105
# File 'app/models/concerns/audit_events/common_model.rb', line 103

def target_type
  super || details[:target_type]
end