Sha256: 640c969a3225917a2bb72c2e812e6ed07809a54ed1b9a622670e7437f7383bb8

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module BetterRecord
  class LoggedAction < Base
    # == Constants ============================================================
    ACTIONS = {
      D: 'DELETE',
      I: 'INSERT',
      U: 'UPDATE',
      T: 'TRUNCATE',
    }.with_indifferent_access

    # == Attributes ===========================================================
    self.table_name = "#{BetterRecord.db_audit_schema}.logged_actions"

    # == Extensions ===========================================================

    # == Relationships ========================================================
    belongs_to :audited,
      polymorphic: :true,
      primary_type: :table_name,
      foreign_key: :row_id,
      foreign_type: :table_name
    # == Validations ==========================================================

    # == Scopes ===============================================================

    # == Callbacks ============================================================

    # == Class Methods ========================================================
    def self.default_print
      [
        :event_id,
        :row_id,
        :table_name,
        :app_user_id,
        :app_user_type,
        :action_type,
        :changed_columns
      ]
    end

    # == Instance Methods =====================================================
    def changed_columns
      (self.changed_fields || {}).keys.join(', ').presence || 'N/A'
    end

    def action_type
      ACTIONS[action] || 'UNKNOWN'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
better_record-0.8.2 app/models/better_record/logged_action.rb
better_record-0.8.1 app/models/better_record/logged_action.rb