Skip to content

Commit c6515ed

Browse files
committed
Ignore empty categories/tags. Fix #1704
1 parent ef6e15d commit c6515ed

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/models/post.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ function pickID(data) {
1111
return data._id;
1212
}
1313

14+
function removeEmptyTag(tags) {
15+
return tags.filter(function(tag) {
16+
return tag != null && tag !== '';
17+
}).map(function(tag) {
18+
return tag + '';
19+
});
20+
}
21+
1422
module.exports = function(ctx) {
1523
var Post = new Schema({
1624
id: String,
@@ -72,6 +80,8 @@ module.exports = function(ctx) {
7280
});
7381

7482
Post.method('setTags', function(tags) {
83+
tags = removeEmptyTag(tags);
84+
7585
var PostTag = ctx.model('PostTag');
7686
var Tag = ctx.model('Tag');
7787
var id = this._id;
@@ -121,6 +131,8 @@ module.exports = function(ctx) {
121131
});
122132

123133
Post.method('setCategories', function(cats) {
134+
cats = removeEmptyTag(cats);
135+
124136
var PostCategory = ctx.model('PostCategory');
125137
var Category = ctx.model('Category');
126138
var id = this._id;

test/scripts/models/post.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ describe('Post', function() {
203203
});
204204
});
205205

206+
it('setTags() - empty tag', function() {
207+
var id;
208+
209+
return Post.insert({
210+
source: 'foo.md',
211+
slug: 'foo'
212+
}).then(function(post) {
213+
id = post._id;
214+
return post.setTags(['', undefined, null, false, 0, 'normal']);
215+
}).then(function() {
216+
var post = Post.findById(id);
217+
218+
post.tags.map(function(tag) {
219+
return tag.name;
220+
}).should.eql(['false', '0', 'normal']);
221+
}).finally(function() {
222+
return Post.removeById(id);
223+
});
224+
});
225+
206226
it('setCategories() - old categories should be removed', function() {
207227
var id;
208228

@@ -304,6 +324,46 @@ describe('Post', function() {
304324
});
305325
});
306326

327+
it('setCategories() - empty category', function() {
328+
var id;
329+
330+
return Post.insert({
331+
source: 'foo.md',
332+
slug: 'foo'
333+
}).then(function(post) {
334+
id = post._id;
335+
return post.setCategories(['test', null]);
336+
}).then(function() {
337+
var post = Post.findById(id);
338+
339+
post.categories.map(function(cat) {
340+
return cat.name;
341+
}).should.eql(['test']);
342+
}).finally(function() {
343+
return Post.removeById(id);
344+
});
345+
});
346+
347+
it('setCategories() - empty category in middle', function() {
348+
var id;
349+
350+
return Post.insert({
351+
source: 'foo.md',
352+
slug: 'foo'
353+
}).then(function(post) {
354+
id = post._id;
355+
return post.setCategories(['foo', null, 'bar']);
356+
}).then(function() {
357+
var post = Post.findById(id);
358+
359+
post.categories.map(function(cat) {
360+
return cat.name;
361+
}).should.eql(['foo', 'bar']);
362+
}).finally(function() {
363+
return Post.removeById(id);
364+
});
365+
});
366+
307367
it('remove PostTag references when a post is removed', function() {
308368
return Post.insert({
309369
source: 'foo.md',

0 commit comments

Comments
 (0)