Sha256: cbe30562bf369995b1dcdea51eb793b8df558ba9b284bbf8848b49c7c10ac14b

Contents?: true

Size: 1.86 KB

Versions: 41

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module Encryption
    class AutoFilteredParameters
      def initialize(app)
        @app = app
        @attributes_by_class = Concurrent::Map.new
        @collecting = true

        install_collecting_hook
      end

      def enable
        apply_collected_attributes
        @collecting = false
      end

      private
        attr_reader :app

        def install_collecting_hook
          ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, attribute|
            attribute_was_declared(klass, attribute)
          end
        end

        def attribute_was_declared(klass, attribute)
          if collecting?
            collect_for_later(klass, attribute)
          else
            apply_filter(klass, attribute)
          end
        end

        def apply_collected_attributes
          @attributes_by_class.each do |klass, attributes|
            attributes.each do |attribute|
              apply_filter(klass, attribute)
            end
          end
        end

        def collecting?
          @collecting
        end

        def collect_for_later(klass, attribute)
          @attributes_by_class[klass] ||= Concurrent::Array.new
          @attributes_by_class[klass] << attribute
        end

        def apply_filter(klass, attribute)
          filter = [("#{klass.model_name.element}" if klass.name), attribute.to_s].compact.join(".")
          unless excluded_from_filter_parameters?(filter)
            app.config.filter_parameters << filter unless app.config.filter_parameters.include?(filter)
            klass.filter_attributes += [ attribute ]
          end
        end

        def excluded_from_filter_parameters?(filter_parameter)
          ActiveRecord::Encryption.config.excluded_from_filter_parameters.find { |excluded_filter| excluded_filter.to_s == filter_parameter }
        end
    end
  end
end

Version data entries

41 entries across 41 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-8.0.1 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-8.0.0.1 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.2.2.1 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.1.5.1 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-8.0.0 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.2.2 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.1.5 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-8.0.0.rc2 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.2.1.2 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.1.4.2 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-8.0.0.rc1 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.2.1.1 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-7.1.4.1 lib/active_record/encryption/auto_filtered_parameters.rb
activerecord-8.0.0.beta1 lib/active_record/encryption/auto_filtered_parameters.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/encryption/auto_filtered_parameters.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/encryption/auto_filtered_parameters.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/encryption/auto_filtered_parameters.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/encryption/auto_filtered_parameters.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/encryption/auto_filtered_parameters.rb