Skip to content

Commit 38d0d40

Browse files
committed
Fix warning on ActiveSupport::Cache.format_version
`Rail.application` was always evaluating to `nil` before as the code wasn't running in an initializer. As a fix, instead simply provide a non-deprecated serializer to the initialized cache_store when in test environment (and where the code would raise a warning otherwise). Also move this block to a proper Rails initializer.
1 parent 5878078 commit 38d0d40

File tree

5 files changed

+15
-26
lines changed

5 files changed

+15
-26
lines changed

lib/graphql/fragment_cache/railtie.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ class Railtie < ::Rails::Railtie # :nodoc:
1010
module Config
1111
class << self
1212
def store=(store)
13-
if Rails.version.to_f >= 7.0 && Rails.application
14-
cache_format_version = 7.0
15-
ActiveSupport::Cache.format_version = cache_format_version if cache_format_version
16-
end
17-
1813
# Handle both:
1914
# store = :memory
2015
# store = :mem_cache, ENV['MEMCACHE']
@@ -30,7 +25,13 @@ def store=(store)
3025
config.graphql_fragment_cache = Config
3126

3227
if ENV["RACK_ENV"] == "test" || ENV["RAILS_ENV"] == "test"
33-
config.graphql_fragment_cache.store = :null_store
28+
initializer "graphql-fragment_cache" do
29+
config.graphql_fragment_cache.store = if Rails.version.to_f >= 7.0
30+
[:null_store, serializer: :marshal_7_0]
31+
else
32+
:null_store
33+
end
34+
end
3435
end
3536
end
3637
end

spec/graphql/fragment_cache/cacher_spec.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ def write_multi(hash, options)
4444

4545
let(:store) { write_multi_store_class.new }
4646

47-
around do |ex|
48-
old_store = GraphQL::FragmentCache.cache_store
47+
before do
48+
allow(store).to receive(:write_multi).and_call_original
4949
GraphQL::FragmentCache.cache_store = store
50-
ex.run
51-
GraphQL::FragmentCache.cache_store = old_store
5250
end
5351

5452
it "uses #write_multi" do
@@ -134,11 +132,8 @@ def write(key, value, options)
134132

135133
let(:store) { write_with_error_store_class.new }
136134

137-
around do |ex|
138-
old_store = GraphQL::FragmentCache.cache_store
135+
before do
139136
GraphQL::FragmentCache.cache_store = store
140-
ex.run
141-
GraphQL::FragmentCache.cache_store = old_store
142137
end
143138

144139
it "raises error" do

spec/graphql/fragment_cache/rails/railtie_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,5 @@
1616
expect(GraphQL::FragmentCache.cache_store).to be_a(ActiveSupport::Cache::MemoryStore)
1717
expect(GraphQL::FragmentCache.cache_store.options[:max_size]).to eq 10.megabytes
1818
end
19-
20-
it "updates ActiveSupport::Cache.format_version when rails version is 7.0 or higher" do
21-
allow(Rails).to receive(:version).and_return("7.0")
22-
Rails.application.config.active_support.cache_format_version = 7.0
23-
Rails.application.config.graphql_fragment_cache.store = :memory_store
24-
25-
expect(ActiveSupport::Cache.format_version).to eq 7.0
26-
end
2719
end
2820
end

spec/rails_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66
require "combustion"
77
require "graphql/fragment_cache/railtie"
88

9-
GraphQL::FragmentCache.cache_store = GraphQL::FragmentCache::MemoryStore.new
10-
119
Combustion.initialize! :active_record

spec/spec_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232
config.include SchemaHelper
3333
config.include_context "graphql"
3434

35-
config.after do
36-
GraphQL::FragmentCache.cache_store.clear if GraphQL::FragmentCache.cache_store.respond_to?(:clear)
35+
config.before do
36+
GraphQL::FragmentCache.cache_store = GraphQL::FragmentCache::MemoryStore.new
37+
end
38+
39+
config.before do
3740
Post.delete_all
3841
Timecop.return
3942
end

0 commit comments

Comments
 (0)