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

No relationship assertion for includes #82

Merged
merged 2 commits into from
Jun 23, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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