Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
update to make links object comply with jsonapi
Browse files Browse the repository at this point in the history
  • Loading branch information
OllieNilsen committed Jan 11, 2016
1 parent ccecdb8 commit bb39fbf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ var _ = require('lodash');
module.exports = {
attributes: {
modelLinks: function (controller, reverseRouteService, actions) {
const result = [];
const result = {};
actions = actions || ['findOne', 'update', 'destroy'];
_.each(actions, (action) => {
let key = action === 'findOne' ? 'self' : action;
const components = {
controller: controller + '.' + action,
args: [this.id]
};
result.push({link: reverseRouteService(components, false), rel: action});
result[key] = reverseRouteService(components, true);
});
console.log('result: ', result);
return result;
}
},
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
"main": "./lib",
"dependencies": {
"lodash": "^3.10.1",
"sails-util": "^0.10.6"
"sails-util": "^0.11.0"
},
"optionalDependencies": {
"sails-reverse-routing": "^1.0.12"
},
"devDependencies": {
"chai": "1.x.x",
"chai": "3.4.1",
"chai-things": "^0.2.0",
"coveralls": "^2.11.4",
"istanbul": "^0.4.0",
"mocha": "^1.21.5",
"mocha": "^2.3.4",
"mocha-lcov-reporter": "^1.0.0",
"rewire": "^2.4.0",
"sinon": "^1.17.2",
Expand Down
21 changes: 10 additions & 11 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ describe('UNIT sails-linking-models', () => {
describe('modelLinks', () => {
let reverseRouteService, result;
before(() => {
reverseRouteService = sinon.stub().returns(['link1', 'link2']);

reverseRouteService = sinon.stub().returns('link1');
});

describe('without actionsArray passed in...', () => {
Expand All @@ -20,23 +21,21 @@ describe('UNIT sails-linking-models', () => {
});
});

it('should return an array of links of length 3', () => {
expect(result).to.have.length(3)
.and.to.contain.a.thing.with.keys('rel', 'link')
.and.to.contain.a.thing.with.property('rel', 'findOne')
.and.to.contain.a.thing.with.property('rel', 'update')
.and.to.contain.a.thing.with.property('rel', 'destroy');

it('should return aa links object with the expected properties', () => {
expect(result).to.have.property('self')
expect(result).to.have.property('update')
expect(result).to.have.property('destroy')
});
});
describe('with actionsArray passed in...', () => {
before(() => {
result = lib.attributes.modelLinks('controllerName', reverseRouteService, ['action1', 'action2']);
});
it("should use the actionsArray parameter", () => {
expect(result).to.have.length(2)
.and.to.contain.a.thing.with.keys('rel', 'link')
.and.to.contain.a.thing.with.property('rel', 'action1')
.and.to.contain.a.thing.with.property('rel', 'action2');
expect(result).to.have.property('action1')
expect(result).to.have.property('action2')

});
});
});
Expand Down

0 comments on commit bb39fbf

Please sign in to comment.