Skip to content

Commit c04baec

Browse files
runspiredigorT
authored andcommitted
[BUGFIX] fix detect usage for native class polymorphism (#6767)
* polymorphic relations and native classes mixins bug test * [BUGFIX] fix detect usage for native class polymorphism
1 parent 6027999 commit c04baec

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

packages/-ember-data/tests/integration/relationships/polymorphic-mixins-has-many-test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ module(
1414
function(hooks) {
1515
setupTest(hooks);
1616

17+
let Message;
18+
1719
hooks.beforeEach(function() {
1820
const User = Model.extend({
1921
name: attr('string'),
2022
messages: hasMany('message', { async: true, polymorphic: true }),
2123
});
2224

23-
const Message = Mixin.create({
25+
Message = Mixin.create({
2426
title: attr('string'),
2527
user: belongsTo('user', { async: true }),
2628
});
@@ -131,6 +133,52 @@ module(
131133
});
132134
});
133135

136+
test('NATIVE CLASSES: Pushing to the hasMany reflects the change on the belongsTo side - async', function(assert) {
137+
class Video extends Model.extend(Message) {}
138+
139+
this.owner.register('model:video', Video);
140+
141+
let store = this.owner.lookup('service:store');
142+
143+
var user, video;
144+
run(function() {
145+
store.push({
146+
data: [
147+
{
148+
type: 'user',
149+
id: '1',
150+
attributes: {
151+
name: 'Stanley',
152+
},
153+
relationships: {
154+
messages: {
155+
data: [],
156+
},
157+
},
158+
},
159+
{
160+
type: 'video',
161+
id: '2',
162+
attributes: {
163+
video: 'Here comes Youtube',
164+
},
165+
},
166+
],
167+
});
168+
user = store.peekRecord('user', 1);
169+
video = store.peekRecord('video', 2);
170+
});
171+
172+
run(function() {
173+
user.get('messages').then(function(fetchedMessages) {
174+
fetchedMessages.pushObject(video);
175+
video.get('user').then(function(fetchedUser) {
176+
assert.equal(fetchedUser, user, 'user got set correctly');
177+
});
178+
});
179+
});
180+
});
181+
134182
/*
135183
Local edits
136184
*/

packages/store/addon/-debug/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ let assertPolymorphicType;
2929
if (DEBUG) {
3030
let checkPolymorphic = function checkPolymorphic(modelClass, addedModelClass) {
3131
if (modelClass.__isMixin) {
32-
//TODO Need to do this in order to support mixins, should convert to public api
33-
//once it exists in Ember
34-
return modelClass.__mixin.detect(addedModelClass.PrototypeMixin);
32+
return (
33+
modelClass.__mixin.detect(addedModelClass.PrototypeMixin) ||
34+
// handle native class extension e.g. `class Post extends Model.extend(Commentable) {}`
35+
modelClass.__mixin.detect(Object.getPrototypeOf(addedModelClass).PrototypeMixin)
36+
);
3537
}
3638

3739
return addedModelClass.prototype instanceof modelClass || modelClass.detect(addedModelClass);

0 commit comments

Comments
 (0)