action_log.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: admin_action_logs
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # action :string default(""), not null
  9. # target_type :string
  10. # target_id :bigint(8)
  11. # recorded_changes :text default(""), not null
  12. # created_at :datetime not null
  13. # updated_at :datetime not null
  14. #
  15. class Admin::ActionLog < ApplicationRecord
  16. serialize :recorded_changes
  17. belongs_to :account
  18. belongs_to :target, polymorphic: true
  19. default_scope -> { order('id desc') }
  20. def action
  21. super.to_sym
  22. end
  23. before_validation :set_changes
  24. private
  25. def set_changes
  26. case action
  27. when :destroy, :create
  28. self.recorded_changes = target.attributes
  29. when :update, :promote, :demote
  30. self.recorded_changes = target.previous_changes
  31. when :change_email
  32. self.recorded_changes = ActiveSupport::HashWithIndifferentAccess.new(
  33. email: [target.email, nil],
  34. unconfirmed_email: [nil, target.unconfirmed_email]
  35. )
  36. end
  37. end
  38. end