From 333775b767cc18f22a283cb0631dc82b2227cb82 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Mon, 15 Oct 2018 04:25:33 +0100 Subject: [PATCH] Use a lazy load hook to configure Active Record Rails provides hooks to configure its core classes without loading them: https://api.rubyonrails.org/v5.2.0/classes/ActiveSupport/LazyLoadHooks.html Referencing `ActiveRecord::Base` directly was forcing it to load before the application had finished initializing, which can cause configuration to be applied incorrectly. --- lib/attr_encrypted/adapters/active_record.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/attr_encrypted/adapters/active_record.rb b/lib/attr_encrypted/adapters/active_record.rb index a22108e4..57c6337a 100644 --- a/lib/attr_encrypted/adapters/active_record.rb +++ b/lib/attr_encrypted/adapters/active_record.rb @@ -134,6 +134,8 @@ def method_missing_with_attr_encrypted(method, *args, &block) end end - ActiveRecord::Base.extend AttrEncrypted - ActiveRecord::Base.extend AttrEncrypted::Adapters::ActiveRecord + ActiveSupport.on_load(:active_record) do + extend AttrEncrypted + extend AttrEncrypted::Adapters::ActiveRecord + end end