Events
Events in Project Forum are used to keep a track record of who did what (log) and as a way to create Notifications (a notification requires an event).
An event consists of a user
performing an action
on a resource
, with optionally message_parameters associated with it. In addition, additional resources can be associate
d with an event.
Actions
Any string (symbol) can be used to specify the action. The resource and the action together should provide enough information to determine what the event represents. (This is currently not consistently done in the codebase, see #230).
Often actions correspond to controller actions (i.e. create/update/destroy).
Using events
The concern Notifyable
can be included to get access to the log_event
and log_event_with_user
methods. The log_event
method is intended to be used from controllers, while the log_event_with_user
can be used from any class.
For example, from a controller you may see the following:
class SomeController < ApplicationController
include Notifyable
...
def someMethod
log_event(
@something,
:create,
{ something_else: 'Store this for use in the message under a specific key' }
)
end
end
log_event
Logs a new event on the given resource of the given action triggered by the current_user.
This method is intended for use from controllers. See
log_event_with_user
for usage outside controllers.
- @param resource [ApplicationRecord] the resource on which the event occurred. If not specified, resource_type is instead set to
controller_name
(i.e.ProjectsController
->project
) - @param action [Symbol, String] the action which occurred on resource. If not specified, the invoked action of the controller is used (
action_name
, i.e. index/show/new/edit/destroy/create). - @param message_parameters [Hash] parameters related to the event (specifically the message)
- @return [Event]
def log_event(resource = nil, action = nil, message_parameters = {})
log_event_with_user
Logs a new event on the given resource of the given action triggered by the given user.
- @param resource [ApplicationRecord, String] the resource or resource_type on which the action occurred
- @param action [Symbol, String] the action which occurred on resource
- @param user [User] the user who triggered the event
- @param message_parameters [Hash] parameters related to the event (specifically the message)
- @return [Event]
def log_event_with_user(resource, action, user, message_parameters = {})