Skip to content

Commit

Permalink
No relationship assertion for includes (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
samselikoff authored Jun 23, 2019
1 parent 10f8168 commit ea14381
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
20 changes: 18 additions & 2 deletions addon/mixins/loadable-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ export default Mixin.create({
});
},

/**
Returns
@method _hasNamedRelationship
@param {String} name The name of a relationship
@return {Boolean} True if the current model has a relationship defined for the provided name
@private
*/
_hasNamedRelationship(name) {
return Boolean(get(this.constructor, `relationshipsByName`).get(name));
},

/**
@method _getRelationshipInfo
@private
Expand Down Expand Up @@ -261,16 +273,20 @@ export default Mixin.create({
/**
Tracks a single include path as being loaded.
We verify that the current model actually has a named relationship defined
for the first segment of the include path, because requests for polymorphic
collections could return mixed sets of models that don't share all of
the relationships that were requested via includes.
@method _trackLoadedIncludePath
@param {String} path A single include path. Example: "comments.author"
@private
*/
_trackLoadedIncludePath(path) {
let [firstInclude, ...rest] = path.split(".");
let relationship = camelize(firstInclude);
let reference = this._getReference(relationship);

if (reference) {
if (this._hasNamedRelationship(relationship)) {
this._loadedReferences[relationship] = true;

if (rest.length) {
Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/mixins/itemizable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Mixin from '@ember/object/mixin';

export default Mixin.create({});
7 changes: 7 additions & 0 deletions tests/dummy/app/models/homepage-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import DS from 'ember-data';

export default DS.Model.extend({

itemizable: DS.belongsTo({ polymorphic: true }),

});
33 changes: 14 additions & 19 deletions tests/integration/mixins/loadable-store/load-records-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,14 @@ import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { waitUntil } from '@ember/test-helpers';
import MirageServer from 'dummy/tests/integration/helpers/mirage-server';
import { Model, hasMany, belongsTo } from 'ember-cli-mirage';
import LoadableStore from 'ember-data-storefront/mixins/loadable-store';

module('Integration | Mixins | LoadableStore | loadRecords', function(hooks) {
setupTest(hooks);

hooks.beforeEach(function() {
this.server = new MirageServer({
models: {
post: Model.extend({
comments: hasMany(),
author: belongsTo(),
tags: hasMany()
}),
comment: Model.extend({
post: belongsTo(),
author: belongsTo()
}),
tag: Model.extend({
posts: hasMany()
}),
author: Model.extend({
comments: hasMany(),
posts: hasMany()
})
},
discoverEmberDataModels: true,
baseConfig() {
this.resource('posts');
}
Expand Down Expand Up @@ -146,6 +128,19 @@ module('Integration | Mixins | LoadableStore | loadRecords', function(hooks) {
assert.equal(posts.get('firstObject.comments.length'), 2);
});

test('it can load a polymorphic collection with model-specific includes', async function(assert) {
this.server.get('/homepage-items');
let post = this.server.create('post');
let comment = this.server.create('comment');
this.server.create('homepage-item', { itemizable: post });
this.server.create('homepage-item', { itemizable: comment });

await this.store.loadRecords('homepage-item', { include: 'itemizable.tags' });

assert.ok(this.store.peekRecord('post', post.id));
assert.ok(this.store.peekRecord('comment', comment.id));
});

module('Tracking includes', function() {
test('it will track an include', async function(assert) {
let serverPost = this.server.create('post', { title: 'My post' });
Expand Down

0 comments on commit ea14381

Please sign in to comment.