Skip to content

Commit

Permalink
FIX: Clear post action types application serializer fragment cache.
Browse files Browse the repository at this point in the history
The bug was introduced in dc10bde
  • Loading branch information
tgxworld committed Jun 4, 2021
1 parent cadf5ea commit 3c1f4d5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/post_action_type.rb
Expand Up @@ -7,8 +7,8 @@ class PostActionType < ActiveRecord::Base
include AnonCacheInvalidator

def expire_cache
ApplicationSerializer.expire_cache_fragment!("post_action_types")
ApplicationSerializer.expire_cache_fragment!("post_action_flag_types")
ApplicationSerializer.expire_cache_fragment!(/^post_action_types_/)
ApplicationSerializer.expire_cache_fragment!(/^post_action_flag_types_/)
end

class << self
Expand Down
11 changes: 9 additions & 2 deletions app/serializers/application_serializer.rb
Expand Up @@ -14,8 +14,15 @@ def as_json(*_args)
end
end

def self.expire_cache_fragment!(name)
fragment_cache.delete(name)
def self.expire_cache_fragment!(name_or_regexp)
case name_or_regexp
when String
fragment_cache.delete(name_or_regexp)
when Regexp
fragment_cache.hash.keys
.select { |k| k =~ name_or_regexp }
.each { |k| fragment_cache.delete(k) }
end
end

def self.fragment_cache
Expand Down
18 changes: 18 additions & 0 deletions spec/models/post_action_type_spec.rb
Expand Up @@ -4,6 +4,24 @@

describe PostActionType do

context "callbacks" do
describe '#expiry_cache' do
it 'should clear the cache on save' do
cache = ApplicationSerializer.fragment_cache

cache["post_action_types_#{I18n.locale}"] = 'test'
cache["post_action_flag_types_#{I18n.locale}"] = 'test2'

PostActionType.new(name_key: 'some_key').save!

expect(cache["post_action_types_#{I18n.locale}"]).to eq(nil)
expect(cache["post_action_flag_types_#{I18n.locale}"]).to eq(nil)
ensure
ApplicationSerializer.fragment_cache.clear
end
end
end

describe '#types' do
context "verify enum sequence" do
before do
Expand Down

0 comments on commit 3c1f4d5

Please sign in to comment.