Skip to content

Commit

Permalink
Support findQuery
Browse files Browse the repository at this point in the history
In emberjs/data#2966 ember-data started passing the `query` from
`findQuery` to `buildURL` as the `id` parameter.
  • Loading branch information
Amiel Martin committed Apr 19, 2015
1 parent cad5701 commit 35baf0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
11 changes: 9 additions & 2 deletions addon/mixins/url-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var get = Ember.get;
var isArray = Ember.isArray;
var sanitize = encodeURIComponent;

var isObject = function(object) { return typeof object === 'object'; };

export default Ember.Mixin.create({
buildURL: function(type, id, snapshot, requestType) {
var template = this.getTemplate(requestType);
Expand Down Expand Up @@ -39,12 +41,17 @@ export default Ember.Mixin.create({
pathForType: function(type) { return this.pathForType(type); },

id: function(type, id) {
if (id && !isArray(id)) { return sanitize(id); }
if (id && !isArray(id) && !isObject(id)) { return sanitize(id); }
},

query: function(type, id) {
if (isObject(id)) { return id; }
},

unknownProperty: function(key) {
return function(type, id, snapshot) {
return get(snapshot, key);
if (id && isObject(id)) { return get(id, key); }
if (snapshot) { return get(snapshot, key); }
};
}
},
Expand Down
40 changes: 29 additions & 11 deletions tests/unit/mixins/url-templates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,46 @@ var NestedAdapter = Ember.Object.extend(UrlTemplatesMixin, {
});

var GenericAdapter = Ember.Object.extend(UrlTemplatesMixin, {
urlTemplate: '{+host}{/namespace}/{pathForType}{/id}'
urlTemplate: '{+host}{/namespace}/{pathForType}{/id}{?query*}'
});


test('BasicAdapter - it uses the template', function(assert) {
test('it uses the template', function(assert) {
var subject = BasicAdapter.create();
var url = subject.buildURL();
assert.equal(url, '/posts');
});

test('BasicAdapter - it fills in the id', function(assert) {
test('it fills in the id', function(assert) {
var subject = BasicAdapter.create();
var url = subject.buildURL('post', 3);
assert.equal(url, '/posts/3');
});

test('BasicAdapter - it escapes basic values', function(assert) {
test('it escapes basic values', function(assert) {
var subject = BasicAdapter.create({ urlTemplate: '/{foo}' });
var url = subject.buildURL('post', null, { foo: 'bar baz' });
assert.equal(url, '/bar%20baz');
});

test('NestedAdapter - it uses snapshot values', function(assert) {
test('it can use values from the snapshot', function(assert) {
var subject = NestedAdapter.create();
var url = subject.buildURL('comment', 5, { postId: 3 });
assert.equal(url, '/posts/3/comments/5');
});

test('GenericAdapter - it pluralizes the type', function(assert) {
test('it pluralizes the type', function(assert) {
var subject = GenericAdapter.create();
var url = subject.buildURL('post');
assert.equal(url, '/posts');
});

test('GenericAdapter - it includes the namespace from the adapter', function(assert) {
test('it includes the namespace from the adapter', function(assert) {
var subject = GenericAdapter.create({ namespace: 'api' });
var url = subject.buildURL('post');
assert.equal(url, '/api/posts');
});

test('GenericAdapter - includes the unescaped host from the adapter', function(assert) {
test('it includes the unescaped host from the adapter', function(assert) {
var subject = GenericAdapter.create({
host: 'http://example.com',
namespace: 'api'
Expand All @@ -63,14 +62,33 @@ test('GenericAdapter - includes the unescaped host from the adapter', function(a
assert.equal(url, 'http://example.com/api/posts');
});

test('BasicAdapter - it can use real query params', function(assert) {
test('it can include values from findQuery as {?query*}', function(assert) {
var subject = GenericAdapter.create();
var url = subject.buildURL('post', { category: 'Uncategorized', date: '2015-11-11' });
assert.equal(url, '/posts?category=Uncategorized&date=2015-11-11');
});

test('it can use real query params', function(assert) {
var subject = BasicAdapter.create({ urlTemplate: '/posts{?date,category,tag}' });
var url = subject.buildURL('post', null, { date: '2015-10-10', tag: 'tagged' });
assert.equal(url, '/posts?date=2015-10-10&tag=tagged');
});

test('BasicAdapter - it can use a custom template for the request type', function(assert) {
test('it can use real query params from findQuery', function(assert) {
var subject = BasicAdapter.create({ urlTemplate: '/posts{?date,category,tag}' });
// findQuery passes query params as the `id` argument.
var url = subject.buildURL('post', { date: '2015-10-10', tag: 'tagged' });
assert.equal(url, '/posts?date=2015-10-10&tag=tagged');
});

test('it can use a custom template for the request type', function(assert) {
var subject = BasicAdapter.create({ createRecordUrlTemplate: '/users/{userId}/create_post' });
var url = subject.buildURL('post', null, { userId: 1 }, 'createRecord');
assert.equal(url, '/users/1/create_post');
});

test('it does not fail for missing values when there is no snapshot', function(assert) {
var subject = NestedAdapter.create();
var url = subject.buildURL('comment');
assert.equal(url, '/posts//comments');
});

0 comments on commit 35baf0f

Please sign in to comment.