Merge pull request #1675 from sebgie/issue#1640

Improve Helpers
This commit is contained in:
Hannah Wolfe 2013-12-15 03:41:03 -08:00
commit 088dac6099
3 changed files with 23 additions and 80 deletions

View File

@ -14,6 +14,7 @@ var _ = require('underscore'),
scriptTemplate = _.template("<script src='<%= source %>?v=<%= version %>'></script>"), scriptTemplate = _.template("<script src='<%= source %>?v=<%= version %>'></script>"),
isProduction = process.env.NODE_ENV === 'production', isProduction = process.env.NODE_ENV === 'production',
api = require('../api'), api = require('../api'),
config = require('../config'),
coreHelpers = {}, coreHelpers = {},
registerHelpers; registerHelpers;
@ -96,11 +97,11 @@ coreHelpers.url = function (options) {
slug: function () { return self.slug; }, slug: function () { return self.slug; },
id: function () { return self.id; } id: function () { return self.id; }
}, },
path = coreHelpers.config.paths().path, path = config.paths().path,
isAbsolute = options && options.hash.absolute; isAbsolute = options && options.hash.absolute;
return api.settings.read('permalinks').then(function (permalinks) { return api.settings.read('permalinks').then(function (permalinks) {
if (isAbsolute) { if (isAbsolute) {
output += coreHelpers.config().url; output += config().url;
} }
if (path && path !== '/') { if (path && path !== '/') {
output += path; output += path;
@ -127,7 +128,7 @@ coreHelpers.url = function (options) {
// flag outputs the asset path for the Ghost admin // flag outputs the asset path for the Ghost admin
coreHelpers.asset = function (context, options) { coreHelpers.asset = function (context, options) {
var output = '', var output = '',
subDir = coreHelpers.config.paths().path, subDir = config.paths().path,
isAdmin = options && options.hash && options.hash.ghost; isAdmin = options && options.hash && options.hash.ghost;
if (subDir === '/') { if (subDir === '/') {
@ -258,15 +259,15 @@ coreHelpers.excerpt = function (options) {
// Returns the config value for fileStorage. // Returns the config value for fileStorage.
coreHelpers.fileStorage = function (context, options) { coreHelpers.fileStorage = function (context, options) {
/*jslint unparam:true*/ /*jslint unparam:true*/
if (coreHelpers.config().hasOwnProperty('fileStorage')) { if (config().hasOwnProperty('fileStorage')) {
return coreHelpers.config().fileStorage.toString(); return config().fileStorage.toString();
} }
return "true"; return "true";
}; };
coreHelpers.ghostScriptTags = function () { coreHelpers.ghostScriptTags = function () {
var scriptFiles = [], var scriptFiles = [],
webroot = coreHelpers.config.paths().webroot; webroot = config.paths().webroot;
if (isProduction) { if (isProduction) {
scriptFiles.push("ghost.min.js"); scriptFiles.push("ghost.min.js");
@ -349,8 +350,8 @@ coreHelpers.post_class = function (options) {
coreHelpers.ghost_head = function (options) { coreHelpers.ghost_head = function (options) {
/*jslint unparam:true*/ /*jslint unparam:true*/
var blog = coreHelpers.config.theme(), var blog = config.theme(),
root = coreHelpers.config.paths().webroot, root = config.paths().webroot,
head = [], head = [],
majorMinor = /^(\d+\.)?(\d+)/, majorMinor = /^(\d+\.)?(\d+)/,
trimmedVersion = this.version; trimmedVersion = this.version;
@ -361,7 +362,7 @@ coreHelpers.ghost_head = function (options) {
head.push('<link rel="alternate" type="application/rss+xml" title="' + _.escape(blog.title) + '" href="' + root + '/rss/' + '">'); head.push('<link rel="alternate" type="application/rss+xml" title="' + _.escape(blog.title) + '" href="' + root + '/rss/' + '">');
if (this.ghostRoot) { if (this.ghostRoot) {
head.push('<link rel="canonical" href="' + coreHelpers.config().url + this.ghostRoot + '" />'); head.push('<link rel="canonical" href="' + config().url + this.ghostRoot + '" />');
} }
return filters.doFilter('ghost_head', head).then(function (head) { return filters.doFilter('ghost_head', head).then(function (head) {
@ -373,7 +374,7 @@ coreHelpers.ghost_head = function (options) {
coreHelpers.ghost_foot = function (options) { coreHelpers.ghost_foot = function (options) {
/*jslint unparam:true*/ /*jslint unparam:true*/
var foot = []; var foot = [];
foot.push('<script src="' + coreHelpers.config().url + '/shared/vendor/jquery/jquery.js"></script>'); foot.push('<script src="' + config().url + '/shared/vendor/jquery/jquery.js"></script>');
return filters.doFilter('ghost_foot', foot).then(function (foot) { return filters.doFilter('ghost_foot', foot).then(function (foot) {
var footString = _.reduce(foot, function (memo, item) { return memo + ' ' + item; }, ''); var footString = _.reduce(foot, function (memo, item) { return memo + ' ' + item; }, '');
@ -387,7 +388,7 @@ coreHelpers.meta_title = function (options) {
blog; blog;
if (_.isString(this.ghostRoot)) { if (_.isString(this.ghostRoot)) {
if (!this.ghostRoot || this.ghostRoot === '/' || this.ghostRoot === '' || this.ghostRoot.match(/\/page/)) { if (!this.ghostRoot || this.ghostRoot === '/' || this.ghostRoot === '' || this.ghostRoot.match(/\/page/)) {
blog = coreHelpers.config.theme(); blog = config.theme();
title = blog.title; title = blog.title;
} else { } else {
title = this.post.title; title = this.post.title;
@ -407,7 +408,7 @@ coreHelpers.meta_description = function (options) {
if (_.isString(this.ghostRoot)) { if (_.isString(this.ghostRoot)) {
if (!this.ghostRoot || this.ghostRoot === '/' || this.ghostRoot === '' || this.ghostRoot.match(/\/page/)) { if (!this.ghostRoot || this.ghostRoot === '/' || this.ghostRoot === '' || this.ghostRoot.match(/\/page/)) {
blog = coreHelpers.config.theme(); blog = config.theme();
description = blog.description; description = blog.description;
} else { } else {
description = ''; description = '';
@ -445,11 +446,6 @@ coreHelpers.e = function (key, defaultString, options) {
}); });
}; };
coreHelpers.json = function (object, options) {
/*jslint unparam:true*/
return JSON.stringify(object);
};
coreHelpers.foreach = function (context, options) { coreHelpers.foreach = function (context, options) {
var fn = options.fn, var fn = options.fn,
inverse = options.inverse, inverse = options.inverse,
@ -520,24 +516,6 @@ coreHelpers.foreach = function (context, options) {
return ret; return ret;
}; };
// ### Check if a tag is contained on the tag list
//
// Usage example:*
//
// `{{#has_tag "test"}} {{tags}} {{else}} no tags {{/has_tag}}`
//
// @param {String} tag name to search on tag list
// @return {String} list of tags formatted according to `tag` helper
//
coreHelpers.has_tag = function (name, options) {
if (_.isArray(this.tags) && !_.isEmpty(this.tags)) {
return (!_.isEmpty(_.filter(this.tags, function (tag) {
return (_.has(tag, "name") && tag.name === name);
}))) ? options.fn(this) : options.inverse(this);
}
return options.inverse(this);
};
// ### Pagination Helper // ### Pagination Helper
// `{{pagination}}` // `{{pagination}}`
// Outputs previous and next buttons, along with info about the current page // Outputs previous and next buttons, along with info about the current page
@ -608,10 +586,7 @@ function registerAsyncAdminHelper(name, fn) {
registerHelpers = function (config, adminHbs) { registerHelpers = function (adminHbs) {
// And expose config
coreHelpers.config = config;
// Expose hbs instance for admin // Expose hbs instance for admin
coreHelpers.adminHbs = adminHbs; coreHelpers.adminHbs = adminHbs;
@ -630,16 +605,8 @@ registerHelpers = function (config, adminHbs) {
registerThemeHelper('excerpt', coreHelpers.excerpt); registerThemeHelper('excerpt', coreHelpers.excerpt);
registerThemeHelper('fileStorage', coreHelpers.fileStorage);
registerThemeHelper('foreach', coreHelpers.foreach); registerThemeHelper('foreach', coreHelpers.foreach);
registerThemeHelper('ghostScriptTags', coreHelpers.ghostScriptTags);
registerThemeHelper('has_tag', coreHelpers.has_tag);
registerThemeHelper('json', coreHelpers.json);
registerThemeHelper('pageUrl', coreHelpers.pageUrl); registerThemeHelper('pageUrl', coreHelpers.pageUrl);
registerThemeHelper('pagination', coreHelpers.pagination); registerThemeHelper('pagination', coreHelpers.pagination);
@ -667,8 +634,12 @@ registerHelpers = function (config, adminHbs) {
registerAdminHelper('asset', coreHelpers.asset); registerAdminHelper('asset', coreHelpers.asset);
registerAdminHelper('ghostScriptTags', coreHelpers.ghostScriptTags); registerAdminHelper('ghostScriptTags', coreHelpers.ghostScriptTags);
};
registerAdminHelper('fileStorage', coreHelpers.fileStorage);
registerAsyncAdminHelper('url', coreHelpers.url);
};
module.exports = coreHelpers; module.exports = coreHelpers;
module.exports.loadCoreHelpers = registerHelpers; module.exports.loadCoreHelpers = registerHelpers;

View File

@ -106,12 +106,10 @@ function setup(server) {
permissions.init() permissions.init()
); );
}).then(function () { }).then(function () {
// Initialise mail after first run, // Initialize mail
// passing in config module to prevent
// circular dependencies.
return mailer.init(); return mailer.init();
}).then(function () { }).then(function () {
var adminHbs; var adminHbs = hbs.create();
// ##Configuration // ##Configuration
@ -123,12 +121,10 @@ function setup(server) {
server.set('view engine', 'hbs'); server.set('view engine', 'hbs');
// Create a hbs instance for admin and init view engine // Create a hbs instance for admin and init view engine
adminHbs = hbs.create();
server.set('admin view engine', adminHbs.express3({partialsDir: config.paths().adminViews + 'partials'})); server.set('admin view engine', adminHbs.express3({partialsDir: config.paths().adminViews + 'partials'}));
// Load helpers // Load helpers
helpers.loadCoreHelpers(config, adminHbs); helpers.loadCoreHelpers(adminHbs);
// ## Middleware // ## Middleware
middleware(server, dbHash); middleware(server, dbHash);

View File

@ -36,7 +36,7 @@ describe('Core Helpers', function () {
}; };
}); });
helpers.loadCoreHelpers(config, adminHbs); helpers.loadCoreHelpers(adminHbs);
// Load template helpers in handlebars // Load template helpers in handlebars
hbs.express3({ partialsDir: [config.paths().helperTemplates] }); hbs.express3({ partialsDir: [config.paths().helperTemplates] });
hbs.cachePartials(function(){ hbs.cachePartials(function(){
@ -580,28 +580,4 @@ describe('Core Helpers', function () {
}); });
describe("has_tag helper", function () {
var tags = [{name: 'haunted'}, {name: 'ghost'}];
it('has loaded has_tag helper', function () {
should.exist(handlebars.helpers.has_tag);
});
it('can call function if tag is found', function () {
helpers.has_tag.call({tags: tags}, 'haunted', {
fn: function (tags) {
should.exist(tags);
}
});
});
it('can call inverse function if tag is not found', function () {
helpers.has_tag.call({tags: tags}, 'undefined', {
inverse: function (tags) {
should.exist(tags);
}
});
});
});
}); });