Skip to content

Commit

Permalink
Merge pull request #34 from amiel/use-snapshots
Browse files Browse the repository at this point in the history
Support DS.Snapshot
  • Loading branch information
amiel authored Jul 3, 2017
2 parents 71c0e93 + ca3fdbf commit 2e4e0c7
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ before_install:
- phantomjs --version

install:
- npm install
- yarn install

script:
# Usually, it's ok to finish the test scenario without reverting
Expand Down
20 changes: 19 additions & 1 deletion addon/mixins/url-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import UriTemplate from 'uri-templates';

const { isArray, copy, typeOf } = Ember;

const ID_KEY_RE = /(_id|Id)$/;

export default Ember.Mixin.create({
mergedProperties: ['urlSegments'],
buildURL(type, id, snapshot, requestType, query) {
Expand Down Expand Up @@ -73,7 +75,15 @@ export default Ember.Mixin.create({
unknownProperty(key) {
return (type, id, snapshot, query) => {
if (query && query[key]) { return query[key]; }
if (snapshot) { return snapshot[key]; }
if (snapshot) {
if (snapshot[key]) {
return snapshot[key];
} else if (isIdKey(key) && snapshot.belongsTo) {
return snapshot.belongsTo(relationshipNameForKey(key), { id: true });
} else if (snapshot.attr) {
return snapshot.attr(key);
}
}
};
}
}
Expand All @@ -82,3 +92,11 @@ export default Ember.Mixin.create({
function isObject(object) {
return typeof object === 'object';
}

function relationshipNameForKey(key) {
return key.replace(ID_KEY_RE, '');
}

function isIdKey(key) {
return ID_KEY_RE.test(key);
}
14 changes: 0 additions & 14 deletions tests/acceptance/basic-url-template-test-test.js

This file was deleted.

28 changes: 28 additions & 0 deletions tests/acceptance/basic-url-template-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | basic url template');

test('it can use a simple custom url', function(assert) {
server.createList('post', 5);

visit('/posts');

andThen(() => {
assert.equal(find('#posts .post').length, 5);
});
});

test('it can use a specific template for one type of call (queryRecord)', function(assert) {
server.create('post', {
slug: 'my-first-post',
title: 'This is my first post',
});

visit('/posts/my-first-post');

andThen(() => {
assert.equal(find('.post h2').text(), 'This is my first post');
});
});

23 changes: 23 additions & 0 deletions tests/acceptance/simple-relationships-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Ember from 'ember';
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

const { get } = Ember;

moduleForAcceptance('Acceptance | simple relationships', {
beforeEach() {
this.post = server.create('post');
this.comment = server.create('comment', {
post: this.post,
});

visit(`/posts/${get(this.post, 'slug')}`);
},
});

test('it can use a belongsTo id from the snapshot when generating a url', function(assert) {
andThen(() => {
assert.equal(find('#comments p').length, 1);
});
});

21 changes: 21 additions & 0 deletions tests/acceptance/using-attributes-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Ember from 'ember';
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

const { get } = Ember;

moduleForAcceptance('Acceptance | using attributes', {
beforeEach() {
this.post = server.create('post');
},
});

test('it can use an attribute from the snapshot when generating a url', function(assert) {
visit(`/posts/${get(this.post, 'slug')}`);
click('#publish-post');
visit(`/posts/${get(this.post, 'slug')}`);

andThen(() => {
assert.equal(find('#publish-post').length, 0);
});
});
6 changes: 6 additions & 0 deletions tests/dummy/app/adapters/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
namespace: 'api',
urlTemplate: "{/namespace}/posts/{postId}/comments{/id}",
});
4 changes: 3 additions & 1 deletion tests/dummy/app/adapters/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
urlTemplate: "{+host}{/namespace}/my-posts{/id}",
urlTemplate: "{+host}{/namespace}/my-posts",
queryRecordUrlTemplate: "{+host}{/namespace}/my-posts/{slug}",
updateRecordUrlTemplate: "{+host}{/namespace}/my-posts/{slug}",
namespace: 'api',
});
14 changes: 14 additions & 0 deletions tests/dummy/app/controllers/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Ember from 'ember';

const { computed } = Ember;

export default Ember.Controller.extend({
post: computed.readOnly('model'),

actions: {
publishPost(post) {
post.set('isPublished', true);
post.save();
},
},
});
4 changes: 4 additions & 0 deletions tests/dummy/app/models/comment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import DS from 'ember-data';

const { attr, belongsTo } = DS;

export default DS.Model.extend({
post: belongsTo(),
message: attr('string'),
});
5 changes: 4 additions & 1 deletion tests/dummy/app/models/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import DS from 'ember-data';

const { attr } = DS;
const { attr, hasMany } = DS;

export default DS.Model.extend({
slug: attr('string'),
title: attr('string'),
isPublished: attr('boolean'),
comments: hasMany(),
});
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Router = Ember.Router.extend({

Router.map(function() {
this.route('posts');
this.route('post', { path: '/posts/:slug' });
});

export default Router;
7 changes: 7 additions & 0 deletions tests/dummy/app/routes/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Ember from 'ember';

export default Ember.Route.extend({
model(params) {
return this.store.queryRecord('post', { slug: params.slug });
},
});
15 changes: 15 additions & 0 deletions tests/dummy/app/templates/post.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div id="post-{{post.id}}" class="post">
<h2>{{post.title}}</h2>

{{#unless post.isPublished}}
<a id="publish-post" {{action 'publishPost' post}}>Publish Post</a>
{{/unless}}

<h3>Comments</h3>

<div id="comments">
{{#each post.comments as |comment|}}
<p>{{comment.message}}</p>
{{/each}}
</div>
</div>
2 changes: 1 addition & 1 deletion tests/dummy/app/templates/posts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div id="posts">
{{#each model as |post|}}
<div id="post-{{post.id}}" class="post">
{{post.title}}
{{#link-to 'post' post.slug}}{{post.title}}{{/link-to}}
</div>
{{/each}}
</div>
12 changes: 12 additions & 0 deletions tests/dummy/mirage/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
export default function() {

this.get('/api/my-posts', 'post');
this.get('/api/my-posts/:slug', (schema, request) => {
return schema.posts.findBy({ slug: request.params.slug });
});

this.patch('/api/my-posts/:slug', (schema, request) => {
let post = schema.posts.findBy({ slug: request.params.slug });

// We could actually update the post, but YAGNI? *shrug*
return post;
});

this.get('/api/posts/:post_id/comments/:id');

// These comments are here to help you get started. Feel free to delete them.

Expand Down
7 changes: 7 additions & 0 deletions tests/dummy/mirage/factories/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Factory, faker } from 'ember-cli-mirage';

export default Factory.extend({
message() {
return faker.lorem.sentence();
},
});
3 changes: 3 additions & 0 deletions tests/dummy/mirage/factories/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Factory, faker } from 'ember-cli-mirage';

export default Factory.extend({
slug() {
return faker.helpers.slugify(this.title);
},
title(i) {
return `Post #${i}: ${faker.lorem.words()}`;
},
Expand Down
5 changes: 5 additions & 0 deletions tests/dummy/mirage/models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Model, belongsTo } from 'ember-cli-mirage';

export default Model.extend({
post: belongsTo(),
});
3 changes: 2 additions & 1 deletion tests/dummy/mirage/models/post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Model } from 'ember-cli-mirage';
import { Model, hasMany } from 'ember-cli-mirage';

export default Model.extend({
comments: hasMany(),
});

0 comments on commit 2e4e0c7

Please sign in to comment.