Skip to content

Commit cdab6f2

Browse files
committed
The cache store needs to be the actually store, not e.g. :memory_store
Status quo in test app: In Rails ActionController::Base.cache_store = :memory_store and then AMS railtie does: ActiveModelSerializers.config.cache_store = config.action_controller.cache_store then, in the Railtie 1. ActiveSupport.on_load(:action_controller) fires - ActiveModelSerializers.config.cache_store #=> nil - ActionController::Base.cache_store #=> #<ActiveSupport::Cache::FileStore:0x007fe319256760...] 2. After set_configs fires - ActiveModelSerializers.config.cache_store #+> #<ActiveSupport::Cache::FileStore:0x007fe319256760 , 3. Tests pass, but notice that we're using the FileStore, not memory store When we change the config to the test app: ActionController::Base.cache_store = :memory_store config = Rails.configuration config.action_controller.cache_store = :memory_store then, in the Railtie: 1. ActiveSupport.on_load(:action_controller) fires - ActiveModelSerializers.config.cache_store #=> nil - ActionController::Base.cache_store #=> #ActiveSupport::Cache::MemoryStore entries=0, size=0, options={}>] 2. After set_configs fires - ActiveModelSerializers.config.cache_store #=> :memory_store 3. And we get a lot of failures: NoMethodError: undefined method `fetch' for :memory_store:Symbol So, we see that when we set the ActionController::Base.cache_store directly in our test app, we could set ActiveModelSerializers.config.cache_store in the :action_controller load hook, but that would never use the Rails config. To fix the Rails config, we change the config to the test app: config = Rails.configuration config.action_controller.cache_store = :memory_store and then AMS railtie does: ActiveModelSerializers.config.cache_store = ActiveSupport::Cache.lookup_store(config.action_controller.cache_store ActiveSupport.on_load(:action_controller) do ::ActiveModelSerializers.config.cache_store = cache_store end then 1. After set_configs fires - ActiveModelSerializers.config.cache_store #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207113611740 2. ActiveSupport.on_load(:action_controller) fires - ActionController::Base.cache_store #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207106279660 - ActiveModelSerializers.config.cache_store #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207106279660 (notice the object_id changed) 3. And we get a failure: 1) Failure: ActiveModelSerializers::CacheTest#test_associations_cache_when_updated [active_model_serializers/test/cache_test.rb:141]: --- expected +++ actual @@ -1 +1 @@ -{:id=>"post", :title=>"New Post", :body=>"Body"} +{:id=>"post", :title=>"New Post", :body=>"Body", :comments=>[{:id=>2, :body=>"ZOMG A NEW COMMENT"}], :blog=>{:id=>999, :name=>"Custom blog"}, :author=>{:id=>"author", :name=>"Joao M. D. Moura"}} If we take out the on_load(:action_controller) hook, we get a ton of failures. So clearly, our code expects the controller cache to be the same as the serializer cache. So, we make sure we use an on_load(:action_controller) hook that runs after set_configs And look at the test and see it is filled with direct calls to ActionController::Base.cache_store assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key)) assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key)) But that's not a problem in this case, since they're the same object. For now, let's remove the :memory_store setting and use the default FileStore
1 parent a7a6841 commit cdab6f2

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

lib/active_model_serializers/railtie.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ class Railtie < Rails::Railtie
2525
# and also before eager_loading (if enabled).
2626
initializer 'active_model_serializers.set_configs', :after => 'action_controller.set_configs' do
2727
ActiveModelSerializers.logger = Rails.configuration.action_controller.logger
28-
ActiveModelSerializers.config.cache_store = Rails.configuration.action_controller.cache_store
2928
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
29+
# We want this hook to run after the config has been set, even if ActionController has already loaded.
30+
ActiveSupport.on_load(:action_controller) do
31+
ActiveModelSerializers.config.cache_store = cache_store
32+
end
3033
end
3134

3235
generators do |app|

test/serializers/caching_configuration_test_isolated.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class PerformCachingTrue < CachingConfigurationTest
3737
app.config.action_controller.perform_caching = true
3838
app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
3939
end
40+
controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
4041
end
4142

4243
test 'it sets perform_caching to true on AMS.config and serializers' do
@@ -103,6 +104,7 @@ class PerformCachingFalse < CachingConfigurationTest
103104
app.config.action_controller.perform_caching = false
104105
app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
105106
end
107+
controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
106108
end
107109

108110
test 'it sets perform_caching to false on AMS.config and serializers' do

test/support/rails_app.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module ActiveModelSerializers
55
config.secret_key_base = 'abc123'
66
config.active_support.test_order = :random
77
config.action_controller.perform_caching = true
8-
ActionController::Base.cache_store = :memory_store
98
end
109

1110
app.routes.default_url_options = { host: 'example.com' }

0 commit comments

Comments
 (0)