module Decidim::Fingerprintable

This module adds support functionality to be able to generate a unique fingerprint from a model, given some fields. Its goal is to provide a way to give an informal “receipt” to a user to they can detect tampering.

Public Instance Methods

fingerprint(fields: nil, &block) click to toggle source

Public: Configures fingerprinting for this model.

fields - An `Array` of `symbols` specifying the fields that will be part of

the fingerprint generation.

block - (optional) When provided, it's given an instance of the model as a

parameter so the fingerprint can be generated in runtime.

Returns nothing.

# File lib/decidim/fingerprintable.rb, line 24
def fingerprint(fields: nil, &block)
  @fingerprint_options = {}

  if block_given?
    @fingerprint_options[:block] = block
  else
    raise "You must provide a set of fields to generate the fingerprint." unless fields

    @fingerprint_options[:fields] = fields
  end
end

Private Instance Methods

fingerprint_data() click to toggle source
# File lib/decidim/fingerprintable.rb, line 50
def fingerprint_data
  options = self.class.fingerprint_options

  if options[:block]
    options[:block].call(self)
  elsif options[:fields]
    options[:fields].each_with_object({}) do |field, result|
      result[field] = send(field)
    end
  else
    raise "Fingerprinting needs to be set up via the `fingerprint` class method."
  end
end