From efea296c7a5b535d7e152f694ccab8313f873103 Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan
Date: Mon, 24 Oct 2016 12:02:38 +0800
Subject: [PATCH] FIX: Do not cook post if `Post#raw` has not been changed.
---
app/models/post.rb | 6 +++++-
spec/models/post_spec.rb | 15 +++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/app/models/post.rb b/app/models/post.rb
index fcc5e0c8182..8fc89f682c6 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -522,7 +522,11 @@ class Post < ActiveRecord::Base
before_save do
self.last_editor_id ||= user_id
- self.cooked = cook(raw, topic_id: topic_id) unless new_record?
+
+ if !new_record? && raw_changed?
+ self.cooked = cook(raw, topic_id: topic_id)
+ end
+
self.baked_at = Time.new
self.baked_version = BAKED_VERSION
end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index a5ceb4a391e..e70f176d4af 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -628,6 +628,21 @@ describe Post do
end
+ describe 'before save' do
+ let(:cooked) { "
" }
+
+ let(:post) do
+ Fabricate(:post,
+ raw: "",
+ cooked: cooked
+ )
+ end
+
+ it 'should not cook the post if raw has not been changed' do
+ post.save!
+ expect(post.cooked).to eq(cooked)
+ end
+ end
describe 'after save' do