Skip to content

Commit

Permalink
Don't mutate attributes with format, mentioned in knex#315
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Jun 12, 2014
1 parent fad7add commit 9258bbf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ _.extend(BookshelfModel.prototype, {
load: Promise.method(function(relations, options) {
return Promise.bind(this)
.then(function() {
return [this.format(this.attributes)];
return [this.format(_.extend(Object.create(null), this.attributes))];
})
.then(function(response) {
return this._handleEager(response, _.extend({}, options, {
Expand Down
2 changes: 1 addition & 1 deletion lib/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ _.extend(BookshelfRelation.prototype, {
if (!this.isInverse()) {
groupedKey = model.id;
} else {
var formatted = model.format(model.attributes);
var formatted = model.format(_.extend(Object.create(null), model.attributes));
groupedKey = this.isThrough() ? formatted[this.key('throughForeignKey')] : formatted[this.key('foreignKey')];
}
var relation = model.relations[relationName] = this.relatedInstance(grouped[groupedKey]);
Expand Down
15 changes: 14 additions & 1 deletion test/integration/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ module.exports = function(bookshelf) {
// TODO: better way to test this.
it('calls format when saving', function() {

var M = Backbone.Model.extend({
var M = bookshelf.Model.extend({
tableName: 'test',
format: function(attrs) {
return _.reduce(attrs, function(memo, val, key) {
Expand All @@ -243,6 +243,19 @@ module.exports = function(bookshelf) {

});

it('does not mutate attributes on format', function() {

var M = bookshelf.Model.extend({
tableName: 'sites',
format: function(attrs) {
assert.ok(attrs !== this.attributes);
return attrs;
}
});

return M.forge({id: 1}).fetch().call('load');
});

});

describe('fetch', function() {
Expand Down

0 comments on commit 9258bbf

Please sign in to comment.