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



108
109
110
111
112
# File 'app/models/concerns/audit_events/common_model.rb', line 108

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

#authorObject



96
97
98
# File 'app/models/concerns/audit_events/common_model.rb', line 96

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

#author_nameObject



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

def author_name
  author&.name
end

#entity_pathObject



126
127
128
# File 'app/models/concerns/audit_events/common_model.rb', line 126

def entity_path
  super || details[:entity_path]
end

#formatted_detailsObject



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

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

#initialize_detailsObject



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

def initialize_details
  return unless has_attribute?(:details)

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

#ip_addressObject



130
131
132
# File 'app/models/concerns/audit_events/common_model.rb', line 130

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

#lazy_authorObject



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

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



122
123
124
# File 'app/models/concerns/audit_events/common_model.rb', line 122

def target_details
  super || details[:target_details]
end

#target_idObject



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

def target_id
  details[:target_id]
end

#target_typeObject



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

def target_type
  super || details[:target_type]
end