FIX: Clear post action types application serializer fragment cache.

FIX: Clear post action types application serializer fragment cache.

The bug was introduced in dc10bdee3dd23ae8d2acb4729c119e4b0b3f4853

diff --git a/app/models/post_action_type.rb b/app/models/post_action_type.rb
index 3a895f1..eac965c 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 700ac78..40ee57b 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 465c084..6fa95eb 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

GitHub sha: 3c1f4d57

This commit appears in #13265 which was approved by eviltrout. It was merged by tgxworld.