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

[BUGFIX] fix detect usage for native class polymorphism #6767

Merged
merged 2 commits into from
Nov 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ module(
function(hooks) {
setupTest(hooks);

let Message;

hooks.beforeEach(function() {
const User = Model.extend({
name: attr('string'),
messages: hasMany('message', { async: true, polymorphic: true }),
});

const Message = Mixin.create({
Message = Mixin.create({
title: attr('string'),
user: belongsTo('user', { async: true }),
});
Expand Down Expand Up @@ -131,6 +133,52 @@ module(
});
});

test('NATIVE CLASSES: Pushing to the hasMany reflects the change on the belongsTo side - async', function(assert) {
class Video extends Model.extend(Message) {}

this.owner.register('model:video', Video);

let store = this.owner.lookup('service:store');

var user, video;
run(function() {
store.push({
data: [
{
type: 'user',
id: '1',
attributes: {
name: 'Stanley',
},
relationships: {
messages: {
data: [],
},
},
},
{
type: 'video',
id: '2',
attributes: {
video: 'Here comes Youtube',
},
},
],
});
user = store.peekRecord('user', 1);
video = store.peekRecord('video', 2);
});

run(function() {
user.get('messages').then(function(fetchedMessages) {
fetchedMessages.pushObject(video);
video.get('user').then(function(fetchedUser) {
assert.equal(fetchedUser, user, 'user got set correctly');
});
});
});
});

/*
Local edits
*/
Expand Down
8 changes: 5 additions & 3 deletions packages/store/addon/-debug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ let assertPolymorphicType;
if (DEBUG) {
let checkPolymorphic = function checkPolymorphic(modelClass, addedModelClass) {
if (modelClass.__isMixin) {
//TODO Need to do this in order to support mixins, should convert to public api
//once it exists in Ember
return modelClass.__mixin.detect(addedModelClass.PrototypeMixin);
return (
modelClass.__mixin.detect(addedModelClass.PrototypeMixin) ||
// handle native class extension e.g. `class Post extends Model.extend(Commentable) {}`
modelClass.__mixin.detect(Object.getPrototypeOf(addedModelClass).PrototypeMixin)
);
}

return addedModelClass.prototype instanceof modelClass || modelClass.detect(addedModelClass);
Expand Down