From f1da5f86b65e704375c3babc6c725bd98ea471ec Mon Sep 17 00:00:00 2001
From: Eugene Kulabuhov <eugene.kulabuhov@gmail.com>
Date: Fri, 19 Dec 2014 00:44:37 +0000
Subject: [PATCH] Updated tag management UI with the post counts

closes #4683
- added post count to 'delete tag' modal
- fixed lint errors
- preventing extra data to be sent to server
- adjustments suggested by @jaswilli
---
 core/client/controllers/modals/delete-tag.js |  4 ++++
 core/client/models/tag.js                    |  3 ++-
 core/client/routes/settings/tags.js          |  2 +-
 core/client/serializers/tag.js               | 21 ++++++++++++++++++++
 core/client/templates/modals/delete-tag.hbs  |  6 +++++-
 core/client/templates/settings/tags.hbs      |  2 +-
 6 files changed, 34 insertions(+), 4 deletions(-)
 create mode 100644 core/client/serializers/tag.js

diff --git a/core/client/controllers/modals/delete-tag.js b/core/client/controllers/modals/delete-tag.js
index 60b4fb8648..b0977c0352 100644
--- a/core/client/controllers/modals/delete-tag.js
+++ b/core/client/controllers/modals/delete-tag.js
@@ -1,4 +1,8 @@
 var DeleteTagController = Ember.Controller.extend({
+    inflection: function () {
+        return this.get('model').get('post_count') > 1 ? 'posts' : 'post';
+    }.property('model'),
+
     actions: {
         confirmAccept: function () {
             var tag = this.get('model'),
diff --git a/core/client/models/tag.js b/core/client/models/tag.js
index 1163e8c9e0..41203459bb 100644
--- a/core/client/models/tag.js
+++ b/core/client/models/tag.js
@@ -16,7 +16,8 @@ var Tag = DS.Model.extend(NProgressSaveMixin, ValidationEngine, {
     created_at: DS.attr('moment-date'),
     updated_at: DS.attr('moment-date'),
     created_by: DS.attr(),
-    updated_by: DS.attr()
+    updated_by: DS.attr(),
+    post_count: DS.attr('number')
 });
 
 export default Tag;
diff --git a/core/client/routes/settings/tags.js b/core/client/routes/settings/tags.js
index 3bc3470585..927fc80a25 100644
--- a/core/client/routes/settings/tags.js
+++ b/core/client/routes/settings/tags.js
@@ -22,7 +22,7 @@ var TagsRoute = AuthenticatedRoute.extend(CurrentUserSettings, PaginationRouteMi
     },
 
     model: function () {
-        return this.store.find('tag');
+        return this.store.find('tag', {include: 'post_count'});
     },
 
     setupController: function (controller, model) {
diff --git a/core/client/serializers/tag.js b/core/client/serializers/tag.js
new file mode 100644
index 0000000000..60792e81c4
--- /dev/null
+++ b/core/client/serializers/tag.js
@@ -0,0 +1,21 @@
+import ApplicationSerializer from 'ghost/serializers/application';
+
+var TagSerializer = ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
+
+    serializeIntoHash: function (hash, type, record, options) {
+        options = options || {};
+
+        // We have a plural root in the API
+        var root = Ember.String.pluralize(type.typeKey),
+            data = this.serialize(record, options);
+
+        // Properties that exist on the model but we don't want sent in the payload
+
+        delete data.post_count;
+        delete data.uuid;
+
+        hash[root] = [data];
+    }
+});
+
+export default TagSerializer;
diff --git a/core/client/templates/modals/delete-tag.hbs b/core/client/templates/modals/delete-tag.hbs
index 240cb0516c..467ace9a29 100644
--- a/core/client/templates/modals/delete-tag.hbs
+++ b/core/client/templates/modals/delete-tag.hbs
@@ -1,6 +1,10 @@
 {{#gh-modal-dialog action="closeModal" showClose=true type="action" style="wide,centered" animation="fade"
     title="Are you sure you want to delete this tag?" confirm=confirm}}
 
-    <p>You're about to delete "<strong>{{model.name}}</strong>".<br />This is permanent! No backups, no restores, no magic undo button. <br /> We warned you, ok?</p>
+    <p>You're about to delete "<strong>{{model.name}}</strong>".<br /> 
+    {{#if model.post_count}}
+    <span class="red">This tag will be removed from {{model.post_count}} {{inflection}}.</span>
+    {{/if}}
+    This is permanent! No backups, no restores, no magic undo button. <br /> We warned you, ok?</p>
 
 {{/gh-modal-dialog}}
\ No newline at end of file
diff --git a/core/client/templates/settings/tags.hbs b/core/client/templates/settings/tags.hbs
index 118d5dddb1..c434442871 100644
--- a/core/client/templates/settings/tags.hbs
+++ b/core/client/templates/settings/tags.hbs
@@ -13,7 +13,7 @@
                 <span class="tag-title">{{tag.name}}</span>
                 <span class="label label-default">/{{tag.slug}}</span>
                 <p class="tag-description">{{tag.description}}</p>
-                <span class="tags-count">N/A</span>
+                <span class="tags-count">{{tag.post_count}}</span>
             </button>
         </div>
     {{/each}}