about summary refs log tree commit diff
path: root/app/models/admin/action_log.rb
blob: 4fa8008f5c0bff8c90b3819a69cffbb593abe8d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

# == Schema Information
#
# Table name: admin_action_logs
#
#  id               :bigint(8)        not null, primary key
#  account_id       :bigint(8)
#  action           :string           default(""), not null
#  target_type      :string
#  target_id        :bigint(8)
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  human_identifier :string
#  route_param      :string
#  permalink        :string
#

class Admin::ActionLog < ApplicationRecord
  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

  private

  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