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
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
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
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
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
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
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
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