From 5b9ddaf972254e5f0c1a92aa676e74fbf3a0a6c2 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 19 Oct 2017 15:41:03 +0800 Subject: [PATCH] FIX: `Topic#fancy_title` should not write in readonly mode. --- app/models/topic.rb | 3 +-- spec/models/topic_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index f4093cb8f5c..89a2c79dd69 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -311,11 +311,10 @@ class Topic < ActiveRecord::Base return ERB::Util.html_escape(title) unless SiteSetting.title_fancy_entities? unless fancy_title = read_attribute(:fancy_title) - fancy_title = Topic.fancy_title(title) write_attribute(:fancy_title, fancy_title) - unless new_record? + if !new_record? && !Discourse.readonly_mode? # make sure data is set in table, this also allows us to change algorithm # by simply nulling this column exec_sql("UPDATE topics SET fancy_title = :fancy_title where id = :id", id: self.id, fancy_title: fancy_title) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 706b2ee4970..748c9aa0b69 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -327,6 +327,27 @@ describe Topic do topic.title = "this is another edge case" expect(topic.fancy_title).to eq("this is another edge case") end + + context 'readonly mode' do + before do + Discourse.enable_readonly_mode + end + + after do + Discourse.disable_readonly_mode + end + + it 'should not attempt to update `fancy_title`' do + topic.save! + expect(topic.fancy_title).to eq('“this topic” – has “fancy stuff”') + + topic.title = "This is a test testing testing" + expect(topic.fancy_title).to eq("This is a test testing testing") + + expect(topic.reload.read_attribute(:fancy_title)) + .to eq('“this topic” – has “fancy stuff”') + end + end end end