diff --git a/app/models/post_action_type.rb b/app/models/post_action_type.rb index 3a895f14d5a..eac965c54fb 100644 --- a/app/models/post_action_type.rb +++ b/app/models/post_action_type.rb @@ -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 diff --git a/app/serializers/application_serializer.rb b/app/serializers/application_serializer.rb index 700ac78600b..40ee57b0622 100644 --- a/app/serializers/application_serializer.rb +++ b/app/serializers/application_serializer.rb @@ -14,8 +14,15 @@ class ApplicationSerializer < ActiveModel::Serializer 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 diff --git a/spec/models/post_action_type_spec.rb b/spec/models/post_action_type_spec.rb index 465c0849c37..6fa95eb039d 100644 --- a/spec/models/post_action_type_spec.rb +++ b/spec/models/post_action_type_spec.rb @@ -4,6 +4,24 @@ require 'rails_helper' 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