diff --git a/lib/helpers/collection_proxy.js b/lib/helpers/collection_proxy.js index 7a91b56..d7646aa 100644 --- a/lib/helpers/collection_proxy.js +++ b/lib/helpers/collection_proxy.js @@ -2,7 +2,7 @@ var PROXY = {} , _ = require('underscore'); function _apply(collection, fn, args) { - return collection[fn].apply(collection, args); + collection[fn].apply(collection, args); } Object.defineProperty(PROXY, 'namespacer', {value: require('./namespacer'), writable: true}); @@ -14,16 +14,19 @@ Object.defineProperty(PROXY, 'mapper', {value: require('./mapper'), writable: tr * @param {Object} model * @param {Object} options * @param {Array} args - * @param {Function} callback */ -PROXY.proxy = function (model, options, args, callback) { +PROXY.proxy = function (model, options, args) { var fn = options.method + , callback , arguments_length = arguments.length; model.getCollection(function (error, collection) { + var is_overwritten = typeof PROXY[fn] !== 'undefined' + , is_native = typeof collection[fn] !== 'undefined'; - // noop - if (arguments_length < 4) { + if (typeof args[args.length - 1] === 'function') { + callback = args[args.length - 1]; + } else { callback = function () {}; } @@ -42,15 +45,10 @@ PROXY.proxy = function (model, options, args, callback) { } // overwritten method with hooks, or custom method - if (typeof PROXY[fn] !== 'undefined' && (typeof collection[fn] !== 'undefined' ? options.hooks : true)) { - return PROXY[fn](model, collection, args, callback); - + if (is_overwritten && (is_native ? options.hooks : true)) { + PROXY[fn](model, collection, args, callback); // driver method or hooks disabled } else { - if (!_.isFunction(args[args.length - 1])) { - args.push(callback); - } - _apply(collection, fn, args); } }); diff --git a/lib/model.js b/lib/model.js index 36dae72..7c57f4c 100644 --- a/lib/model.js +++ b/lib/model.js @@ -44,7 +44,7 @@ module.exports = function (db, collection_name) { * @param {String|Object} options * @param {Mixed} args * @param {Function} callback - * @returns itself + * @return itself */ MODEL.mongo = function (args) { var parsed_method, options, callback; @@ -70,14 +70,7 @@ module.exports = function (db, collection_name) { , mapping: true }, options); - if (typeof args[args.length - 1] === 'function') { - callback = args[args.length - 1]; - } else { - callback = function () {}; // noop - args.push(callback); - } - - MODEL.collection_proxy.proxy(MODEL, options, args, callback); + MODEL.collection_proxy.proxy(MODEL, options, args); return MODEL; }; diff --git a/test/helpers_collection_proxy_test.js b/test/helpers_collection_proxy_test.js index e138b21..4b8104f 100644 --- a/test/helpers_collection_proxy_test.js +++ b/test/helpers_collection_proxy_test.js @@ -128,7 +128,7 @@ describe('collection proxy test', function () { ['find', 'update', 'insert'].forEach(function (method) { stubGetCollection(self); stub = self.stub(Collection, method, function (args, callback) { - assert.equal(typeof callback, 'function'); + assert.equal(typeof callback, 'undefined'); }); CollectionProxy.proxy(Model, {method: method, hooks: false}, args); diff --git a/test/model_test.js b/test/model_test.js index 31b9071..c600335 100644 --- a/test/model_test.js +++ b/test/model_test.js @@ -47,10 +47,10 @@ describe('Models', function () { , stub, self = this , query = {name: 'zemba'}; - stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) { + stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args) { assert.deepEqual(_options, {hooks: true, namespacing: true, mapping: true, method: 'findArray'}); assert.deepEqual(_args[0], query); - assert.equal(_callback, callback); + assert.deepEqual(_args[1], callback); }); User.mongo('findArray', query, callback); @@ -62,7 +62,7 @@ describe('Models', function () { , stub, self = this , query = {name: 'zemba'}; - stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) { + stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args) { assert.deepEqual(_options, { hooks: true , namespacing: true @@ -71,7 +71,7 @@ describe('Models', function () { , namespace: 'public' }); assert.deepEqual(_args[0], query); - assert.equal(_callback, callback); + assert.deepEqual(_args[1], callback); }); User.mongo('findArray:public', query, callback); @@ -83,7 +83,7 @@ describe('Models', function () { , stub, self = this , query = {name: 'zemba'}; - stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) { + stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args) { assert.deepEqual(_options, { hooks: false , namespacing: true @@ -92,33 +92,13 @@ describe('Models', function () { , namespace: 'public' }); assert.deepEqual(_args[0], query); - assert.equal(_callback, callback); + assert.deepEqual(_args[1], callback); }); User.mongo({method: 'findArray', namespace: 'public', hooks: false}, query, callback); sinon.assert.calledOnce(stub); })); - it('`mongo` can be called without a callback', sinon.test(function () { - var query = {name: 'zemba'} - , stub, self = this; - - stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) { - assert.deepEqual(_options, { - hooks: true - , namespacing: true - , mapping: true - , method: 'findArray' - }); - assert.deepEqual(_args[0], query); - assert.equal(typeof _args[_args.length - 1], 'function'); - assert.equal(typeof _callback, 'function'); - }); - - User.mongo('findArray', query); - sinon.assert.calledOnce(stub); - })); - it('`validate` validates a mongo document', sinon.test(function () { var document = {} , self = this, stub