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.
Constants
- DECRYPT_ATTRS_TYPES
Enumeration for different payload types
- DECRYPT_UPDATE_ATTRS_TYPES
Enumeration for different updatepayload types
Public Instance Methods
Returns the decrypted attrs.
@return [Hash] The decrypted attributes.
Example:
payload.attrs Returns: { "street" => "Machu Picchu", "city" => "Cusco" }
# File lib/loggable_activity/payload.rb, line 64 def attrs return deleted_attrs if record.nil? case related_to_activity_as when *DECRYPT_ATTRS_TYPES decrypted_attrs when *DECRYPT_UPDATE_ATTRS_TYPES decrypted_update_attrs else {} end end
Check if the record has been deleted.
@return [Boolean] True if the record has been deleted.
# File lib/loggable_activity/payload.rb, line 104 def deleted? encryption_key.deleted? end
Returns the route for the payload unless the encryption_key is deleted.
Example:
payload.payload_route
@return [String] The route for the payload.
# File lib/loggable_activity/payload.rb, line 83 def payload_route return '' if deleted? route end
Returns the display name for the record.
Example:
payload.record_display_name
@return [String] The display name for the record.
# File lib/loggable_activity/payload.rb, line 95 def record_display_name return I18n.t('loggable.activity.deleted') if deleted? ::LoggableActivity::Encryption.decrypt(encrypted_record_name, secret_key) 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 159 def decrypt_attr(value) # return 'loggable_activity.attr.deleted' if secret_key.nil? ::LoggableActivity::Encryption.decrypt(value, secret_key) end
Decrypts all attributes.
@return [Hash] The decrypted attributes.
# File lib/loggable_activity/payload.rb, line 149 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 138 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 127 def decrypted_update_attrs # return encrypted_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 120 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 113 def secret_key encryption_key.secret_key end