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>"),
isProduction = process.env.NODE_ENV === 'production',
api = require('../api'),
config = require('../config'),
coreHelpers = {},
registerHelpers;
@ -96,11 +97,11 @@ coreHelpers.url = function (options) {
slug: function () { return self.slug; },
id: function () { return self.id; }
},
path = coreHelpers.config.paths().path,
path = config.paths().path,
isAbsolute = options && options.hash.absolute;
return api.settings.read('permalinks').then(function (permalinks) {
if (isAbsolute) {
output += coreHelpers.config().url;
output += config().url;
}
if (path && path !== '/') {
output += path;
@ -127,7 +128,7 @@ coreHelpers.url = function (options) {
// flag outputs the asset path for the Ghost admin
coreHelpers.asset = function (context, options) {
var output = '',
subDir = coreHelpers.config.paths().path,
subDir = config.paths().path,
isAdmin = options && options.hash && options.hash.ghost;
if (subDir === '/') {
@ -258,15 +259,15 @@ coreHelpers.excerpt = function (options) {
// Returns the config value for fileStorage.
coreHelpers.fileStorage = function (context, options) {
/*jslint unparam:true*/
if (coreHelpers.config().hasOwnProperty('fileStorage')) {
return coreHelpers.config().fileStorage.toString();
if (config().hasOwnProperty('fileStorage')) {
return config().fileStorage.toString();
}
return "true";
};
coreHelpers.ghostScriptTags = function () {
var scriptFiles = [],
webroot = coreHelpers.config.paths().webroot;
webroot = config.paths().webroot;
if (isProduction) {
scriptFiles.push("ghost.min.js");
@ -349,8 +350,8 @@ coreHelpers.post_class = function (options) {
coreHelpers.ghost_head = function (options) {
/*jslint unparam:true*/
var blog = coreHelpers.config.theme(),
root = coreHelpers.config.paths().webroot,
var blog = config.theme(),
root = config.paths().webroot,
head = [],
majorMinor = /^(\d+\.)?(\d+)/,
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/' + '">');
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) {
@ -373,7 +374,7 @@ coreHelpers.ghost_head = function (options) {
coreHelpers.ghost_foot = function (options) {
/*jslint unparam:true*/
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) {
var footString = _.reduce(foot, function (memo, item) { return memo + ' ' + item; }, '');
@ -387,7 +388,7 @@ coreHelpers.meta_title = function (options) {
blog;
if (_.isString(this.ghostRoot)) {
if (!this.ghostRoot || this.ghostRoot === '/' || this.ghostRoot === '' || this.ghostRoot.match(/\/page/)) {
blog = coreHelpers.config.theme();
blog = config.theme();
title = blog.title;
} else {
title = this.post.title;
@ -407,7 +408,7 @@ coreHelpers.meta_description = function (options) {
if (_.isString(this.ghostRoot)) {
if (!this.ghostRoot || this.ghostRoot === '/' || this.ghostRoot === '' || this.ghostRoot.match(/\/page/)) {
blog = coreHelpers.config.theme();
blog = config.theme();
description = blog.description;
} else {
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) {
var fn = options.fn,
inverse = options.inverse,
@ -520,24 +516,6 @@ coreHelpers.foreach = function (context, options) {
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}}`
// Outputs previous and next buttons, along with info about the current page
@ -608,10 +586,7 @@ function registerAsyncAdminHelper(name, fn) {
registerHelpers = function (config, adminHbs) {
// And expose config
coreHelpers.config = config;
registerHelpers = function (adminHbs) {
// Expose hbs instance for admin
coreHelpers.adminHbs = adminHbs;
@ -630,16 +605,8 @@ registerHelpers = function (config, adminHbs) {
registerThemeHelper('excerpt', coreHelpers.excerpt);
registerThemeHelper('fileStorage', coreHelpers.fileStorage);
registerThemeHelper('foreach', coreHelpers.foreach);
registerThemeHelper('ghostScriptTags', coreHelpers.ghostScriptTags);
registerThemeHelper('has_tag', coreHelpers.has_tag);
registerThemeHelper('json', coreHelpers.json);
registerThemeHelper('pageUrl', coreHelpers.pageUrl);
registerThemeHelper('pagination', coreHelpers.pagination);
@ -667,8 +634,12 @@ registerHelpers = function (config, adminHbs) {
registerAdminHelper('asset', coreHelpers.asset);
registerAdminHelper('ghostScriptTags', coreHelpers.ghostScriptTags);
};
registerAdminHelper('fileStorage', coreHelpers.fileStorage);
registerAsyncAdminHelper('url', coreHelpers.url);
};
module.exports = coreHelpers;
module.exports.loadCoreHelpers = registerHelpers;

View File

@ -106,12 +106,10 @@ function setup(server) {
permissions.init()
);
}).then(function () {
// Initialise mail after first run,
// passing in config module to prevent
// circular dependencies.
// Initialize mail
return mailer.init();
}).then(function () {
var adminHbs;
var adminHbs = hbs.create();
// ##Configuration
@ -123,12 +121,10 @@ function setup(server) {
server.set('view engine', 'hbs');
// 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'}));
// Load helpers
helpers.loadCoreHelpers(config, adminHbs);
helpers.loadCoreHelpers(adminHbs);
// ## Middleware
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
hbs.express3({ partialsDir: [config.paths().helperTemplates] });
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);
}
});
});
});
});