bookshelf-scopes
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "bookshelf-scopes", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Giving you Rails like scopes in Bookshelf.js.", | ||
"main": "src/scopes.js", | ||
"dependencies": { | ||
"lodash": "^3.6.0" | ||
"lodash": "^3.9.3" | ||
}, | ||
"devDependencies": { | ||
"bluebird": "^2.9.15", | ||
"bookshelf": "^0.7.9", | ||
"chai": "^2.1.2", | ||
"knex": "^0.7.6", | ||
"mocha": "^2.2.1", | ||
"sqlite3": "^3.0.5" | ||
"bluebird": "^2.9.27", | ||
"bookshelf": "^0.8.1", | ||
"chai": "^2.3.0", | ||
"knex": "^0.8.6", | ||
"mocha": "^2.2.5", | ||
"sqlite3": "^3.0.8" | ||
}, | ||
@@ -17,0 +17,0 @@ "scripts": { |
@@ -11,7 +11,10 @@ 'use strict'; | ||
bookshelf.Model.extend = function(protoProps) { | ||
var self = this; | ||
self.scopes = _.extend({}, self.scopes || {}, protoProps.scopes || {}); | ||
var model = baseExtend.apply(this, arguments); | ||
Object.keys(self.scopes).forEach(function(property) { | ||
self.prototype[property] = function() { | ||
model.prototype.scopes = model.prototype.scopes || {}; | ||
_.defaults(model.prototype.scopes, this.prototype.scopes || {}); | ||
Object.keys(model.prototype.scopes).forEach(function(property) { | ||
model.prototype[property] = function() { | ||
var _this = this; | ||
@@ -21,9 +24,9 @@ var passedInArguments = _.toArray(arguments); | ||
if (passedInArguments.length > 0 && passedInArguments[0] instanceof QueryBuilder) { | ||
self.scopes[property].apply(this, passedInArguments); | ||
this.scopes[property].apply(this, passedInArguments); | ||
return self; | ||
return this; | ||
} else { | ||
return this.query(function(qb) { | ||
passedInArguments.unshift(qb); | ||
self.scopes[property].apply(_this, passedInArguments); | ||
_this.scopes[property].apply(_this, passedInArguments); | ||
}); | ||
@@ -33,9 +36,26 @@ } | ||
self[property] = function() { | ||
var model = this.forge(); | ||
return model[property].apply(model, arguments); | ||
model[property] = function() { | ||
var instance = model.forge(); | ||
return instance[property].apply(instance, arguments); | ||
}; | ||
}); | ||
return baseExtend.apply(self, arguments); | ||
_.each(['hasMany', 'hasOne', 'belongsToMany', 'morphOne', 'morphMany', | ||
'belongsTo', 'through'], function(method) { | ||
var original = model.prototype[method]; | ||
model.prototype[method] = function() { | ||
var relationship = original.apply(this, arguments); | ||
if (relationship.model.prototype.scopes && relationship.model.prototype.scopes.default) { | ||
var originalSelectConstraints = relationship.relatedData.selectConstraints; | ||
relationship.relatedData.selectConstraints = function(knex, options) { | ||
originalSelectConstraints.apply(this, arguments); | ||
relationship.model.prototype.scopes.default.apply(this, [knex]); | ||
}; | ||
} | ||
return relationship; | ||
}; | ||
}); | ||
return model; | ||
}; | ||
@@ -42,0 +62,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7823
6
71
1
Updatedlodash@^3.9.3