mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-24 23:48:13 -05:00
Fixed {{price}} helper to render empty instead of throwing
refs https://github.com/TryGhost/Toolbox/issues/497
refs fb7532bf5d
- We downgraded the 'GS090-NO-PRICE-DATA-CURRENCY-CONTEXT' rule in gscan to non-fatal, meaning Ghost should not be throwing an error but instead render an empty value for {{price}} helper when price data is empty.
- For example, a legacy syntax like this: '{{price currency=@price.currency}}' should not cause a page render error but return an empty price string.
- The pattern of returning an empty string instead of crashing is used in other helpers like {{img_url}} and and {{url}}
This commit is contained in:
parent
249942892a
commit
25d81c99ca
2 changed files with 29 additions and 22 deletions
|
@ -8,10 +8,10 @@
|
||||||
// Usage: `{{price 500 currency="USD"}}`
|
// Usage: `{{price 500 currency="USD"}}`
|
||||||
// Usage: `{{price currency="USD"}}`
|
// Usage: `{{price currency="USD"}}`
|
||||||
//
|
//
|
||||||
// Returns amount equal to the dominant denomintation of the currency.
|
// Returns amount equal to the dominant denomination of the currency.
|
||||||
// For example, if 2100 is passed, it will return 21.
|
// For example, if 2100 is passed, it will return 21.
|
||||||
const errors = require('@tryghost/errors');
|
|
||||||
const tpl = require('@tryghost/tpl');
|
const tpl = require('@tryghost/tpl');
|
||||||
|
const logging = require('@tryghost/logging');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
const messages = {
|
const messages = {
|
||||||
|
@ -82,22 +82,19 @@ module.exports = function price(planOrAmount, options) {
|
||||||
|
|
||||||
// CASE: if no amount is passed, e.g. `{{price}}` we throw an error
|
// CASE: if no amount is passed, e.g. `{{price}}` we throw an error
|
||||||
if (arguments.length < 2) {
|
if (arguments.length < 2) {
|
||||||
throw new errors.IncorrectUsageError({
|
logging.warn(tpl(messages.attrIsRequired));
|
||||||
message: tpl(messages.attrIsRequired)
|
return '';
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CASE: if amount is passed, but it is undefined we throw an error
|
// CASE: if amount is passed, but it is undefined we throw an error
|
||||||
if (amount === undefined) {
|
if (amount === undefined) {
|
||||||
throw new errors.IncorrectUsageError({
|
logging.warn(tpl(messages.attrIsRequired));
|
||||||
message: tpl(messages.attrIsRequired)
|
return '';
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_.isNumber(amount)) {
|
if (!_.isNumber(amount)) {
|
||||||
throw new errors.IncorrectUsageError({
|
logging.warn(tpl(messages.attrMustBeNumeric));
|
||||||
message: tpl(messages.attrMustBeNumeric)
|
return '';
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return amount / 100;
|
return amount / 100;
|
||||||
|
|
|
@ -1,9 +1,21 @@
|
||||||
const should = require('should');
|
const sinon = require('sinon');
|
||||||
const price = require('../../../../core/frontend/helpers/price');
|
const price = require('../../../../core/frontend/helpers/price');
|
||||||
|
|
||||||
const {registerHelper, shouldCompileToError, shouldCompileToExpected} = require('./utils/handlebars');
|
const {registerHelper, shouldCompileToExpected} = require('./utils/handlebars');
|
||||||
|
|
||||||
|
const logging = require('@tryghost/logging');
|
||||||
|
|
||||||
describe('{{price}} helper', function () {
|
describe('{{price}} helper', function () {
|
||||||
|
let logWarnStub;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
logWarnStub = sinon.stub(logging, 'warn');
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
sinon.restore();
|
||||||
|
});
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
registerHelper('price');
|
registerHelper('price');
|
||||||
});
|
});
|
||||||
|
@ -11,24 +23,22 @@ describe('{{price}} helper', function () {
|
||||||
it('throws an error for no provided parameters', function () {
|
it('throws an error for no provided parameters', function () {
|
||||||
const templateString = '{{price}}';
|
const templateString = '{{price}}';
|
||||||
|
|
||||||
shouldCompileToError(templateString, {}, {
|
shouldCompileToExpected(templateString, {}, '');
|
||||||
name: 'IncorrectUsageError'
|
logWarnStub.calledOnce.should.be.true();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws an error for undefined parameter', function () {
|
it('throws an error for undefined parameter', function () {
|
||||||
const templateString = '{{price @dont.exist}}';
|
const templateString = '{{price @dont.exist}}';
|
||||||
|
|
||||||
shouldCompileToError(templateString, {}, {
|
shouldCompileToExpected(templateString, {}, '');
|
||||||
name: 'IncorrectUsageError'
|
logWarnStub.calledOnce.should.be.true();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws if argument is not a number', function () {
|
it('throws if argument is not a number', function () {
|
||||||
const templateString = '{{price "not_a_number"}}';
|
const templateString = '{{price "not_a_number"}}';
|
||||||
shouldCompileToError(templateString, {}, {
|
|
||||||
name: 'IncorrectUsageError'
|
shouldCompileToExpected(templateString, {}, '');
|
||||||
});
|
logWarnStub.calledOnce.should.be.true();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('will format decimal adjusted amount', function () {
|
it('will format decimal adjusted amount', function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue