Skip to content

Commit

Permalink
[BUGFIX] fix detect usage for native class polymorphism (#6767)
Browse files Browse the repository at this point in the history
* polymorphic relations and native classes mixins bug test

* [BUGFIX] fix detect usage for native class polymorphism
  • Loading branch information
runspired authored and igorT committed Dec 18, 2019
1 parent 6c1ee12 commit 5ed556b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
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 @@ -29,9 +29,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

0 comments on commit 5ed556b

Please sign in to comment.