mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-28 13:22:39 +03:00
Merge pull request #2153 from halfdan/2115-has-block-helper
This commit is contained in:
commit
5524787509
@ -572,6 +572,37 @@ coreHelpers.foreach = function (context, options) {
|
||||
return ret;
|
||||
};
|
||||
|
||||
// ### Has Helper
|
||||
// `{{#has tag="video, music"}}`
|
||||
// Checks whether a post has at least one of the tags
|
||||
coreHelpers.has = function (options) {
|
||||
var tags = _.pluck(this.tags, 'name'),
|
||||
tagList = options && options.hash ? options.hash.tag : false;
|
||||
|
||||
function evaluateTagList(expr, tags) {
|
||||
return expr.split(',').map(function (v) {
|
||||
return v.trim();
|
||||
}).reduce(function (p, c) {
|
||||
return p || (_.findIndex(tags, function (item) {
|
||||
// Escape regex special characters
|
||||
item = item.replace(/[\-\/\\\^$*+?.()|\[\]{}]/g, '\\$&');
|
||||
item = new RegExp(item, 'i');
|
||||
return item.test(c);
|
||||
}) !== -1);
|
||||
}, false);
|
||||
}
|
||||
|
||||
if (!tagList) {
|
||||
errors.logWarn("Invalid or no attribute given to has helper");
|
||||
return;
|
||||
}
|
||||
|
||||
if (tagList && evaluateTagList(tagList, tags)) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
};
|
||||
|
||||
// ### Pagination Helper
|
||||
// `{{pagination}}`
|
||||
// Outputs previous and next buttons, along with info about the current page
|
||||
@ -702,6 +733,8 @@ registerHelpers = function (adminHbs, assetHash) {
|
||||
|
||||
registerThemeHelper('foreach', coreHelpers.foreach);
|
||||
|
||||
registerThemeHelper('has', coreHelpers.has);
|
||||
|
||||
registerThemeHelper('page_url', coreHelpers.page_url);
|
||||
|
||||
registerThemeHelper('pageUrl', coreHelpers.pageUrl);
|
||||
|
@ -427,6 +427,64 @@ describe('Core Helpers', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('has Block Helper', function () {
|
||||
it('has loaded has block helper', function () {
|
||||
should.exist(handlebars.helpers.has);
|
||||
});
|
||||
|
||||
it('should handle tag list that validates true', function () {
|
||||
var fn = sinon.spy(),
|
||||
inverse = sinon.spy();
|
||||
|
||||
helpers.has.call(
|
||||
{tags: [{ name: 'foo'}, { name: 'bar'}, { name: 'baz'}]},
|
||||
{hash: { tag: 'invalid, bar, wat'}, fn: fn, inverse: inverse}
|
||||
);
|
||||
|
||||
fn.called.should.be.true;
|
||||
inverse.called.should.be.false;
|
||||
});
|
||||
|
||||
it('should handle tags with case-insensitivity', function () {
|
||||
var fn = sinon.spy(),
|
||||
inverse = sinon.spy();
|
||||
|
||||
helpers.has.call(
|
||||
{tags: [{ name: 'ghost'}]},
|
||||
{hash: { tag: 'GhoSt'}, fn: fn, inverse: inverse}
|
||||
);
|
||||
|
||||
fn.called.should.be.true;
|
||||
inverse.called.should.be.false;
|
||||
});
|
||||
|
||||
it('should handle tag list that validates false', function () {
|
||||
var fn = sinon.spy(),
|
||||
inverse = sinon.spy();
|
||||
|
||||
helpers.has.call(
|
||||
{tags: [{ name: 'foo'}, { name: 'bar'}, { name: 'baz'}]},
|
||||
{hash: { tag: 'much, such, wow'}, fn: fn, inverse: inverse}
|
||||
);
|
||||
|
||||
fn.called.should.be.false;
|
||||
inverse.called.should.be.true;
|
||||
});
|
||||
|
||||
it('should not do anything when an invalid attribute is given', function () {
|
||||
var fn = sinon.spy(),
|
||||
inverse = sinon.spy();
|
||||
|
||||
helpers.has.call(
|
||||
{tags: [{ name: 'foo'}, { name: 'bar'}, { name: 'baz'}]},
|
||||
{hash: { invalid: 'nonsense'}, fn: fn, inverse: inverse}
|
||||
);
|
||||
|
||||
fn.called.should.be.false;
|
||||
inverse.called.should.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('url Helper', function () {
|
||||
it('has loaded url helper', function () {
|
||||
should.exist(handlebars.helpers.url);
|
||||
|
Loading…
Reference in New Issue
Block a user