From 157cf5c3b99ab60c1f9cc29033741b293a413a63 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 5 Aug 2014 16:42:42 +0100 Subject: [PATCH] Fix the author context block fixes #3599 - If the author helper is called as a block (i.e. fn is present) then treat it as a with call --- core/server/helpers/index.js | 12 ++++++++---- core/test/unit/server_helpers_index_spec.js | 15 +++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index f236d31ec1..8e7842dff8 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -202,11 +202,15 @@ coreHelpers.asset = function (context, options) { // Returns the full name of the author of a given post, or a blank string // if the author could not be determined. // -coreHelpers.author = function (options) { - options = options || {}; - options.hash = options.hash || {}; +coreHelpers.author = function (context, options) { + if (_.isUndefined(options)) { + options = context; + } + + if (options.fn) { + return hbs.handlebars.helpers['with'].call(this, this.author, options); + } - /*jshint unused:false*/ var autolink = _.isString(options.hash.autolink) && options.hash.autolink === 'false' ? false : true, output = ''; diff --git a/core/test/unit/server_helpers_index_spec.js b/core/test/unit/server_helpers_index_spec.js index 541822779a..7d614aebae 100644 --- a/core/test/unit/server_helpers_index_spec.js +++ b/core/test/unit/server_helpers_index_spec.js @@ -209,7 +209,7 @@ describe('Core Helpers', function () { it('Returns the link to the author from the context', function () { var data = {'author': {'name': 'abc 123', slug: 'abc123', bio: '', website: '', status: '', location: ''}}, - result = helpers.author.call(data); + result = helpers.author.call(data, {hash: {}}); String(result).should.equal('abc 123'); }); @@ -224,13 +224,22 @@ describe('Core Helpers', function () { it('Returns a blank string where author data is missing', function () { var data = {'author': null}, - result = helpers.author.call(data); + result = helpers.author.call(data, {hash: {}}); String(result).should.equal(''); }); + it('Functions as block helper if called with #', function () { + var data = {'author': {'name': 'abc 123', slug: 'abc123'}}, + // including fn emulates the # + result = helpers.author.call(data, {hash: {}, fn: function () { return 'FN'; }}); + + // It outputs the result of fn + String(result).should.equal('FN'); + }); }); + describe('encode Helper', function () { it('has loaded encode helper', function () { @@ -247,8 +256,6 @@ describe('Core Helpers', function () { }); }); - - describe('Plural Helper', function () { it('has loaded plural helper', function () {