Add audit log entries for user roles (#19040)
* Refactor audit log schema * Add audit log entries for user roles
This commit is contained in:
parent
99aed9069d
commit
0396acf39e
27 changed files with 151 additions and 99 deletions
|
@ -9,38 +9,42 @@
|
|||
# action :string default(""), not null
|
||||
# target_type :string
|
||||
# target_id :bigint(8)
|
||||
# recorded_changes :text default(""), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# human_identifier :string
|
||||
# route_param :string
|
||||
# permalink :string
|
||||
#
|
||||
|
||||
class Admin::ActionLog < ApplicationRecord
|
||||
serialize :recorded_changes
|
||||
self.ignored_columns = %w(
|
||||
recorded_changes
|
||||
)
|
||||
|
||||
belongs_to :account
|
||||
belongs_to :target, polymorphic: true, optional: true
|
||||
|
||||
default_scope -> { order('id desc') }
|
||||
|
||||
before_validation :set_human_identifier
|
||||
before_validation :set_route_param
|
||||
before_validation :set_permalink
|
||||
|
||||
def action
|
||||
super.to_sym
|
||||
end
|
||||
|
||||
before_validation :set_changes
|
||||
|
||||
private
|
||||
|
||||
def set_changes
|
||||
case action
|
||||
when :destroy, :create
|
||||
self.recorded_changes = target.attributes
|
||||
when :update, :promote, :demote
|
||||
self.recorded_changes = target.previous_changes
|
||||
when :change_email
|
||||
self.recorded_changes = ActiveSupport::HashWithIndifferentAccess.new(
|
||||
email: [target.email, nil],
|
||||
unconfirmed_email: [nil, target.unconfirmed_email]
|
||||
)
|
||||
end
|
||||
def set_human_identifier
|
||||
self.human_identifier = target.to_log_human_identifier if target.respond_to?(:to_log_human_identifier)
|
||||
end
|
||||
|
||||
def set_route_param
|
||||
self.route_param = target.to_log_route_param if target.respond_to?(:to_log_route_param)
|
||||
end
|
||||
|
||||
def set_permalink
|
||||
self.permalink = target.to_log_permalink if target.respond_to?(:to_log_permalink)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue