File: encrypted_fixtures.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (38 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

module ActiveRecord
  module Encryption
    module EncryptedFixtures
      def initialize(fixture, model_class)
        @clean_values = {}
        encrypt_fixture_data(fixture, model_class)
        process_preserved_original_columns(fixture, model_class)
        super
      end

      private
        def encrypt_fixture_data(fixture, model_class)
          model_class&.encrypted_attributes&.each do |attribute_name|
            if clean_value = fixture[attribute_name.to_s]
              @clean_values[attribute_name.to_s] = clean_value

              type = model_class.type_for_attribute(attribute_name)
              encrypted_value = type.serialize(clean_value)
              fixture[attribute_name.to_s] = encrypted_value
            end
          end
        end

        def process_preserved_original_columns(fixture, model_class)
          model_class&.encrypted_attributes&.each do |attribute_name|
            if source_attribute_name = model_class.source_attribute_from_preserved_attribute(attribute_name)
              clean_value = @clean_values[source_attribute_name.to_s]
              type = model_class.type_for_attribute(attribute_name)
              encrypted_value = type.serialize(clean_value)
              fixture[attribute_name.to_s] = encrypted_value
            end
          end
        end
    end
  end
end