Skip to content

Commit a9ce4fb

Browse files
committed
Move caching initialization to Railtie
Also - Add reference to config from ActiveModelSerializers.config - correctly call super in FragmentCacheTest#setup - rename test rails app from Foo to ActiveModelSerializers::RailsApplication
1 parent 2bea7f9 commit a9ce4fb

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

lib/active_model/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def self.fragmented(serializer)
152152
# @todo require less code comments. See
153153
# https://github.com/rails-api/active_model_serializers/pull/1249#issuecomment-146567837
154154
def self.cache(options = {})
155-
self._cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching
155+
self._cache = ActiveModelSerializers.config.cache_store if ActiveModelSerializers.config.perform_caching
156156
self._cache_key = options.delete(:key)
157157
self._cache_only = options.delete(:only)
158158
self._cache_except = options.delete(:except)

lib/active_model/serializer/railtie.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ class Railtie < Rails::Railtie
88
end
99
end
1010

11+
initializer 'active_model_serializers.caching' do
12+
ActiveSupport.on_load(:action_controller) do
13+
ActiveModelSerializers.config.cache_store = ActionController::Base.cache_store
14+
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
15+
end
16+
end
17+
1118
initializer 'generators' do |app|
1219
app.load_generators
1320
require 'generators/serializer/resource_override'

lib/active_model_serializers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
module ActiveModelSerializers
66
mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
77

8+
def self.config
9+
ActiveModel::Serializer.config
10+
end
11+
812
extend ActiveSupport::Autoload
913
autoload :Model
1014
autoload :Callbacks

test/adapter/fragment_cache_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Serializer
44
module Adapter
55
class FragmentCacheTest < Minitest::Test
66
def setup
7+
super
78
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
89
@author = Author.new(name: 'Joao M. D. Moura')
910
@role = Role.new(name: 'Great Author', description: nil)

test/support/rails_app.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
class Foo < Rails::Application
1+
class ActiveModelSerializers::RailsApplication < Rails::Application
22
if Rails::VERSION::MAJOR >= 4
33
config.eager_load = false
4+
45
config.secret_key_base = 'abc123'
5-
config.action_controller.perform_caching = true
6+
67
config.active_support.test_order = :random
8+
79
config.logger = Logger.new(nil)
10+
11+
config.action_controller.perform_caching = true
812
ActionController::Base.cache_store = :memory_store
913
end
1014
end
11-
Foo.initialize!
15+
ActiveModelSerializers::RailsApplication.initialize!
1216

1317
module TestHelper
1418
Routes = ActionDispatch::Routing::RouteSet.new

0 commit comments

Comments
 (0)