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

Commit 7484aded by Daniel Durante

Cleaned up the scopes code a bit, added more specs involving non existing scopes…

… as well as a {silent} options as the last argument.
1 parent f7a926ab
Showing with 77 additions and 13 deletions
......@@ -193,7 +193,15 @@ module.exports = (function() {
, type
, options
, merge
, i
, scope
, scopeName
, argLength = arguments.length
, lastArg = arguments[argLength-1]
// Set defaults
scopeOptions = (typeof lastArg === "object" && !Array.isArray(lastArg) ? lastArg : {}) || {} // <-- for no arguments
scopeOptions.silent = (scopeOptions !== null && scopeOptions.hasOwnProperty('silent') ? scopeOptions.silent : true)
// Clear out any predefined scopes...
self.scopeObj = {}
......@@ -207,36 +215,53 @@ module.exports = (function() {
return self
}
for (var i = 0; i < argLength; i++) {
for (i = 0; i < argLength; i++) {
options = Array.isArray(arguments[i]) ? arguments[i] : [arguments[i]]
options.forEach(function(o){
type = typeof o
scope = null
merge = false
scopeName = null
if (type === "object") {
// Right now we only support a merge functionality for objects
if (!!o.merge) {
if (Array.isArray(o.merge)) {
merge = self.options.scopes[o.merge[0]].apply(self, o.merge.splice(1))
Utils.injectScope.call(self, merge, true)
merge = true
scopeName = o.merge[0]
if (Array.isArray(o.merge) && !!self.options.scopes[scopeName]) {
scope = self.options.scopes[scopeName].apply(self, o.merge.splice(1))
}
else if (typeof o.merge === "string") {
Utils.injectScope.call(self, self.options.scopes[o.merge], true)
scopeName = o.merge
scope = self.options.scopes[scopeName]
}
}
if (!!o.method) {
if (Array.isArray(o.method) && !!self.options.scopes[o.method[0]]) {
merge = self.options.scopes[o.method[0]].apply(self, o.method.splice(1))
Utils.injectScope.call(self, merge, (!!o.merge))
} else {
Utils.injectScope.call(self, self.options.scopes[o.method].apply(self))
scopeName = o.method[0]
scope = self.options.scopes[scopeName].apply(self, o.method.splice(1))
merge = !!o.merge
}
else if (!!self.options.scopes[o.method]) {
scopeName = o.method
scope = self.options.scopes[scopeName].apply(self)
}
} else {
Utils.injectScope.call(self, self.options.scopes[o])
scopeName = o
scope = self.options.scopes[scopeName]
}
} else {
scopeName = o
scope = self.options.scopes[scopeName]
}
if (!!scope) {
Utils.injectScope.call(self, scope, merge)
}
else if (!!self.options.scopes[o]) {
Utils.injectScope.call(self, self.options.scopes[o])
else if (scopeOptions.silent !== true && !!scopeName) {
throw new Error("Invalid scope " + scopeName + " called.")
}
})
}
......
......@@ -2096,7 +2096,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
it("should be able to use a defaultScope if declared", function(done) {
this.ScopeMe.findAll().success(function(users) {
this.ScopeMe.all().success(function(users) {
expect(users).toBeArray()
expect(users.length).toEqual(2)
expect([10,5].indexOf(users[0].access_level) !== -1).toBeTrue()
......@@ -2235,6 +2235,45 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
})
it("should gracefully omit any scopes that don't exist", function(done) {
this.ScopeMe.scope('sequelizeTeam', 'orderScope', 'doesntexist').all().success(function(team) {
expect(team).toBeArray()
expect(team.length).toEqual(2)
expect(team[0].username).toEqual('dan')
expect(team[1].username).toEqual('tony')
done()
})
})
it("should gracefully omit any scopes that don't exist through an array", function(done) {
this.ScopeMe.scope(['sequelizeTeam', 'orderScope', 'doesntexist']).all().success(function(team) {
expect(team).toBeArray()
expect(team.length).toEqual(2)
expect(team[0].username).toEqual('dan')
expect(team[1].username).toEqual('tony')
done()
})
})
it("should gracefully omit any scopes that don't exist through an object", function(done) {
this.ScopeMe.scope('sequelizeTeam', 'orderScope', {method: 'doesntexist'}).all().success(function(team) {
expect(team).toBeArray()
expect(team.length).toEqual(2)
expect(team[0].username).toEqual('dan')
expect(team[1].username).toEqual('tony')
done()
})
})
it("should emit an error for scopes that don't exist with silent: false", function(done) {
try {
this.ScopeMe.scope('doesntexist', {silent: false})
} catch (err) {
expect(err.message).toEqual('Invalid scope doesntexist called.')
done()
}
})
})
describe('schematic support', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!