BigW Consortium Gitlab

attr_encrypted_no_db_connection.rb 830 Bytes
Newer Older
1 2 3
module AttrEncrypted
  module Adapters
    module ActiveRecord
Douwe Maan committed
4
      def attribute_instance_methods_as_symbols_with_no_db_connection
Douwe Maan committed
5 6 7 8
        # Use with_connection so the connection doesn't stay pinned to the thread.
        connected = ::ActiveRecord::Base.connection_pool.with_connection(&:active?) rescue false
        
        if connected
Douwe Maan committed
9 10
          # Call version from AttrEncrypted::Adapters::ActiveRecord
          attribute_instance_methods_as_symbols_without_no_db_connection
11
        else
Douwe Maan committed
12
          # Call version from AttrEncrypted, i.e., `super` with regards to AttrEncrypted::Adapters::ActiveRecord
Douwe Maan committed
13
          AttrEncrypted.instance_method(:attribute_instance_methods_as_symbols).bind(self).call
14 15 16
        end
      end

Douwe Maan committed
17
      alias_method_chain :attribute_instance_methods_as_symbols, :no_db_connection
18 19 20
    end
  end
end