From 9abadd57c44c7f26830fafc45f8a412e3276e7c3 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Thu, 8 Nov 2018 13:49:00 +0000 Subject: [PATCH] Automatically update count.posts when serialising tags in mirage no issue - add a new mirage serialiser for tags that updates the count.posts for each tag that gets serialised - better matches real API behaviour --- ghost/admin/mirage/factories/tag.js | 5 ++--- ghost/admin/mirage/serializers/tag.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 ghost/admin/mirage/serializers/tag.js diff --git a/ghost/admin/mirage/factories/tag.js b/ghost/admin/mirage/factories/tag.js index 66db206988..9ea30ee572 100644 --- a/ghost/admin/mirage/factories/tag.js +++ b/ghost/admin/mirage/factories/tag.js @@ -14,8 +14,7 @@ export default Factory.extend({ updatedAt: '2015-10-19T16:25:07.756Z', updatedBy: 1, count() { - return { - posts: 1 - }; + // this gets updated automatically by the tag serializer + return {posts: 0}; } }); diff --git a/ghost/admin/mirage/serializers/tag.js b/ghost/admin/mirage/serializers/tag.js new file mode 100644 index 0000000000..6906521de0 --- /dev/null +++ b/ghost/admin/mirage/serializers/tag.js @@ -0,0 +1,18 @@ +import BaseSerializer from './application'; + +export default BaseSerializer.extend({ + // make the tag.count.posts value dynamic + serialize(tagModelOrCollection, request) { + let updatePostCount = (tag) => { + tag.update('count', {posts: tag.postIds.length}); + }; + + if (this.isModel(tagModelOrCollection)) { + updatePostCount(tagModelOrCollection); + } else { + tagModelOrCollection.models.forEach(updatePostCount); + } + + return BaseSerializer.prototype.serialize.call(this, tagModelOrCollection, request); + } +});