forked from TryGhost/Ghost
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes TryGhost#4439 - adds get helper + tests
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// # Get Helper | ||
// Usage: `{{#get "posts" limit="5"}}`, `{{#get "tags" limit="all"}}` | ||
// Fetches data from the API | ||
|
||
var _ = require('lodash'), | ||
hbs = require('express-hbs'), | ||
errors = require('../errors'), | ||
api = require('../api'), | ||
jsonpath = require('jsonpath'), | ||
|
||
resources = ['posts', 'tags', 'users'], | ||
pathAliases = { | ||
'post.tags': 'post.tags[*].slug', | ||
'post.author': 'post.author.slug' | ||
}, | ||
get; | ||
|
||
function doBrowse(context, options) { | ||
var browse = true; | ||
if (options.limit && options.limit === 1) { | ||
browse = false; | ||
} | ||
|
||
if (options.id || options.slug) { | ||
browse = false; | ||
} | ||
|
||
return browse; | ||
} | ||
|
||
function resolvePaths(data, value) { | ||
var regex = /\{\{(.*?)\}\}/g; | ||
|
||
value = value.replace(regex, function (match, path) { | ||
var result; | ||
|
||
// Handle tag, author and role aliases | ||
path = pathAliases[path] ? pathAliases[path] : path; | ||
|
||
result = jsonpath.query(data, path); | ||
|
||
if (_.isArray(result)) { | ||
result = result.join(','); | ||
} | ||
|
||
return result; | ||
}); | ||
|
||
return value; | ||
} | ||
|
||
function parseOptions(data, options) { | ||
options = _.omit(options.hash, 'context'); | ||
if (_.isArray(options.tag)) { | ||
options.tag = _.pluck(options.tag, 'slug').join(','); | ||
} | ||
|
||
if (_.isString(options.filter)) { | ||
options.filter = resolvePaths(data, options.filter); | ||
} | ||
|
||
return options; | ||
} | ||
|
||
get = function get(context, options) { | ||
options = options || {}; | ||
options.hash = options.hash || {}; | ||
|
||
var self = this, | ||
data, | ||
apiOptions, | ||
apiMethod; | ||
|
||
if (!_.contains(resources, context)) { | ||
errors.logWarn('Invalid resource given to get helper'); | ||
return; | ||
} | ||
|
||
if (options.data) { | ||
data = hbs.handlebars.createFrame(options.data); | ||
} | ||
|
||
// Determine if this is a read or browse | ||
apiMethod = doBrowse(context, options) ? api[context].browse : api[context].read; | ||
// Parse the options we're going to pass to the API | ||
apiOptions = parseOptions(this, options); | ||
|
||
return apiMethod(apiOptions).then(function success(result) { | ||
var data = _.merge(self, result); | ||
if (_.isEmpty(result[context])) { | ||
return options.inverse(self); | ||
} | ||
|
||
return options.fn(data, { | ||
data: data, | ||
blockParams: [result[context]] | ||
}); | ||
}).catch(function error(err) { | ||
if (data) { | ||
data.error = err.message; | ||
} | ||
return options.inverse(self, {data: data}); | ||
}); | ||
}; | ||
|
||
module.exports = get; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*globals describe, before, beforeEach, afterEach, it*/ | ||
/*jshint expr:true*/ | ||
var should = require('should'), | ||
sinon = require('sinon'), | ||
hbs = require('express-hbs'), | ||
Promise = require('bluebird'), | ||
utils = require('./utils'), | ||
|
||
// Stuff we are testing | ||
handlebars = hbs.handlebars, | ||
helpers = require('../../../server/helpers'), | ||
api = require('../../../server/api'); | ||
|
||
describe('{{#get}} helper', function () { | ||
var sandbox; | ||
|
||
before(function () { | ||
utils.loadHelpers(); | ||
}); | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('has loaded get block helper', function () { | ||
should.exist(handlebars.helpers.get); | ||
}); | ||
|
||
describe('posts', function () { | ||
var testPostsObj = { | ||
posts: [ | ||
{id: 1} | ||
] | ||
}; | ||
beforeEach(function () { | ||
sandbox.stub(api.posts, 'browse', function () { | ||
return Promise.resolve(testPostsObj); | ||
}); | ||
}); | ||
|
||
it('should handle default posts call', function (done) { | ||
var fn = sinon.spy(), | ||
inverse = sinon.spy(); | ||
|
||
helpers.get.call( | ||
{}, | ||
'posts', | ||
{hash: {}, fn: fn, inverse: inverse} | ||
).then(function () { | ||
fn.called.should.be.true; | ||
fn.firstCall.args[0].should.be.an.Object; | ||
fn.firstCall.args[0].should.eql(testPostsObj); | ||
inverse.called.should.be.false; | ||
|
||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters