class LoggableActivity::Payload

This class represents a payload in the log, containing encrypted data of one record in the database. When the record is deleted, the encryption key for the payload is also deleted. Payloads come in different types, each serving a specific purpose.

Public Instance Methods

attrs() click to toggle source

Returns the decrypted attributes of the payload based on its type.

@return [Hash] The decrypted attributes.

Example:

payload.attrs

Returns:
{
         "street" => "Machu Picchu",
           "city" => "Aguas Calientes",
        "country" => "Peru",
    "postal_code" => "08680"
}
# File lib/loggable_activity/payload.rb, line 42
def attrs
  return deleted_attrs if record.nil?

  case payload_type
  when 'current_association', 'primary_payload', 'previous_association'
    decrypted_attrs
  when 'update_payload'
    decrypted_update_attrs
  end
end

Private Instance Methods

decrypt_attr(value) click to toggle source

Decrypts a single attribute.

@param value [String] The encrypted value to decrypt. @return [String] The decrypted value.

# File lib/loggable_activity/payload.rb, line 103
def decrypt_attr(value)
  LoggableActivity::Encryption.decrypt(value, payload_encryption_key)
end
decrypted_attrs() click to toggle source

Decrypts all attributes.

@return [Hash] The decrypted attributes.

# File lib/loggable_activity/payload.rb, line 93
def decrypted_attrs
  encrypted_attrs.each do |key, value|
    encrypted_attrs[key] = decrypt_attr(value)
  end
end
decrypted_from_to_attr(change) click to toggle source

Decrypts ‘from’ and ‘to’ attributes.

@param change [Hash] The change hash containing ‘from’ and ‘to’ values. @return [Hash] The decrypted ‘from’ and ‘to’ values.

# File lib/loggable_activity/payload.rb, line 82
def decrypted_from_to_attr(change)
  change.to_h do |key, value|
    from = decrypt_attr(value['from'])
    to = decrypt_attr(value['to'])
    [key, { from:, to: }]
  end
end
decrypted_update_attrs() click to toggle source

Decrypts the ‘from’ and ‘to’ attributes in the update payload.

@return [Array<Hash>] The array of decrypted changes.

# File lib/loggable_activity/payload.rb, line 72
def decrypted_update_attrs
  encrypted_attrs['changes'].map do |change|
    decrypted_from_to_attr(change)
  end
end
deleted_attrs() click to toggle source

Helper method to handle deleted attributes.

@return [Hash] The hash with deleted attributes.

# File lib/loggable_activity/payload.rb, line 65
def deleted_attrs
  encrypted_attrs.transform_values! { I18n.t('loggable.activity.deleted') }
end
payload_encryption_key() click to toggle source

Retrieves the encryption key for the payload.

@return [String, nil] The encryption key.

# File lib/loggable_activity/payload.rb, line 58
def payload_encryption_key
  @payload_encryption_key ||= LoggableActivity::EncryptionKey.for_record(record)&.key
end