File: configurable.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 (60 lines) | stat: -rw-r--r-- 2,207 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

module ActiveRecord
  module Encryption
    # Configuration API for ActiveRecord::Encryption
    module Configurable
      extend ActiveSupport::Concern

      included do
        mattr_reader :config, default: Config.new
        mattr_accessor :encrypted_attribute_declaration_listeners
      end

      class_methods do
        # Expose getters for context properties
        Context::PROPERTIES.each do |name|
          delegate name, to: :context
        end

        def configure(primary_key: nil, deterministic_key: nil, key_derivation_salt: nil, **properties) # :nodoc:
          config.primary_key = primary_key
          config.deterministic_key = deterministic_key
          config.key_derivation_salt = key_derivation_salt

          # Set the default for this property here instead of in +Config#set_defaults+ as this needs
          # to happen *after* the keys have been set.
          properties[:support_sha1_for_non_deterministic_encryption] = true if properties[:support_sha1_for_non_deterministic_encryption].nil?

          properties.each do |name, value|
            ActiveRecord::Encryption.config.send "#{name}=", value if ActiveRecord::Encryption.config.respond_to?("#{name}=")
          end

          ActiveRecord::Encryption.reset_default_context

          properties.each do |name, value|
            ActiveRecord::Encryption.context.send "#{name}=", value if ActiveRecord::Encryption.context.respond_to?("#{name}=")
          end
        end

        # Register callback to be invoked when an encrypted attribute is declared.
        #
        # === Example
        #
        #   ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, attribute_name|
        #     ...
        #   end
        def on_encrypted_attribute_declared(&block)
          self.encrypted_attribute_declaration_listeners ||= Concurrent::Array.new
          self.encrypted_attribute_declaration_listeners << block
        end

        def encrypted_attribute_was_declared(klass, name) # :nodoc:
          self.encrypted_attribute_declaration_listeners&.each do |block|
            block.call(klass, name)
          end
        end
      end
    end
  end
end