generated from BCDevOps/opendev-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaudit_log.rb
More file actions
33 lines (28 loc) · 1017 Bytes
/
audit_log.rb
File metadata and controls
33 lines (28 loc) · 1017 Bytes
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
class AuditLog < ApplicationRecord
belongs_to :user, optional: true
validates :table_name, presence: true
validates :action, presence: true
# Basic scopes that use our indexes
scope :by_user, ->(user) { where(user: user) }
scope :by_table, ->(table_name) { where(table_name: table_name) }
scope :by_action, ->(action) { where(action: action) }
scope :recent, -> { order(created_at: :desc) }
# Advanced scopes that leverage composite indexes
scope :for_table_since,
->(table_name, date) do
where(table_name: table_name).where("created_at >= ?", date).order(
:created_at
)
end
scope :by_user_since,
->(user, date) do
where(user: user).where("created_at >= ?", date).order(:created_at)
end
# Convenience methods for common queries
def self.recent_changes(limit = 100)
recent.limit(limit).includes(:user)
end
def self.user_activity(user, days_back = 30)
by_user_since(user, days_back.days.ago)
end
end