Initial work on internal tags feature

refs #6165
This commit is contained in:
Hannah Wolfe 2015-12-02 00:13:05 +08:00 committed by Austin Burdine
parent eeb41a8976
commit 369fd2c6bd
9 changed files with 105 additions and 8 deletions

View File

@ -33,6 +33,7 @@ function getBaseConfig() {
useGoogleFonts: {value: !config.isPrivacyDisabled('useGoogleFonts'), type: 'bool'},
useGravatar: {value: !config.isPrivacyDisabled('useGravatar'), type: 'bool'},
publicAPI: labsFlag('publicAPI'),
hiddenTags: labsFlag('hiddenTags'),
blogUrl: {value: config.url.replace(/\/$/, ''), type: 'string'},
blogTitle: {value: config.theme.title, type: 'string'},
routeKeywords: {value: JSON.stringify(config.routeKeywords), type: 'json'}

View File

@ -54,7 +54,7 @@ tags = {
* @return {Promise<Tag>} Tag
*/
read: function read(options) {
var attrs = ['id', 'slug'],
var attrs = ['id', 'slug', 'hidden'],
tasks;
/**

View File

@ -1,5 +1,6 @@
var _ = require('lodash'),
config = require('../../config'),
labs = require('../../utils/labs'),
channelConfig;
channelConfig = function channelConfig() {
@ -13,13 +14,13 @@ channelConfig = function channelConfig() {
name: 'tag',
route: '/' + config.routeKeywords.tag + '/:slug/',
postOptions: {
filter: 'tags:\'%s\''
filter: labs.isSet('hiddenTags') ? 'tags:\'%s\'+tags.hidden:false' : 'tags:\'%s\''
},
data: {
tag: {
type: 'read',
resource: 'tags',
options: {slug: '%s'}
options: labs.isSet('hiddenTags') ? {slug: '%s', hidden: false} : {slug: '%s'}
}
},
slugTemplate: true,

View File

@ -9,6 +9,7 @@
var hbs = require('express-hbs'),
_ = require('lodash'),
config = require('../config'),
labs = require('../utils/labs'),
utils = require('./utils'),
tags;
@ -26,6 +27,10 @@ tags = function (options) {
output = '';
function createTagList(tags) {
if (labs.isSet('hiddenTags')) {
tags = _.filter(tags, 'hidden', false);
}
if (autolink) {
return _.map(tags, function (tag) {
return utils.linkTemplate({

View File

@ -33,8 +33,9 @@ tagUpdate = {
},
createTagThenAttachTagToPost: function createTagThenAttachTagToPost(TagModel, post, tag, index, options) {
var fields = ['name', 'slug', 'description', 'image', 'hidden', 'parent_id', 'meta_title', 'meta_description'];
return function () {
return TagModel.add({name: tag.name}, options).then(function then(createdTag) {
return TagModel.add(_.pick(tag, fields), options).then(function then(createdTag) {
return tagUpdate.attachTagToPost(post, createdTag, index, options)();
});
};

View File

@ -76,7 +76,8 @@ Tag = ghostBookshelf.Model.extend({
// these are the only options that can be passed to Bookshelf / Knex.
validOptions = {
findPage: ['page', 'limit', 'columns', 'filter', 'order'],
findAll: ['columns']
findAll: ['columns'],
findOne: ['hidden']
};
if (validOptions[methodName]) {

View File

@ -4,7 +4,7 @@ var config = require('../config'),
flagIsSet = function flagIsSet(flag) {
var labsConfig = config.labs;
return !!labsConfig[flag] && labsConfig[flag] === true;
return labsConfig && labsConfig[flag] && labsConfig[flag] === true;
};
module.exports.isSet = flagIsSet;

View File

@ -0,0 +1,54 @@
/*globals describe, afterEach, it*/
/*jshint expr:true*/
var should = require('should'),
sinon = require('sinon'),
// Stuff we are testing
labs = require('../../../../server/utils/labs'),
channelConfig = require('../../../../server/controllers/frontend/channel-config'),
sandbox = sinon.sandbox.create();
describe('Channel Config', function () {
afterEach(function () {
sandbox.restore();
});
it('should get the index config', function () {
var result = channelConfig('index');
should.exist(result);
result.name.should.eql('index');
});
it('should get the author config', function () {
var result = channelConfig('author');
should.exist(result);
result.name.should.eql('author');
});
it('should get the tag config', function () {
var result = channelConfig('tag');
should.exist(result);
result.name.should.eql('tag');
});
describe('hidden tags labs flag', function () {
it('should return old tag config if labs flag is not set', function () {
sandbox.stub(labs, 'isSet').returns(false);
var result = channelConfig('tag');
should.exist(result);
result.name.should.eql('tag');
result.postOptions.filter.should.eql('tags:%s');
result.data.tag.options.should.eql({slug: '%s'});
});
it('should return new tag config if labs flag is not set', function () {
sandbox.stub(labs, 'isSet').returns(true);
var result = channelConfig('tag');
should.exist(result);
result.name.should.eql('tag');
result.postOptions.filter.should.eql('tags:%s+tags.hidden:false');
result.data.tag.options.should.eql({slug: '%s', hidden: false});
});
});
});

View File

@ -1,18 +1,27 @@
/*globals describe, before, it*/
/*globals describe, afterEach, before, it*/
var should = require('should'),
sinon = require('sinon'),
hbs = require('express-hbs'),
utils = require('./utils'),
rewire = require('rewire'),
// Stuff we are testing
handlebars = hbs.handlebars,
helpers = rewire('../../../server/helpers');
labs = require('../../../server/utils/labs'),
helpers = rewire('../../../server/helpers'),
sandbox = sinon.sandbox.create();
describe('{{tags}} helper', function () {
before(function () {
utils.loadHelpers();
});
afterEach(function () {
sandbox.restore();
});
it('has loaded tags helper', function () {
should.exist(handlebars.helpers.tags);
});
@ -174,4 +183,29 @@ describe('{{tags}} helper', function () {
String(rendered).should.equal('<a href="/tag/foo-bar/">foo</a>, <a href="/tag/bar/">bar</a>, <a href="/tag/baz/">baz</a>');
});
describe('Hidden/Internal tags', function () {
it('Should output internal tags when the labs flag IS NOT set', function () {
sandbox.stub(labs, 'isSet').returns(false);
var tags = [{name: 'foo', slug: 'foo-bar', hidden: false}, {name: 'bar', slug: 'bar', hidden: true}],
rendered = helpers.tags.call(
{tags: tags}
);
should.exist(rendered);
String(rendered).should.equal('<a href="/tag/foo-bar/">foo</a>, <a href="/tag/bar/">bar</a>');
});
it('Should NOT output internal tags when the labs flag IS set', function () {
sandbox.stub(labs, 'isSet').returns(true);
var tags = [{name: 'foo', slug: 'foo-bar', hidden: false}, {name: 'bar', slug: 'bar', hidden: true}],
rendered = helpers.tags.call(
{tags: tags}
);
should.exist(rendered);
String(rendered).should.equal('<a href="/tag/foo-bar/">foo</a>');
});
});
});