File tree Expand file tree Collapse file tree 5 files changed +46
-2
lines changed
railties/test/application Expand file tree Collapse file tree 5 files changed +46
-2
lines changed Original file line number Diff line number Diff line change
1
+ * Ensure ` ActiveRecord::Encryption.config ` is always ready before access.
2
+
3
+ Previously, ` ActiveRecord::Encryption ` configuration was deferred until ` ActiveRecord::Base `
4
+ was loaded. Therefore, accessing ` ActiveRecord::Encryption.config ` properties before
5
+ ` ActiveRecord::Base ` was loaded would give incorrect results.
6
+
7
+ ` ActiveRecord::Encryption ` now has its own loading hook so that its configuration is set as
8
+ soon as needed.
9
+
10
+ When ` ActiveRecord::Base ` is loaded, even lazily, it in turn triggers the loading of
11
+ ` ActiveRecord::Encryption ` , thus preserving the original behavior of having its config ready
12
+ before any use of ` ActiveRecord::Base ` .
13
+
14
+ * Maxime Réty*
15
+
1
16
* Add ` TimeZoneConverter#== ` method, so objects will be properly compared by
2
17
their type, scale, limit & precision.
3
18
Original file line number Diff line number Diff line change @@ -53,4 +53,6 @@ def self.eager_load!
53
53
Cipher . eager_load!
54
54
end
55
55
end
56
+
57
+ ActiveSupport . run_load_hooks :active_record_encryption , Encryption
56
58
end
Original file line number Diff line number Diff line change @@ -356,16 +356,19 @@ class Railtie < Rails::Railtie # :nodoc:
356
356
end
357
357
358
358
initializer "active_record_encryption.configuration" do |app |
359
- ActiveSupport . on_load ( :active_record ) do
360
- ActiveRecord ::Encryption . configure \
359
+ ActiveSupport . on_load ( :active_record_encryption ) do
360
+ ActiveRecord ::Encryption . configure (
361
361
primary_key : app . credentials . dig ( :active_record_encryption , :primary_key ) ,
362
362
deterministic_key : app . credentials . dig ( :active_record_encryption , :deterministic_key ) ,
363
363
key_derivation_salt : app . credentials . dig ( :active_record_encryption , :key_derivation_salt ) ,
364
364
**app . config . active_record . encryption
365
+ )
365
366
366
367
auto_filtered_parameters = ActiveRecord ::Encryption ::AutoFilteredParameters . new ( app )
367
368
auto_filtered_parameters . enable if ActiveRecord ::Encryption . config . add_to_filter_parameters
369
+ end
368
370
371
+ ActiveSupport . on_load ( :active_record ) do
369
372
# Support extended queries for deterministic attributes and validations
370
373
if ActiveRecord ::Encryption . config . extend_queries
371
374
ActiveRecord ::Encryption ::ExtendedDeterministicQueries . install_support
Original file line number Diff line number Diff line change @@ -3755,6 +3755,7 @@ These are the load hooks you can use in your own code. To hook into the initiali
3755
3755
| `ActiveModel::Model` | `active_model` |
3756
3756
| `ActiveModel::Translation` | `active_model_translation` |
3757
3757
| `ActiveRecord::Base` | `active_record` |
3758
+ | `ActiveRecord::Encryption` | `active_record_encryption` |
3758
3759
| `ActiveRecord::TestFixtures` | `active_record_fixtures` |
3759
3760
| `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter` | `active_record_postgresqladapter` |
3760
3761
| `ActiveRecord::ConnectionAdapters::Mysql2Adapter` | `active_record_mysql2adapter` |
Original file line number Diff line number Diff line change @@ -3731,6 +3731,29 @@ class Post < ActiveRecord::Base
3731
3731
assert_not_includes ActiveRecord ::Base . filter_attributes , :content
3732
3732
end
3733
3733
3734
+ test "ActiveRecord::Encryption.config is ready when accessed before loading ActiveRecord::Base" do
3735
+ add_to_config <<-RUBY
3736
+ config.enable_reloading = false
3737
+ config.eager_load = false
3738
+
3739
+ config.active_record.encryption.primary_key = "dummy_key"
3740
+ config.active_record.encryption.extend_queries = true
3741
+ RUBY
3742
+
3743
+ app "development"
3744
+
3745
+ # Encryption config is ready to be accessed
3746
+ assert_equal "dummy_key" , ActiveRecord ::Encryption . config . primary_key
3747
+ assert ActiveRecord ::Encryption . config . extend_queries
3748
+
3749
+ # ActiveRecord::Base is not loaded yet (lazy loading preserved)
3750
+ active_record_loaded = ActiveRecord . autoload? ( :Base ) . nil?
3751
+ assert_not active_record_loaded
3752
+
3753
+ # When ActiveRecord::Base loaded, extended queries should be installed
3754
+ assert ActiveRecord ::Base . include? ( ActiveRecord ::Encryption ::ExtendedDeterministicQueries ::CoreQueries )
3755
+ end
3756
+
3734
3757
test "ActiveRecord::Encryption.config is ready for encrypted attributes when app is lazy loaded" do
3735
3758
add_to_config <<-RUBY
3736
3759
config.enable_reloading = false
You can’t perform that action at this time.
0 commit comments