From c496e0a025320765b31bbaedff63855c8f2b2dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Moreira?= Date: Fri, 19 Feb 2016 19:07:12 +0000 Subject: [PATCH 1/2] Add epilogue.initialize([options.resource]) --- lib/index.js | 9 ++++++++- tests/epilogue.test.js | 16 ++++++++++++++++ tests/resource/resource.test.js | 9 +++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index bd82b95..0d68d08 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,11 +34,18 @@ var epilogue = { this.updateMethod = method; } + + this.defaultOptions = { + resource: {} + }; + if (options.resource) { + this.defaultOptions.resource = options.resource; + } }, resource: function(options) { options = options || {}; - _.defaults(options, { + _.defaults(options, this.defaultOptions.resource, { include: [], associations: false }); diff --git a/tests/epilogue.test.js b/tests/epilogue.test.js index 6c69cea..3d2c2a0 100644 --- a/tests/epilogue.test.js +++ b/tests/epilogue.test.js @@ -34,6 +34,22 @@ describe('Epilogue', function() { done(); }); + it('should extend .resource options if they are provided on initialize', function() { + var resourceOptions = { pagination: false }; + + epilogue.initialize({ + app: {}, + sequelize: new Sequelize('main', null, null, { + dialect: 'sqlite', + storage: ':memory:', + logging: (process.env.SEQ_LOG ? console.log : false) + }), + resource: resourceOptions + }); + + expect(epilogue.defaultOptions.resource).to.be.equal(resourceOptions); + }); + it('should allow the user to pass in a sequelize instance rather than prototype', function() { var db = new Sequelize('main', null, null, { dialect: 'sqlite', diff --git a/tests/resource/resource.test.js b/tests/resource/resource.test.js index c27b980..3cba012 100644 --- a/tests/resource/resource.test.js +++ b/tests/resource/resource.test.js @@ -89,7 +89,16 @@ describe('Resource(basic)', function() { expect(exception).to.eql(new Error('resource needs a model')); done(); } + }); + + it('should extend default options', function() { + rest.defaultOptions.resource = { pagination: false }; + + var resource = rest.resource({ + model: test.models.Person + }); + expect( resource.pagination ).to.be.equal( false ); }); it('should auto generate endpoints if none were provided', function() { From 41862dc20d29baa06909f6cc99684c4ee1cc84c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Moreira?= Date: Fri, 19 Feb 2016 19:24:50 +0000 Subject: [PATCH 2/2] Added context to `rest.resource` Your test wasn't being called with a context, so it was throwing an error trying to find `defaultOptions` property on `this`. --- tests/resource/resource.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/resource/resource.test.js b/tests/resource/resource.test.js index 3cba012..b5e48eb 100644 --- a/tests/resource/resource.test.js +++ b/tests/resource/resource.test.js @@ -78,7 +78,7 @@ describe('Resource(basic)', function() { // TESTS describe('construction', function() { it('should throw an exception if called with an invalid model', function(done) { - expect(rest.resource).to.throw('please specify a valid model'); + expect(rest.resource.bind(rest)).to.throw('please specify a valid model'); done(); });