0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Moved visibility utility from static model fn to Ghost-SDK

refs #10618

- Visibility methods don't belong on model, but are generic utils
- Used directly from ghost helper's visibility methods, cleans up core
- Removes direct model dependency of theme helper
- Updated `foreach_spec` to correct test data as per schema - visibility property cannot be empty
This commit is contained in:
Rish 2019-06-07 12:15:26 +05:30 committed by Rishabh Garg
parent ea37b78456
commit a4f119cb7f
7 changed files with 20 additions and 71 deletions

View file

@ -1,8 +1,8 @@
const models = require('../../models'); const ghostHelperUtils = require('@tryghost/helpers').utils;
function getKeywords(data) { function getKeywords(data) {
if (data.post && data.post.tags && data.post.tags.length > 0) { if (data.post && data.post.tags && data.post.tags.length > 0) {
return models.Base.Model.filterByVisibility(data.post.tags, ['public'], false, function processItem(item) { return ghostHelperUtils.visibility.filter(data.post.tags, ['public'], function processItem(item) {
return item.name; return item.name;
}); });
} }

View file

@ -9,7 +9,8 @@
const proxy = require('./proxy'); const proxy = require('./proxy');
const _ = require('lodash'); const _ = require('lodash');
const urlService = require('../services/url'); const urlService = require('../services/url');
const {SafeString, templates, models} = proxy; const {SafeString, templates} = proxy;
const ghostHelperUtils = require('@tryghost/helpers').utils;
module.exports = function authors(options = {}) { module.exports = function authors(options = {}) {
options.hash = options.hash || {}; options.hash = options.hash || {};
@ -25,7 +26,6 @@ module.exports = function authors(options = {}) {
to to
} = options.hash; } = options.hash;
let output = ''; let output = '';
const visibilityArr = models.Base.Model.parseVisibilityString(visibility);
autolink = !(_.isString(autolink) && autolink === 'false'); autolink = !(_.isString(autolink) && autolink === 'false');
limit = limit ? parseInt(limit, 10) : limit; limit = limit ? parseInt(limit, 10) : limit;
@ -40,7 +40,7 @@ module.exports = function authors(options = {}) {
}) : _.escape(author.name); }) : _.escape(author.name);
} }
return models.Base.Model.filterByVisibility(authors, visibilityArr, !!visibility, processAuthor); return ghostHelperUtils.visibility.filter(authors, visibility, processAuthor);
} }
if (this.authors && this.authors.length) { if (this.authors && this.authors.length) {

View file

@ -3,14 +3,9 @@
// //
// Block helper designed for looping through posts // Block helper designed for looping through posts
const _ = require('lodash'); const _ = require('lodash');
const {logging, i18n, models, hbs} = require('./proxy'); const {logging, i18n, hbs} = require('./proxy');
const {Utils: hbsUtils, handlebars: {createFrame}} = hbs; const {Utils: hbsUtils, handlebars: {createFrame}} = hbs;
const ghostHelperUtils = require('@tryghost/helpers').utils;
function filterItemsByVisibility(items, options) {
const visibilityArr = models.Base.Model.parseVisibilityString(options.hash.visibility);
return models.Base.Model.filterByVisibility(items, visibilityArr, !!options.hash.visibility);
}
module.exports = function foreach(items, options) { module.exports = function foreach(items, options) {
if (!options) { if (!options) {
@ -22,7 +17,7 @@ module.exports = function foreach(items, options) {
} }
// Exclude items which should not be visible in the theme // Exclude items which should not be visible in the theme
items = filterItemsByVisibility(items, options); items = ghostHelperUtils.visibility.filter(items, options.hash.visibility);
// Initial values set based on parameters sent through. If nothing sent, set to defaults // Initial values set based on parameters sent through. If nothing sent, set to defaults
const {fn, inverse, hash, data, ids} = options; const {fn, inverse, hash, data, ids} = options;

View file

@ -19,7 +19,6 @@ module.exports = {
// TODO: Expose less of the API to make this safe // TODO: Expose less of the API to make this safe
api: require('../api'), api: require('../api'),
models: require('../models'),
// TODO: Only expose "get" // TODO: Only expose "get"
settingsCache: settingsCache, settingsCache: settingsCache,

View file

@ -5,12 +5,13 @@
// By default, tags are separated by commas. // By default, tags are separated by commas.
// //
// Note that the standard {{#each tags}} implementation is unaffected by this helper // Note that the standard {{#each tags}} implementation is unaffected by this helper
const proxy = require('./proxy'), const proxy = require('./proxy');
_ = require('lodash'), const _ = require('lodash');
urlService = proxy.urlService, const ghostHelperUtils = require('@tryghost/helpers').utils;
SafeString = proxy.SafeString,
templates = proxy.templates, const urlService = proxy.urlService;
models = proxy.models; const SafeString = proxy.SafeString;
const templates = proxy.templates;
module.exports = function tags(options) { module.exports = function tags(options) {
options = options || {}; options = options || {};
@ -20,8 +21,7 @@ module.exports = function tags(options) {
separator = _.isString(options.hash.separator) ? options.hash.separator : ', ', separator = _.isString(options.hash.separator) ? options.hash.separator : ', ',
prefix = _.isString(options.hash.prefix) ? options.hash.prefix : '', prefix = _.isString(options.hash.prefix) ? options.hash.prefix : '',
suffix = _.isString(options.hash.suffix) ? options.hash.suffix : '', suffix = _.isString(options.hash.suffix) ? options.hash.suffix : '',
limit = options.hash.limit ? parseInt(options.hash.limit, 10) : undefined, limit = options.hash.limit ? parseInt(options.hash.limit, 10) : undefined;
visibilityArr = models.Base.Model.parseVisibilityString(options.hash.visibility);
let output = '', let output = '',
from = options.hash.from ? parseInt(options.hash.from, 10) : 1, from = options.hash.from ? parseInt(options.hash.from, 10) : 1,
@ -35,7 +35,7 @@ module.exports = function tags(options) {
}) : _.escape(tag.name); }) : _.escape(tag.name);
} }
return models.Base.Model.filterByVisibility(tags, visibilityArr, !!options.hash.visibility, processTag); return ghostHelperUtils.visibility.filter(tags, options.hash.visibility, processTag);
} }
if (this.tags && this.tags.length) { if (this.tags && this.tags.length) {

View file

@ -1155,51 +1155,6 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return result; return result;
}, },
/**
* All models which have a visibility property, can use this static helper function.
* Filter models by visibility.
*
* @param {Array|Object} items
* @param {Array} visibility
* @param {Boolean} [explicit]
* @param {Function} [fn]
* @returns {Array|Object} filtered items
*/
filterByVisibility: function filterByVisibility(items, visibility, explicit, fn) {
var memo = _.isArray(items) ? [] : {};
if (_.includes(visibility, 'all')) {
return fn ? _.map(items, fn) : items;
}
// We don't want to change the structure of what is returned
return _.reduce(items, function (items, item, key) {
if (!item.visibility && !explicit || _.includes(visibility, item.visibility)) {
var newItem = fn ? fn(item) : item;
if (_.isArray(items)) {
memo.push(newItem);
} else {
memo[key] = newItem;
}
}
return memo;
}, memo);
},
/**
* Returns an Array of visibility values.
* e.g. public,all => ['public, 'all']
* @param visibility
* @returns {*}
*/
parseVisibilityString: function parseVisibilityString(visibility) {
if (!visibility) {
return ['public'];
}
return _.map(visibility.split(','), _.trim);
},
/** /**
* If you want to fetch all data fast, i recommend using this function. * If you want to fetch all data fast, i recommend using this function.
* Bookshelf is just too slow, too much ORM overhead. * Bookshelf is just too slow, too much ORM overhead.

View file

@ -506,7 +506,7 @@ describe('{{#foreach}} helper', function () {
{name: 'second', visibility: 'public'}, {name: 'second', visibility: 'public'},
{name: 'third', visibility: 'internal'}, {name: 'third', visibility: 'internal'},
{name: 'fourth', visibility: 'public'}, {name: 'fourth', visibility: 'public'},
{name: 'fifth'} {name: 'fifth', visibility: 'public'}
] ]
}, },
tagObjectHash = { tagObjectHash = {
@ -515,7 +515,7 @@ describe('{{#foreach}} helper', function () {
second: {name: 'second', visibility: 'public'}, second: {name: 'second', visibility: 'public'},
third: {name: 'third', visibility: 'internal'}, third: {name: 'third', visibility: 'internal'},
fourth: {name: 'fourth', visibility: 'public'}, fourth: {name: 'fourth', visibility: 'public'},
fifth: {name: 'fifth'} fifth: {name: 'fifth', visibility: 'public'}
} }
}; };
@ -545,7 +545,7 @@ describe('{{#foreach}} helper', function () {
it('should output all tags with visibility property set with visibility="public,internal"', function () { it('should output all tags with visibility property set with visibility="public,internal"', function () {
var templateString = '<ul>{{#foreach tags visibility="public,internal"}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>', var templateString = '<ul>{{#foreach tags visibility="public,internal"}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>',
expected = '<ul><li>0 first</li><li>1 second</li><li>2 third</li><li>3 fourth</li></ul>'; expected = '<ul><li>0 first</li><li>1 second</li><li>2 third</li><li>3 fourth</li><li>4 fifth</li></ul>';
shouldCompileToExpected(templateString, tagObjectHash, expected); shouldCompileToExpected(templateString, tagObjectHash, expected);
shouldCompileToExpected(templateString, tagArrayHash, expected); shouldCompileToExpected(templateString, tagArrayHash, expected);