Class: Activity::EventLog

Inherits:
ApplicationRecord show all
Defined in:
app/models/activity/event_log.rb

Constant Summary collapse

SUPPORTED_FILTERS =

Supported field names and scopes for each field

[:user_id, :attached_id, :model, :event, :created_at, :updated_at]

Class Method Summary collapse

Class Method Details

.create_log(log) ⇒ Object



28
29
30
31
32
33
34
35
# File 'app/models/activity/event_log.rb', line 28

def self.create_log(log)
  self.create!(
    user_id: log[:user_id],
    attached_id: log[:attached_id],
    model: log[:model],
    event: log[:event],
  )
end

.filter(page, per_page, order_by, attributes) ⇒ Object

Filter that finds all records that match a given request Example Request: attributes = 1, event: “created_user”

  • Will return any records where attached_id = 1 and event = “created_user”

  • Allows for simple searching with many unique cases



16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/activity/event_log.rb', line 16

def self.filter(page, per_page, order_by, attributes)
  logs = attributes.slice(*SUPPORTED_FILTERS).reduce(all) do |scope, (key, value)|
    value.present? ? scope.send(key, value) : scope
  end

  {
    current_page: page,
    total_pages: logs.page(page).per(per_page).total_pages,
    logs: logs.order(order_by).page(page).per(per_page),
  }
end