diff --git a/app/models/watched_word.rb b/app/models/watched_word.rb index ecc239ae675..9251d7d2b6a 100644 --- a/app/models/watched_word.rb +++ b/app/models/watched_word.rb @@ -20,7 +20,7 @@ class WatchedWord < ActiveRecord::Base before_validation do self.word = self.class.normalize_word(self.word) if self.action == WatchedWord.actions[:link] && !(self.replacement =~ /^https?:\/\//) - self.replacement = "#{Discourse.base_url}#{self.replacement.starts_with?("/") ? "" : "/"}#{self.replacement}" + self.replacement = "#{Discourse.base_url}#{self.replacement&.starts_with?("/") ? "" : "/"}#{self.replacement}" end end diff --git a/spec/models/watched_word_spec.rb b/spec/models/watched_word_spec.rb index e58a9432b67..a2d425a76ca 100644 --- a/spec/models/watched_word_spec.rb +++ b/spec/models/watched_word_spec.rb @@ -90,5 +90,16 @@ describe WatchedWord do expect(w.id).to eq(existing.id) }.to_not change { described_class.count } end + + it "replaces link with absolute URL" do + word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta1") + expect(word.replacement).to eq("http://test.localhost/") + + word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta2", replacement: "test") + expect(word.replacement).to eq("http://test.localhost/test") + + word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta3", replacement: "/test") + expect(word.replacement).to eq("http://test.localhost/test") + end end end