Added newsletter in get helper

refs https://github.com/TryGhost/Product/issues/3859

---------

Co-authored-by: Michael Barrett <mike182uk@gmail.com>
This commit is contained in:
Princi Vershwal 2023-09-15 11:48:22 +05:30 committed by GitHub
parent 99f29a169c
commit 3436c4870d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 0 deletions

View File

@ -32,6 +32,14 @@ module.exports = function foreach(items, options) {
visibility = visibility || 'all';
}
}
if (_.isArray(items) && items.length > 0 && checks.isNewsletter(items[0])) {
visibility = visibility || 'all';
} else if (_.isObject(items) && _.isArray(Object.values(items))) {
if (Object.values(items).length > 0 && checks.isNewsletter(Object.values(items)[0])) {
visibility = visibility || 'all';
}
}
// Exclude items which should not be visible in the theme
items = ghostHelperUtils.visibility.filter(items, visibility);

View File

@ -33,6 +33,9 @@ const RESOURCES = {
},
tiers: {
alias: 'tiersPublic'
},
newsletters: {
alias: 'newslettersPublic'
}
};

View File

@ -3,6 +3,11 @@ function isPost(jsonData) {
Object.prototype.hasOwnProperty.call(jsonData, 'title') && Object.prototype.hasOwnProperty.call(jsonData, 'slug');
}
function isNewsletter(jsonData) {
return Object.prototype.hasOwnProperty.call(jsonData, 'name') &&
Object.prototype.hasOwnProperty.call(jsonData, 'subscribe_on_signup') && Object.prototype.hasOwnProperty.call(jsonData, 'visibility');
}
function isPage(jsonData = {}) {
return Object.prototype.hasOwnProperty.call(jsonData, 'show_title_and_feature_image');
}
@ -24,6 +29,7 @@ function isNav(jsonData) {
module.exports = {
isPost,
isNewsletter,
isPage,
isTag,
isUser,

View File

@ -402,6 +402,27 @@ describe('{{#foreach}} helper', function () {
shouldCompileToExpected(templateString, objectHashWithVis, expected);
});
it('foreach with newsletters with members visibility', function () {
const newsletterObjectHashWithVisibility = {
newsletters: {
first: {name: 'first', visibility: 'members', subscribe_on_signup: true},
second: {name: 'second', visibility: 'members', subscribe_on_signup: true},
third: {name: 'third', visibility: 'paid', subscribe_on_signup: false}
}
};
const newsletterArrayHashWithVisibility = {
newsletters: [
{name: 'first', visibility: 'members', subscribe_on_signup: true},
{name: 'second', visibility: 'members', subscribe_on_signup: true},
{name: 'third', visibility: 'paid', subscribe_on_signup: false}
]
};
const templateString = '<ul>{{#foreach newsletters}}<li>{{name}}</li>{{else}}not this{{/foreach}}</ul>';
const expected = '<ul><li>first</li><li>second</li><li>third</li></ul>';
shouldCompileToExpected(templateString, newsletterObjectHashWithVisibility, expected);
shouldCompileToExpected(templateString, newsletterArrayHashWithVisibility, expected);
});
it('foreach with from 2', function () {
const templateString = '<ul>{{#foreach posts from="2"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>';
const expected = '<ul><li>second</li><li>third</li><li>fourth</li><li>fifth</li></ul>';

View File

@ -96,6 +96,35 @@ describe('{{#get}} helper', function () {
});
});
describe('newsletters', function () {
const meta = {pagination: {}};
beforeEach(function () {
locals = {root: {_locals: {}}};
sinon.stub(api, 'newslettersPublic').get(() => {
return {
browse: sinon.stub().resolves({newsletters: [], meta: meta})
};
});
});
it('browse newsletters', function (done) {
get.call(
{},
'newsletters',
{hash: {}, data: locals, fn: fn, inverse: inverse}
).then(function () {
fn.called.should.be.true();
fn.firstCall.args[0].should.be.an.Object().with.property('newsletters');
fn.firstCall.args[0].newsletters.should.eql([]);
inverse.called.should.be.false();
done();
}).catch(done);
});
});
describe('general error handling', function () {
it('should return an error for an unknown resource', function (done) {
get.call(

View File

@ -5,6 +5,7 @@ describe('Checks', function () {
it('methods', function () {
Object.keys(checks).should.eql([
'isPost',
'isNewsletter',
'isPage',
'isTag',
'isUser',
@ -26,6 +27,13 @@ describe('Checks', function () {
checks.isPage({title: 'Test', show_title_and_feature_image: true}).should.eql(true);
});
it('isNewsletter', function () {
checks.isNewsletter({}).should.eql(false);
checks.isNewsletter({name: 'Test'}).should.eql(false);
checks.isNewsletter({name: 'Test', visibility: 'members', subscribe_on_signup: true}).should.eql(true);
checks.isNewsletter({name: 'Test', visibility: 'paid', subscribe_on_signup: false}).should.eql(true);
});
it('isTag', function () {
checks.isTag({}).should.eql(false);
checks.isTag({name: 'Test'}).should.eql(false);