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 Resource helper custom path parameter #1068

Merged
merged 6 commits into from Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions addon/server.js
Expand Up @@ -424,7 +424,8 @@ export default class Server {
}
}

resource(resourceName, { only, except } = {}) {
resource(resourceName, { only, except, path } = {}) {
path = path || `/${resourceName}`;
only = only || [];
except = except || [];

Expand All @@ -433,11 +434,11 @@ export default class Server {
}

let actionsMethodsAndsPathsMappings = {
index: { methods: ['get'], path: `/${resourceName}` },
show: { methods: ['get'], path: `/${resourceName}/:id` },
create: { methods: ['post'], path: `/${resourceName}` },
update: { methods: ['put', 'patch'], path: `/${resourceName}/:id` },
delete: { methods: ['del'], path: `/${resourceName}/:id` }
index: { methods: ['get'], path: `${path}` },
show: { methods: ['get'], path: `${path}/:id` },
create: { methods: ['post'], path: `${path}` },
update: { methods: ['put', 'patch'], path: `${path}/:id` },
delete: { methods: ['del'], path: `${path}/:id` }
};

let allActions = Object.keys(actionsMethodsAndsPathsMappings);
Expand All @@ -448,7 +449,11 @@ export default class Server {
actions.forEach((action) => {
let methodsWithPath = actionsMethodsAndsPathsMappings[action];

methodsWithPath.methods.forEach((method) => this[method](methodsWithPath.path));
methodsWithPath.methods.forEach((method) => {
return path === resourceName
? this[method](methodsWithPath.path)
: this[method](methodsWithPath.path, resourceName);
});
});
}

Expand Down
147 changes: 132 additions & 15 deletions tests/integration/server/resource-shorthand-test.js
Expand Up @@ -7,7 +7,8 @@ module('Integration | Server | Resource shorthand', {
this.server = new Server({
environment: 'test',
models: {
contact: Model
contact: Model,
blogPost: Model
},
serializers: {
application: ActiveModelSerializer
Expand All @@ -22,17 +23,22 @@ module('Integration | Server | Resource shorthand', {
});

test('resource generates get shorthand for index action', function(assert) {
assert.expect(2);
let done = assert.async();
assert.expect(3);
let done = assert.async(2);

this.server.db.loadData({
contacts: [
{ id: 1, name: 'Link' },
{ id: 2, name: 'Zelda' }
],
blogPosts: [
{ id: 1, title: 'Post 1' },
{ id: 2, title: 'Post 2' }
]
});

this.server.resource('contacts');
this.server.resource('blog-posts', { path: '/posts' });

$.ajax({
method: 'GET',
Expand All @@ -42,20 +48,36 @@ test('resource generates get shorthand for index action', function(assert) {
assert.deepEqual(res, { contacts: [{ id: '1', name: 'Link' }, { id: '2', name: 'Zelda' }] });
done();
});

$.ajax({
method: 'GET',
url: '/posts'
}).fail((xhr, textStatus, error) => {
assert.ok(false, 'failed to find custom path');
done();
}).done(function(res, status, xhr) {
assert.ok(true);
done();
});
});

test('resource generates get shorthand for show action', function(assert) {
assert.expect(2);
let done = assert.async();
assert.expect(3);
let done = assert.async(2);

this.server.db.loadData({
contacts: [
{ id: 1, name: 'Link' },
{ id: 2, name: 'Zelda' }
],
blogPosts: [
{ id: 1, title: 'Post 1' },
{ id: 2, title: 'Post 2' }
]
});

this.server.resource('contacts');
this.server.resource('blog-posts', { path: '/posts' });

$.ajax({
method: 'GET',
Expand All @@ -65,14 +87,26 @@ test('resource generates get shorthand for show action', function(assert) {
assert.deepEqual(res, { contact: { id: '2', name: 'Zelda' } });
done();
});

$.ajax({
method: 'GET',
url: '/posts/2'
}).fail((xhr, textStatus, error) => {
assert.ok(false, 'failed to find custom path');
done();
}).done(function(res, status, xhr) {
assert.ok(true);
done();
});
});

test('resource generates post shorthand', function(assert) {
let { server } = this;
assert.expect(2);
let done = assert.async();
assert.expect(3);
let done = assert.async(2);

server.resource('contacts');
server.resource('blog-posts', { path: '/posts' });

$.ajax({
method: 'POST',
Expand All @@ -87,20 +121,40 @@ test('resource generates post shorthand', function(assert) {
assert.equal(server.db.contacts.length, 1);
done();
});

$.ajax({
method: 'POST',
url: '/posts',
data: JSON.stringify({
blog_post: {
name: 'Post 1'
}
})
}).fail((xhr, textStatus, error) => {
assert.ok(false, 'failed to find custom path');
done();
}).done((res, status, xhr) => {
assert.ok(true);
done();
});
});

test('resource generates put shorthand', function(assert) {
let { server } = this;
assert.expect(2);
let done = assert.async();
assert.expect(3);
let done = assert.async(2);

this.server.db.loadData({
contacts: [
{ id: 1, name: 'Link' }
],
blogPosts: [
{ id: 1, title: 'Post 1' }
]
});

server.resource('contacts');
server.resource('blog-posts', { path: '/posts' });

$.ajax({
method: 'PUT',
Expand All @@ -115,20 +169,40 @@ test('resource generates put shorthand', function(assert) {
assert.equal(server.db.contacts[0].name, 'Zelda');
done();
});

$.ajax({
method: 'PUT',
url: '/posts/1',
data: JSON.stringify({
blog_post: {
name: 'Post 2'
}
})
}).fail((xhr, textStatus, error) => {
assert.ok(false, 'failed to find custom path');
done();
}).done((res, status, xhr) => {
assert.ok(true);
done();
});
});

test('resource generates patch shorthand', function(assert) {
let { server } = this;
assert.expect(2);
let done = assert.async();
assert.expect(3);
let done = assert.async(2);

this.server.db.loadData({
contacts: [
{ id: 1, name: 'Link' }
],
blogPosts: [
{ id: 1, title: 'Post 1' }
]
});

server.resource('contacts');
server.resource('blog-posts', { path: '/posts' });

$.ajax({
method: 'PATCH',
Expand All @@ -143,20 +217,40 @@ test('resource generates patch shorthand', function(assert) {
assert.equal(server.db.contacts[0].name, 'Zelda');
done();
});

$.ajax({
method: 'PATCH',
url: '/posts/1',
data: JSON.stringify({
blog_post: {
name: 'Post 2'
}
})
}).fail((xhr, textStatus, error) => {
assert.ok(false, 'failed to find custom path');
done();
}).done((res, status, xhr) => {
assert.ok(true);
done();
});
});

test('resource generates delete shorthand works', function(assert) {
let { server } = this;
assert.expect(2);
let done = assert.async();
assert.expect(3);
let done = assert.async(2);

this.server.db.loadData({
contacts: [
{ id: 1, name: 'Link' }
],
blogPosts: [
{ id: 1, title: 'Post 1' }
]
});

server.resource('contacts');
server.resource('blog-posts', { path: '/posts' });

$.ajax({
method: 'DELETE',
Expand All @@ -166,6 +260,17 @@ test('resource generates delete shorthand works', function(assert) {
assert.equal(server.db.contacts.length, 0);
done();
});

$.ajax({
method: 'DELETE',
url: '/posts/1'
}).fail((xhr, textStatus, error) => {
assert.ok(false, 'failed to find custom path');
done();
}).done((res, status, xhr) => {
assert.ok(true);
done();
});
});

test('resource does not accept both :all and :except options', function(assert) {
Expand All @@ -178,8 +283,8 @@ test('resource does not accept both :all and :except options', function(assert)

test('resource generates shorthands which are whitelisted by :only option', function(assert) {
let { server } = this;
assert.expect(1);
let done = assert.async();
assert.expect(2);
let done = assert.async(2);

server.db.loadData({
contacts: [
Expand All @@ -189,6 +294,7 @@ test('resource generates shorthands which are whitelisted by :only option', func
});

server.resource('contacts', { only: ['index'] });
server.resource('blog-posts', { path: '/posts', only: ['index'] });

$.ajax({
method: 'GET',
Expand All @@ -197,6 +303,17 @@ test('resource generates shorthands which are whitelisted by :only option', func
assert.equal(xhr.status, 200);
done();
});

$.ajax({
method: 'GET',
url: '/posts'
}).fail(function() {
assert.ok(false, 'failed to find custom path');
done();
}).done((res, status, xhr) => {
assert.equal(xhr.status, 200);
done();
});
});

test('resource does not generate shorthands which are not whitelisted with :only option', function(assert) {
Expand Down