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

View File

@ -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"),

View File

@ -40,12 +40,12 @@
→ {{bound-avatar-template user_changes.current.avatar_template "small"}} {{user_changes.current.username}}
{{/if}}
{{#if wiki_changes}}
— {{{wiki_diff}}}
— {{{wikiDiff}}}
{{/if}}
{{#if post_type_changes}}
— {{{post_type_diff}}}
— {{{postTypeDiff}}}
{{/if}}
{{#if category_changes}}
{{#if category_id_changes}}
— {{{previousCategory}}} → {{{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>

View File

@ -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