A platform for community discussion. Free, open, simple.
FIX: Make serialized watched word regex Javascript compatible (#22010)
Selase Krakanib3bb3872cfde
diff --git a/app/serializers/watched_word_list_serializer.rb b/app/serializers/watched_word_list_serializer.rbindex 4b47ce16f78..9d0929a9fa9 100644--- a/app/serializers/watched_word_list_serializer.rb+++ b/app/serializers/watched_word_list_serializer.rb@@ -18,7 +18,7 @@ class WatchedWordListSerializer < ApplicationSerializerdef compiled_regular_expressionsexpressions = {}actions.each do |action|- expressions[action] = WordWatcher.serializable_word_matcher_regexp(action)+ expressions[action] = WordWatcher.serializable_word_matcher_regexp(action, engine: :js)endexpressionsenddiff --git a/app/serializers/watched_word_serializer.rb b/app/serializers/watched_word_serializer.rbindex 135aca292b3..0ba4f7cc5da 100644--- a/app/serializers/watched_word_serializer.rb+++ b/app/serializers/watched_word_serializer.rb@@ -4,7 +4,7 @@ class WatchedWordSerializer < ApplicationSerializerattributes :id, :word, :regexp, :replacement, :action, :case_sensitivedef regexp- WordWatcher.word_to_regexp(word)+ WordWatcher.word_to_regexp(word, engine: :js)enddef actiondiff --git a/spec/requests/admin/watched_words_controller_spec.rb b/spec/requests/admin/watched_words_controller_spec.rbindex ef140bbdcf3..12677fbc655 100644--- a/spec/requests/admin/watched_words_controller_spec.rb+++ b/spec/requests/admin/watched_words_controller_spec.rb@@ -6,6 +6,60 @@ RSpec.describe Admin::WatchedWordsController dofab!(:admin) { Fabricate(:admin) }fab!(:user) { Fabricate(:user) }+ describe "#index" do+ context "when logged in as non-staff user" do+ before { sign_in(user) }++ it "does not return watched words" do+ get "/admin/customize/watched_words.json"++ expect(response.status).to eq(404)+ end+ end++ context "when logged in as a staff user" do+ fab!(:word1) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }+ fab!(:word2) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }+ fab!(:word3) { Fabricate(:watched_word, action: WatchedWord.actions[:censor]) }+ fab!(:word4) { Fabricate(:watched_word, action: WatchedWord.actions[:censor]) }++ before { sign_in(admin) }++ it "returns all watched words" do+ get "/admin/customize/watched_words.json"++ expect(response.status).to eq(200)++ watched_words = response.parsed_body++ expect(watched_words["actions"]).to match_array(WatchedWord.actions.keys.map(&:to_s))+ expect(watched_words["words"].length).to eq(4)+ expect(watched_words["words"]).to include(+ hash_including(+ "id" => word1.id,+ "word" => word1.word,+ "regexp" => WordWatcher.word_to_regexp(word1.word, engine: :js),+ "case_sensitive" => false,+ "action" => "block",+ ),+ hash_including(+ "id" => word4.id,+ "word" => word4.word,+ "regexp" => WordWatcher.word_to_regexp(word4.word, engine: :js),+ "case_sensitive" => false,+ "action" => "censor",+ ),+ )+ expect(watched_words["compiled_regular_expressions"]["block"].first).to eq(+ WordWatcher+ .serializable_word_matcher_regexp(:block, engine: :js)+ .first+ .deep_stringify_keys,+ )+ end+ end+ end+describe "#destroy" dofab!(:watched_word) { Fabricate(:watched_word) }