Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add epilogue.initialize([options.resource]) #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
16 changes: 16 additions & 0 deletions tests/epilogue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it makes a little more sense to embed this in a section called defaults? like:

epilogue.initialize({
  app: {},
  sequelize: new Sequelize('main', null, null, {
    dialect: 'sqlite',
    storage: ':memory:',
    logging: (process.env.SEQ_LOG ? console.log : false)
  }),
  defaults: {
    pagination: false
  }

we already know semantically that we will be dealing with resources, so resource sounds redundant to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I don't know, but I don't feel right giving non-semantic definitions on things we don't know if they will change. I've understood why you prefer so, but with defaults key I would see something more like: defaults: { resource: { /* ... */ } } (In fact I was going to implement that way but I've changed my mind because sequelize is using { define: { /* defaults for define */ } }).

My motto is always to avoid future BC by having good implementations. Even if this is only creating resources, it doesn't mean we could introduce another kind of bindings later.

But it is your decision, tell me what you think about, I will write it. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think { defaults: { resource ... is a good compromise. The existing keys are going to be global to the module (app, sequelize). Arguably, so would have been resource, as after all epilogue is chiefly interested in creating resources - however, I get your point and I think this would be a good middle ground.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, you are meaning it would stay as it is or do I have to change anything here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would become:

epilogue.initialize({
  app: {},
  sequelize: new Sequelize('main', null, null, {
    dialect: 'sqlite',
    storage: ':memory:',
    logging: (process.env.SEQ_LOG ? console.log : false)
  }),
  defaults: {
    resource: {
      pagination: false
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the fact that epilogue only creates resources, I am making up in my mind features to enhance productivity that could change that fact, but I would let that discussion to later as soon as I have an architecture well formed.

});

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',
Expand Down
11 changes: 10 additions & 1 deletion tests/resource/resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbroadst I had to edit this test because except was calling resource method without rest context. It doesn't change at all the purpose of this test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha, nice catch

done();
});

Expand All @@ -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() {
Expand Down