Class: Decidim::ActionLogger

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/action_logger.rb

Overview

Use this class to log actions by any user. You probably shouldn’t use this class dfirectly, but rather use ‘Decidim.traceability` instead. Check the docs on `Decidim::Traceability` for more info.

Usage:

ActionLogger.log(:create, user, new_proposal, extra_data)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, user, resource, version_id = nil, resource_extra = {}) ⇒ ActionLogger

Public: Initializes the instance.

action - a String representing the name of the action user - the Decidim::User that performed the action resource - the resource onn which the action was performed version_id - the ID of the ‘PaperTrail::Version` that was created on that action resource_extra - a Hash with resource_extra info to be recorded



33
34
35
36
37
38
39
40
# File 'app/services/decidim/action_logger.rb', line 33

def initialize(action, user, resource, version_id = nil, resource_extra = {})
  @action = action
  @user = user
  @resource = resource
  @version_id = version_id
  @resource_extra = resource_extra
  @visibility = resource_extra.delete(:visibility).presence || "admin-only"
end

Class Method Details

.log(action, user, resource, version_id, resource_extra = {}) ⇒ Object

Public: Logs the given ‘action` by the given `user` on the given `resource`. Delegates the work to the instance method.

action - a String representing the name of the action user - the Decidim::User that performed the action resource - the resource onn which the action was performed version_id - the ID of the ‘PaperTrail::Version` that was created on that action resource_extra - a Hash with resource_extra info to be recorded

Returns the newly created ‘Decidim::ActionLog` resource.



22
23
24
# File 'app/services/decidim/action_logger.rb', line 22

def self.log(action, user, resource, version_id, resource_extra = {})
  new(action, user, resource, version_id, resource_extra).log!
end

Instance Method Details

#log!Object

Public: Logs the given ‘action` by the given `user` on the given `resource`.

Returns the newly created ‘Decidim::ActionLog` resource.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/decidim/action_logger.rb', line 46

def log!
  Decidim::ActionLog.create!(
    user: user,
    organization: organization,
    action: action,
    resource: resource,
    resource_id: resource.id,
    resource_type: resource.class.name,
    participatory_space: participatory_space,
    component: component,
    area: area,
    scope: scope,
    version_id: version_id,
    extra: extra_data,
    visibility: visibility
  )
end