Class: Decidim::Traceability

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

Overview

This class wraps the logic to trace resource changes and their authorship. It is expected to be used with classes implementing the ‘Decidim::Traceable` concern. Version authors can be retrieved using the methods in `Decidim::TraceabilityHelper`.

Examples:

# consider MyResource implements Decidim::Traceable
resource = Decidim::Traceability.new.create!(MyResource, author, params)
resource.versions.count # => 1
resource.versions.last.whodunnit # => author.to_gid.to_s
resource.versions.last.event # => "create"
resource = Decidim::Traceability.new.update!(resource, author, params)
resource.versions.count # => 2
resource.versions.last.event # => "update"

This class uses the ‘paper_trail` gem internally, so refer to its documentation for further info on how to interact with versions.

Instance Method Summary collapse

Instance Method Details

#create(klass, author, params) ⇒ Object

Calls the ‘create` method to the given class and sets the author of the version.

klass - An ActiveRecord class that implements ‘Decidim::Traceable` author - An object that implements `to_gid` or a String params - a Hash

Returns an instance of ‘klass`.



30
31
32
33
34
# File 'app/services/decidim/traceability.rb', line 30

def create(klass, author, params)
  PaperTrail.whodunnit(gid(author)) do
    klass.create(params)
  end
end

#create!(klass, author, params) ⇒ Object

Calls the ‘create!` method to the given class and sets the author of the version.

klass - An ActiveRecord class that implements ‘Decidim::Traceable` author - An object that implements `to_gid` or a String params - a Hash

Returns an instance of ‘klass`.



43
44
45
46
47
# File 'app/services/decidim/traceability.rb', line 43

def create!(klass, author, params)
  PaperTrail.whodunnit(gid(author)) do
    klass.create!(params)
  end
end

#last_editor(resource) ⇒ Object

Finds the author of the last version of the resource.

resource - an object implementing ‘Decidim::Traceable`

Returns an object identifiable via GlobalID or a String.



68
69
70
# File 'app/services/decidim/traceability.rb', line 68

def last_editor(resource)
  version_editor(resource.versions.last)
end

#update!(resource, author, params) ⇒ Object

Updates the ‘resource` with `update_attributes!` and sets the author of the version.

resource - An ActiveRecord instance that implements ‘Decidim::Traceable` author - An object that implements `to_gid` or a String params - a Hash

Returns the updated ‘resource`.



56
57
58
59
60
61
# File 'app/services/decidim/traceability.rb', line 56

def update!(resource, author, params)
  PaperTrail.whodunnit(gid(author)) do
    resource.update_attributes!(params)
    resource
  end
end

#version_editor(version) ⇒ Object

Finds the author of the given version.

version - an object that responds to ‘whodunnit` and returns a String.

Returns an object identifiable via GlobalID or a String.



77
78
79
# File 'app/services/decidim/traceability.rb', line 77

def version_editor(version)
  ::GlobalID::Locator.locate(version.whodunnit) || version.whodunnit
end