From 4a46d4ee3560033bcc07e08b1575b09d8168d096 Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Tue, 27 Jan 2015 17:12:43 -0500
Subject: [PATCH] Refactor and enable extensibility in PostRevisionSerializer

---
 .../discourse/controllers/history.js.es6      | 16 ++---
 .../discourse/templates/modal/history.hbs     | 16 ++---
 app/serializers/post_revision_serializer.rb   | 58 +++++++------------
 3 files changed, 37 insertions(+), 53 deletions(-)

diff --git a/app/assets/javascripts/discourse/controllers/history.js.es6 b/app/assets/javascripts/discourse/controllers/history.js.es6
index 55302edc9..a69da38dd 100644
--- a/app/assets/javascripts/discourse/controllers/history.js.es6
+++ b/app/assets/javascripts/discourse/controllers/history.js.es6
@@ -66,22 +66,22 @@ export default ObjectController.extend(ModalFunctionality, {
   displayingSideBySideMarkdown: Em.computed.equal("viewMode", "side_by_side_markdown"),
 
   previousCategory: function() {
-    var changes = this.get("category_changes");
+    var changes = this.get("category_id_changes");
     if (changes) {
       var category = Discourse.Category.findById(changes["previous"]);
       return categoryBadgeHTML(category, { allowUncategorized: true });
     }
-  }.property("category_changes"),
+  }.property("category_id_changes"),
 
   currentCategory: function() {
-    var changes = this.get("category_changes");
+    var changes = this.get("category_id_changes");
     if (changes) {
       var category = Discourse.Category.findById(changes["current"]);
       return categoryBadgeHTML(category, { allowUncategorized: true });
     }
-  }.property("category_changes"),
+  }.property("category_id_changes"),
 
-  wiki_diff: function() {
+  wikiDiff: function() {
     var changes = this.get("wiki_changes");
     if (changes) {
       return changes["current"] ?
@@ -90,7 +90,7 @@ export default ObjectController.extend(ModalFunctionality, {
     }
   }.property("wiki_changes"),
 
-  post_type_diff: function () {
+  postTypeDiff: function () {
     var moderator = Discourse.Site.currentProp('post_types.moderator_action');
     var changes = this.get("post_type_changes");
     if (changes) {
@@ -100,13 +100,13 @@ export default ObjectController.extend(ModalFunctionality, {
     }
   }.property("post_type_changes"),
 
-  title_diff: function() {
+  titleDiff: function() {
     var viewMode = this.get("viewMode");
     if (viewMode === "side_by_side_markdown") { viewMode = "side_by_side"; }
     return this.get("title_changes." + viewMode);
   }.property("viewMode", "title_changes"),
 
-  body_diff: function() {
+  bodyDiff: function() {
     return this.get("body_changes." + this.get("viewMode"));
   }.property("viewMode", "body_changes"),
 
diff --git a/app/assets/javascripts/discourse/templates/modal/history.hbs b/app/assets/javascripts/discourse/templates/modal/history.hbs
index 97f648034..0c3742e77 100644
--- a/app/assets/javascripts/discourse/templates/modal/history.hbs
+++ b/app/assets/javascripts/discourse/templates/modal/history.hbs
@@ -40,12 +40,12 @@
         &rarr; {{bound-avatar-template user_changes.current.avatar_template "small"}} {{user_changes.current.username}}
       {{/if}}
       {{#if wiki_changes}}
-        &mdash; {{{wiki_diff}}}
+        &mdash; {{{wikiDiff}}}
       {{/if}}
       {{#if post_type_changes}}
-        &mdash; {{{post_type_diff}}}
+        &mdash; {{{postTypeDiff}}}
       {{/if}}
-      {{#if category_changes}}
+      {{#if category_id_changes}}
         &mdash; {{{previousCategory}}} &rarr; {{{currentCategory}}}
       {{/if}}
     {{/unless}}
@@ -53,7 +53,7 @@
   <div id="revisions" {{bind-attr class="hiddenClasses"}}>
     {{#if title_changes}}
       <div class="row">
-        <h2>{{{title_diff}}}</h2>
+        <h2>{{{titleDiff}}}</h2>
       </div>
     {{/if}}
     {{#if site.mobileView}}
@@ -65,22 +65,22 @@
       {{/if}}
       {{#if wiki_changes}}
         <div class="row">
-          {{{wiki_diff}}}
+          {{{wikiDiff}}}
         </div>
       {{/if}}
       {{#if post_type_changes}}
         <div class="row">
-          {{{post_type_diff}}}
+          {{{postTypeDiff}}}
         </div>
       {{/if}}
-      {{#if category_changes}}
+      {{#if category_id_changes}}
         <div class="row">
           {{{previousCategory}}} &rarr; {{{currentCategory}}}
         </div>
       {{/if}}
     {{/if}}
     <div class="row">
-      {{{body_diff}}}
+      {{{bodyDiff}}}
     </div>
   </div>
 </div>
diff --git a/app/serializers/post_revision_serializer.rb b/app/serializers/post_revision_serializer.rb
index 637f07453..1d24b6630 100644
--- a/app/serializers/post_revision_serializer.rb
+++ b/app/serializers/post_revision_serializer.rb
@@ -22,10 +22,27 @@ class PostRevisionSerializer < ApplicationSerializer
              :edit_reason,
              :body_changes,
              :title_changes,
-             :category_changes,
-             :user_changes,
-             :wiki_changes,
-             :post_type_changes
+             :user_changes
+
+
+  # Creates a field called field_name_changes with previous and
+  # current members if a field has changed in this revision
+  def self.add_compared_field(field)
+    changes_name = "#{field}_changes".to_sym
+
+    self.attributes changes_name
+    define_method(changes_name) do
+      { previous: previous[field], current: current[field] }
+    end
+
+    define_method("include_#{changes_name}?") do
+      previous[field] != current[field]
+    end
+  end
+
+  add_compared_field :category_id
+  add_compared_field :wiki
+  add_compared_field :post_type
 
   def previous_hidden
     previous["hidden"]
@@ -109,17 +126,6 @@ class PostRevisionSerializer < ApplicationSerializer
     }
   end
 
-  def category_changes
-    prev = previous["category_id"]
-    cur = current["category_id"]
-    return if prev == cur
-
-    {
-      previous: prev,
-      current: cur,
-    }
-  end
-
   def user_changes
     prev = previous["user_id"]
     cur = current["user_id"]
@@ -143,28 +149,6 @@ class PostRevisionSerializer < ApplicationSerializer
     }
   end
 
-  def wiki_changes
-    prev = previous["wiki"]
-    cur = current["wiki"]
-    return if prev == cur
-
-    {
-        previous: prev,
-        current: cur,
-    }
-  end
-
-  def post_type_changes
-    prev = previous["post_type"]
-    cur = current["post_type"]
-    return if prev == cur
-
-    {
-        previous: prev,
-        current: cur,
-    }
-  end
-
   protected
 
     def post