Class: AuditEvent

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, BulkInsertSafe, CreatedAtFilterable, EachBatch, PartitionedTable
Defined in:
app/models/audit_event.rb

Constant Summary collapse

PARALLEL_PERSISTENCE_COLUMNS =
[
  :author_name,
  :entity_path,
  :target_details,
  :target_type,
  :target_id
].freeze

Constants included from BulkInsertSafe

BulkInsertSafe::ALLOWED_CALLBACKS, BulkInsertSafe::DEFAULT_BATCH_SIZE, BulkInsertSafe::MethodNotAllowedError, BulkInsertSafe::PrimaryKeySetError

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.order_by(method) ⇒ Object


48
49
50
51
52
53
54
55
# File 'app/models/audit_event.rb', line 48

def self.order_by(method)
  case method.to_s
  when 'created_asc'
    order(id: :asc)
  else
    order(id: :desc)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object


83
84
85
86
87
# File 'app/models/audit_event.rb', line 83

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

#authorObject


71
72
73
# File 'app/models/audit_event.rb', line 71

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

#author_nameObject


63
64
65
# File 'app/models/audit_event.rb', line 63

def author_name
  author&.name
end

#formatted_detailsObject


67
68
69
# File 'app/models/audit_event.rb', line 67

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

#initialize_detailsObject


57
58
59
60
61
# File 'app/models/audit_event.rb', line 57

def initialize_details
  return unless self.has_attribute?(:details)

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

#lazy_authorObject


75
76
77
78
79
80
81
# File 'app/models/audit_event.rb', line 75

def lazy_author
  BatchLoader.for(author_id).batch do |author_ids, loader|
    User.select(:id, :name, :username).where(id: author_ids).find_each do |user|
      loader.call(user.id, user)
    end
  end
end