Sha256: de1740266582857fad8634422d355fed4eb086c72ba88306535081424459eb10

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

# This is the payload of the log. It contains the encrypted data of one record in the DB.
# When the record is deleted, the encryption_key for the payload is deleted.
# Payloads comes in different flavors.
# The primary_payload is the payload that contains the parrent record.
# When fecthing the attrs, they are packed differently depending on the payload_type.

module LoggableActivity
  class Payload < ActiveRecord::Base
    self.table_name = 'loggable_payloads'
    belongs_to :record, polymorphic: true, optional: true

    belongs_to :activity
    belongs_to :record, polymorphic: true, optional: true
    validates :encrypted_attrs, presence: true
    enum payload_type: {
      primary_payload: 0,
      update_payload: 1,
      current_association: 2,
      previous_association: 3
    }

    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
        # when 'destroy_payload'
        #   destroy_payload_attrs
      end
    end

    def payload_encryption_key
      @payload_encryption_key ||= LoggableActivity::EncryptionKey.for_record(record)&.key
    end

    private

    def deleted_attrs
      encrypted_attrs.transform_values! { I18n.t('loggable.activity.deleted') }
    end

    def decrypted_update_attrs
      encrypted_attrs['changes'].map do |change|
        decrypted_from_to_attr(change)
      end
    end

    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

    def decrypted_attrs
      encrypted_attrs.each do |key, value|
        encrypted_attrs[key] = decrypt_attr(value)
      end
    end

    def decrypt_attr(value)
      LoggableActivity::Encryption.decrypt(value, payload_encryption_key)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
loggable_activity-0.1.39 lib/loggable_activity/payload.rb
loggable_activity-0.1.38b lib/loggable_activity/payload.rb
loggable_activity-0.1.36 lib/loggable_activity/payload.rb