不要怂,就是干,撸起袖子干!

Commit 0c88023e by Jan Aagaard Meier

bug(dependency) Use lodash.runInContext when using.template. This prevents other…

…s messing with template settings. Closes #2281
1 parent 00ec6d3b
...@@ -109,9 +109,9 @@ var BelongsToMany = function(source, target, options) { ...@@ -109,9 +109,9 @@ var BelongsToMany = function(source, target, options) {
} }
this.foreignKeyAttribute = {}; this.foreignKeyAttribute = {};
this.foreignKey = this.options.foreignKey || _.camelizeIf( this.foreignKey = this.options.foreignKey || Utils.camelizeIf(
[ [
_.underscoredIf(this.source.options.name.singular, this.source.options.underscored), Utils.underscoredIf(this.source.options.name.singular, this.source.options.underscored),
this.source.primaryKeyAttribute this.source.primaryKeyAttribute
].join('_'), ].join('_'),
!this.source.options.underscored !this.source.options.underscored
...@@ -127,9 +127,9 @@ var BelongsToMany = function(source, target, options) { ...@@ -127,9 +127,9 @@ var BelongsToMany = function(source, target, options) {
} }
this.otherKeyAttribute = {}; this.otherKeyAttribute = {};
this.otherKey = this.options.otherKey || _.camelizeIf( this.otherKey = this.options.otherKey || Utils.camelizeIf(
[ [
_.underscoredIf( Utils.underscoredIf(
this.isSelfAssociation ? this.isSelfAssociation ?
Utils.singularize(this.as) : Utils.singularize(this.as) :
this.target.options.name.singular, this.target.options.name.singular,
......
...@@ -42,12 +42,12 @@ var BelongsTo = function(source, target, options) { ...@@ -42,12 +42,12 @@ var BelongsTo = function(source, target, options) {
this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName; this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName;
} else if (this.options.foreignKey) { } else if (this.options.foreignKey) {
this.foreignKey = this.options.foreignKey; this.foreignKey = this.options.foreignKey;
} }
if (!this.foreignKey) { if (!this.foreignKey) {
this.foreignKey = _.camelizeIf( this.foreignKey = Utils.camelizeIf(
[ [
_.underscoredIf(this.as, this.source.options.underscored), Utils.underscoredIf(this.as, this.source.options.underscored),
this.target.primaryKeyAttribute this.target.primaryKeyAttribute
].join('_'), ].join('_'),
!this.source.options.underscored !this.source.options.underscored
......
...@@ -70,9 +70,9 @@ var HasMany = function(source, target, options) { ...@@ -70,9 +70,9 @@ var HasMany = function(source, target, options) {
} }
if (!this.foreignKey) { if (!this.foreignKey) {
this.foreignKey = _.camelizeIf( this.foreignKey = Utils.camelizeIf(
[ [
_.underscoredIf(this.source.options.name.singular, this.source.options.underscored), Utils.underscoredIf(this.source.options.name.singular, this.source.options.underscored),
this.source.primaryKeyAttribute this.source.primaryKeyAttribute
].join('_'), ].join('_'),
!this.source.options.underscored !this.source.options.underscored
......
...@@ -43,9 +43,9 @@ var HasOne = function(srcModel, targetModel, options) { ...@@ -43,9 +43,9 @@ var HasOne = function(srcModel, targetModel, options) {
} }
if (!this.foreignKey) { if (!this.foreignKey) {
this.foreignKey = _.camelizeIf( this.foreignKey = Utils.camelizeIf(
[ [
_.underscoredIf(Utils.singularize(this.source.name), this.target.options.underscored), Utils.underscoredIf(Utils.singularize(this.source.name), this.target.options.underscored),
this.source.primaryKeyAttribute this.source.primaryKeyAttribute
].join('_'), ].join('_'),
!this.source.options.underscored !this.source.options.underscored
......
...@@ -52,7 +52,7 @@ var Model = function(name, attributes, options) { ...@@ -52,7 +52,7 @@ var Model = function(name, attributes, options) {
this.underscored = this.underscored || this.underscoredAll; this.underscored = this.underscored || this.underscoredAll;
if (!this.options.tableName) { if (!this.options.tableName) {
this.tableName = this.options.freezeTableName ? name : Utils._.underscoredIf(Utils.pluralize(name), this.options.underscoredAll); this.tableName = this.options.freezeTableName ? name : Utils.underscoredIf(Utils.pluralize(name), this.options.underscoredAll);
} else { } else {
this.tableName = this.options.tableName; this.tableName = this.options.tableName;
} }
...@@ -637,13 +637,13 @@ Model.prototype.init = function(modelManager) { ...@@ -637,13 +637,13 @@ Model.prototype.init = function(modelManager) {
this._timestampAttributes = {}; this._timestampAttributes = {};
if (this.options.timestamps) { if (this.options.timestamps) {
if (this.options.createdAt !== false) { if (this.options.createdAt !== false) {
this._timestampAttributes.createdAt = this.options.createdAt || Utils._.underscoredIf('createdAt', this.options.underscored); this._timestampAttributes.createdAt = this.options.createdAt || Utils.underscoredIf('createdAt', this.options.underscored);
} }
if (this.options.updatedAt !== false) { if (this.options.updatedAt !== false) {
this._timestampAttributes.updatedAt = this.options.updatedAt || Utils._.underscoredIf('updatedAt', this.options.underscored); this._timestampAttributes.updatedAt = this.options.updatedAt || Utils.underscoredIf('updatedAt', this.options.underscored);
} }
if (this.options.paranoid && this.options.deletedAt !== false) { if (this.options.paranoid && this.options.deletedAt !== false) {
this._timestampAttributes.deletedAt = this.options.deletedAt || Utils._.underscoredIf('deletedAt', this.options.underscored); this._timestampAttributes.deletedAt = this.options.deletedAt || Utils.underscoredIf('deletedAt', this.options.underscored);
} }
} }
......
...@@ -23,7 +23,7 @@ var CounterCache = function(association, options) { ...@@ -23,7 +23,7 @@ var CounterCache = function(association, options) {
this.columnName = this.as; this.columnName = this.as;
} else { } else {
this.as = 'count_' + this.target.options.name.plural; this.as = 'count_' + this.target.options.name.plural;
this.columnName = Utils._.camelizeIf( this.columnName = Utils.camelizeIf(
this.as, this.as,
!this.source.options.underscored !this.source.options.underscored
); );
......
...@@ -2,62 +2,33 @@ ...@@ -2,62 +2,33 @@
var DataTypes = require('./data-types') var DataTypes = require('./data-types')
, SqlString = require('./sql-string') , SqlString = require('./sql-string')
, lodash = require('lodash') , _ = require('lodash').runInContext() // Prevent anyone messing with template settings by creating a fresh copy
, parameterValidator = require('./utils/parameter-validator') , parameterValidator = require('./utils/parameter-validator')
, inflection = require('inflection') , inflection = require('inflection')
, dottie = require('dottie')
, uuid = require('node-uuid') , uuid = require('node-uuid')
, deprecate = require('depd')('Utils'); , deprecate = require('depd')('Utils');
var Utils = module.exports = { var Utils = module.exports = {
inflection: inflection, inflection: inflection,
_: (function() { _: _,
var _ = lodash; camelizeIf: function(string, condition) {
var result = string;
_.mixin({ if (condition) {
camelizeIf: function(string, condition) { result = Utils.camelize(string);
var result = string; }
if (condition) {
result = Utils.camelize(string);
}
return result;
},
underscoredIf: function(string, condition) {
var result = string;
if (condition) { return result;
result = inflection.underscore(string); },
} underscoredIf: function(string, condition) {
var result = string;
return result; if (condition) {
}, result = inflection.underscore(string);
/* }
* Returns an array with some falsy values removed. The values null, "", undefined and NaN are considered falsey.
*/
compactLite: function(array) {
var index = -1,
length = array ? array.length : 0,
result = [];
while (++index < length) {
var value = array[index];
if (typeof value === 'boolean' || value === 0 || value) {
result.push(value);
}
}
return result;
},
matchesDots: function (dots, value) {
return function (item) {
return dottie.get(item, dots) === value;
};
}
});
return _; return result;
})(), },
// Same concept as _.merge, but don't overwrite properties that have already been assigned // Same concept as _.merge, but don't overwrite properties that have already been assigned
mergeDefaults: function (a, b) { mergeDefaults: function (a, b) {
return this._.merge(a, b, function (objectValue, sourceValue) { return this._.merge(a, b, function (objectValue, sourceValue) {
...@@ -89,9 +60,9 @@ var Utils = module.exports = { ...@@ -89,9 +60,9 @@ var Utils = module.exports = {
return SqlString.formatNamedParameters(sql, parameters, timeZone, dialect); return SqlString.formatNamedParameters(sql, parameters, timeZone, dialect);
}, },
cloneDeep: function(obj, fn) { cloneDeep: function(obj, fn) {
return lodash.cloneDeep(obj, function (elem) { return _.cloneDeep(obj, function (elem) {
// Do not try to customize cloning of plain objects and strings // Do not try to customize cloning of plain objects and strings
if (Array.isArray(elem) || lodash.isPlainObject(elem)) { if (Array.isArray(elem) || _.isPlainObject(elem)) {
return undefined; return undefined;
} }
// Preserve special data-types like `fn` across clones. _.get() is used for checking up the prototype chain // Preserve special data-types like `fn` across clones. _.get() is used for checking up the prototype chain
...@@ -141,7 +112,7 @@ var Utils = module.exports = { ...@@ -141,7 +112,7 @@ var Utils = module.exports = {
delete attributes[attribute]; delete attributes[attribute];
} }
if (lodash.isPlainObject(attributes[attribute])) { if (_.isPlainObject(attributes[attribute])) {
attributes[attribute] = Utils.mapOptionFieldNames({ attributes[attribute] = Utils.mapOptionFieldNames({
where: attributes[attribute] where: attributes[attribute]
}, Model).where; }, Model).where;
...@@ -256,7 +227,7 @@ var Utils = module.exports = { ...@@ -256,7 +227,7 @@ var Utils = module.exports = {
if (value instanceof DataTypes.UUIDV1 || value instanceof DataTypes.UUIDV4) { return false; } if (value instanceof DataTypes.UUIDV1 || value instanceof DataTypes.UUIDV4) { return false; }
if (lodash.isFunction(value)) { if (_.isFunction(value)) {
return false; return false;
} }
...@@ -404,7 +375,7 @@ var Utils = module.exports = { ...@@ -404,7 +375,7 @@ var Utils = module.exports = {
validateParameter: parameterValidator, validateParameter: parameterValidator,
formatReferences: function (obj) { formatReferences: function (obj) {
if (!lodash.isPlainObject(obj.references)) { if (!_.isPlainObject(obj.references)) {
deprecate('Non-object references property found. Support for that will be removed in version 4. Expected { references: { model: "value", key: "key" } } instead of { references: "value", referencesKey: "key" }.'); deprecate('Non-object references property found. Support for that will be removed in version 4. Expected { references: { model: "value", key: "key" } } instead of { references: "value", referencesKey: "key" }.');
obj.references = { model: obj.references, key: obj.referencesKey, deferrable: obj.referencesDeferrable }; obj.references = { model: obj.references, key: obj.referencesKey, deferrable: obj.referencesDeferrable };
obj.referencesKey = undefined; obj.referencesKey = undefined;
......
...@@ -70,29 +70,29 @@ describe(Support.getTestDialectTeaser('Utils'), function() { ...@@ -70,29 +70,29 @@ describe(Support.getTestDialectTeaser('Utils'), function() {
describe('underscore', function() { describe('underscore', function() {
describe('underscoredIf', function() { describe('underscoredIf', function() {
it('is defined', function() { it('is defined', function() {
expect(Utils._.underscoredIf).to.be.ok; expect(Utils.underscoredIf).to.be.ok;
}); });
it('underscores if second param is true', function() { it('underscores if second param is true', function() {
expect(Utils._.underscoredIf('fooBar', true)).to.equal('foo_bar'); expect(Utils.underscoredIf('fooBar', true)).to.equal('foo_bar');
}); });
it('doesn\'t underscore if second param is false', function() { it('doesn\'t underscore if second param is false', function() {
expect(Utils._.underscoredIf('fooBar', false)).to.equal('fooBar'); expect(Utils.underscoredIf('fooBar', false)).to.equal('fooBar');
}); });
}); });
describe('camelizeIf', function() { describe('camelizeIf', function() {
it('is defined', function() { it('is defined', function() {
expect(Utils._.camelizeIf).to.be.ok; expect(Utils.camelizeIf).to.be.ok;
}); });
it('camelizes if second param is true', function() { it('camelizes if second param is true', function() {
expect(Utils._.camelizeIf('foo_bar', true)).to.equal('fooBar'); expect(Utils.camelizeIf('foo_bar', true)).to.equal('fooBar');
}); });
it('doesn\'t camelize if second param is false', function() { it('doesn\'t camelize if second param is false', function() {
expect(Utils._.underscoredIf('fooBar', true)).to.equal('foo_bar'); expect(Utils.underscoredIf('fooBar', true)).to.equal('foo_bar');
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!