Refactor and enable extensibility in PostRevisionSerializer

This commit is contained in:
Robin Ward 2015-01-27 17:12:43 -05:00
parent d43944b3ed
commit 4a46d4ee35
3 changed files with 37 additions and 53 deletions
app
assets/javascripts/discourse
controllers
templates/modal
serializers

View file

@ -66,22 +66,22 @@ export default ObjectController.extend(ModalFunctionality, {
displayingSideBySideMarkdown: Em.computed.equal("viewMode", "side_by_side_markdown"), displayingSideBySideMarkdown: Em.computed.equal("viewMode", "side_by_side_markdown"),
previousCategory: function() { previousCategory: function() {
var changes = this.get("category_changes"); var changes = this.get("category_id_changes");
if (changes) { if (changes) {
var category = Discourse.Category.findById(changes["previous"]); var category = Discourse.Category.findById(changes["previous"]);
return categoryBadgeHTML(category, { allowUncategorized: true }); return categoryBadgeHTML(category, { allowUncategorized: true });
} }
}.property("category_changes"), }.property("category_id_changes"),
currentCategory: function() { currentCategory: function() {
var changes = this.get("category_changes"); var changes = this.get("category_id_changes");
if (changes) { if (changes) {
var category = Discourse.Category.findById(changes["current"]); var category = Discourse.Category.findById(changes["current"]);
return categoryBadgeHTML(category, { allowUncategorized: true }); return categoryBadgeHTML(category, { allowUncategorized: true });
} }
}.property("category_changes"), }.property("category_id_changes"),
wiki_diff: function() { wikiDiff: function() {
var changes = this.get("wiki_changes"); var changes = this.get("wiki_changes");
if (changes) { if (changes) {
return changes["current"] ? return changes["current"] ?
@ -90,7 +90,7 @@ export default ObjectController.extend(ModalFunctionality, {
} }
}.property("wiki_changes"), }.property("wiki_changes"),
post_type_diff: function () { postTypeDiff: function () {
var moderator = Discourse.Site.currentProp('post_types.moderator_action'); var moderator = Discourse.Site.currentProp('post_types.moderator_action');
var changes = this.get("post_type_changes"); var changes = this.get("post_type_changes");
if (changes) { if (changes) {
@ -100,13 +100,13 @@ export default ObjectController.extend(ModalFunctionality, {
} }
}.property("post_type_changes"), }.property("post_type_changes"),
title_diff: function() { titleDiff: function() {
var viewMode = this.get("viewMode"); var viewMode = this.get("viewMode");
if (viewMode === "side_by_side_markdown") { viewMode = "side_by_side"; } if (viewMode === "side_by_side_markdown") { viewMode = "side_by_side"; }
return this.get("title_changes." + viewMode); return this.get("title_changes." + viewMode);
}.property("viewMode", "title_changes"), }.property("viewMode", "title_changes"),
body_diff: function() { bodyDiff: function() {
return this.get("body_changes." + this.get("viewMode")); return this.get("body_changes." + this.get("viewMode"));
}.property("viewMode", "body_changes"), }.property("viewMode", "body_changes"),

View file

@ -40,12 +40,12 @@
→ {{bound-avatar-template user_changes.current.avatar_template "small"}} {{user_changes.current.username}} → {{bound-avatar-template user_changes.current.avatar_template "small"}} {{user_changes.current.username}}
{{/if}} {{/if}}
{{#if wiki_changes}} {{#if wiki_changes}}
— {{{wiki_diff}}} — {{{wikiDiff}}}
{{/if}} {{/if}}
{{#if post_type_changes}} {{#if post_type_changes}}
— {{{post_type_diff}}} — {{{postTypeDiff}}}
{{/if}} {{/if}}
{{#if category_changes}} {{#if category_id_changes}}
— {{{previousCategory}}} → {{{currentCategory}}} — {{{previousCategory}}} → {{{currentCategory}}}
{{/if}} {{/if}}
{{/unless}} {{/unless}}
@ -53,7 +53,7 @@
<div id="revisions" {{bind-attr class="hiddenClasses"}}> <div id="revisions" {{bind-attr class="hiddenClasses"}}>
{{#if title_changes}} {{#if title_changes}}
<div class="row"> <div class="row">
<h2>{{{title_diff}}}</h2> <h2>{{{titleDiff}}}</h2>
</div> </div>
{{/if}} {{/if}}
{{#if site.mobileView}} {{#if site.mobileView}}
@ -65,22 +65,22 @@
{{/if}} {{/if}}
{{#if wiki_changes}} {{#if wiki_changes}}
<div class="row"> <div class="row">
{{{wiki_diff}}} {{{wikiDiff}}}
</div> </div>
{{/if}} {{/if}}
{{#if post_type_changes}} {{#if post_type_changes}}
<div class="row"> <div class="row">
{{{post_type_diff}}} {{{postTypeDiff}}}
</div> </div>
{{/if}} {{/if}}
{{#if category_changes}} {{#if category_id_changes}}
<div class="row"> <div class="row">
{{{previousCategory}}} &rarr; {{{currentCategory}}} {{{previousCategory}}} &rarr; {{{currentCategory}}}
</div> </div>
{{/if}} {{/if}}
{{/if}} {{/if}}
<div class="row"> <div class="row">
{{{body_diff}}} {{{bodyDiff}}}
</div> </div>
</div> </div>
</div> </div>

View file

@ -22,10 +22,27 @@ class PostRevisionSerializer < ApplicationSerializer
:edit_reason, :edit_reason,
:body_changes, :body_changes,
:title_changes, :title_changes,
:category_changes, :user_changes
:user_changes,
:wiki_changes,
:post_type_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 def previous_hidden
previous["hidden"] previous["hidden"]
@ -109,17 +126,6 @@ class PostRevisionSerializer < ApplicationSerializer
} }
end end
def category_changes
prev = previous["category_id"]
cur = current["category_id"]
return if prev == cur
{
previous: prev,
current: cur,
}
end
def user_changes def user_changes
prev = previous["user_id"] prev = previous["user_id"]
cur = current["user_id"] cur = current["user_id"]
@ -143,28 +149,6 @@ class PostRevisionSerializer < ApplicationSerializer
} }
end 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 protected
def post def post