Skip to content

Commit

Permalink
feat: explicit relationship polymorphism
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Aug 31, 2022
1 parent e6da96d commit 4dd2342
Show file tree
Hide file tree
Showing 35 changed files with 965 additions and 929 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module('Deprecations', function (hooks) {

const StaticModelMethods = [
{ name: 'typeForRelationship', count: 3 },
{ name: 'inverseFor', count: 6 },
{ name: '_findInverseFor', count: 4 },
{ name: 'inverseFor', count: 5 },
{ name: '_findInverseFor', count: 3 },
{ name: 'eachRelationship', count: 3 },
{ name: 'eachRelatedType', count: 3 },
{ name: 'determineRelationshipType', count: 1 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,33 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
}

class Handle extends Model {
@belongsTo('user', { async: true, inverse: 'handles' }) user;
@belongsTo('user', { async: true, inverse: 'handles', as: 'handle' }) user;
}

class GithubHandle extends Handle {
class GithubHandle extends Model {
@attr('string') username;
@belongsTo('user', { async: true, inverse: 'handles', as: 'handle' }) user;
}

class TwitterHandle extends Handle {
class TwitterHandle extends Model {
@attr('string') nickname;
@belongsTo('user', { async: true, inverse: 'handles', as: 'handle' }) user;
}

class Company extends Model {
@attr('string') name;
@hasMany('user', { async: true, inverse: 'company' }) employees;
@hasMany('user', { async: true, inverse: 'company', as: 'company' }) employees;
}

class DevelopmentShop extends Company {
class DevelopmentShop extends Model {
@attr('boolean') coffee;
@attr('string') name;
@hasMany('user', { async: true, inverse: 'company', as: 'company' }) employees;
}

class DesignStudio extends Company {
class DesignStudio extends Model {
@attr('number') hipsters;
@attr('string') name;
@hasMany('user', { async: true, inverse: 'company', as: 'company' }) employees;
}

this.owner.register('adapter:application', class extends JSONAPIAdapter {});
Expand Down Expand Up @@ -768,6 +773,14 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
});

test('create record', async function (assert) {
this.owner.register(
'serializer:application',
class extends JSONAPISerializer {
shouldSerializeHasMany() {
return true;
}
}
);
assert.expect(3);

ajaxResponse([
Expand Down Expand Up @@ -798,6 +811,15 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
},
},
});
let twitterHandle = store.push({
data: {
type: 'twitter-handle',
id: '2',
attributes: {
nickname: 'wycats',
},
},
});

let user = store.createRecord('user', {
firstName: 'Yehuda',
Expand All @@ -808,6 +830,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
let handles = await user.handles;

handles.push(githubHandle);
handles.push(twitterHandle);

await user.save();

Expand All @@ -827,6 +850,12 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
company: {
data: { type: 'companies', id: '1' },
},
handles: {
data: [
{ type: 'github-handles', id: '2' },
{ type: 'twitter-handles', id: '2' },
],
},
},
},
},
Expand All @@ -836,6 +865,15 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
test('update record', async function (assert) {
assert.expect(3);

this.owner.register(
'serializer:application',
class extends JSONAPISerializer {
shouldSerializeHasMany() {
return true;
}
}
);

ajaxResponse([
{
data: {
Expand Down Expand Up @@ -901,6 +939,9 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
company: {
data: { type: 'companies', id: '2' },
},
handles: {
data: [{ type: 'github-handles', id: '3' }],
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { get } from '@ember/object';
import { module, test } from 'qunit';
import { defer, resolve } from 'rsvp';

import DS from 'ember-data';
import { setupTest } from 'ember-qunit';

import JSONAPIAdapter from '@ember-data/adapter/json-api';
import Model, { attr } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import { recordIdentifierFor } from '@ember-data/store';

module('integration/references/record', function (hooks) {
setupTest(hooks);

hooks.beforeEach(function () {
const Person = DS.Model.extend({
name: DS.attr(),
const Person = Model.extend({
name: attr(),
});

this.owner.register('model:person', Person);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,20 @@ module('integration/relationship/belongs_to Belongs-To Relationships', function
}

class Message extends Model {
@belongsTo('user', { inverse: 'messages', async: false }) user;
@belongsTo('user', { inverse: 'messages', async: false, as: 'message' }) user;
@attr('date') created_at;
}

class Comment extends Message {
class Comment extends Model {
@attr('string') body;
@attr('date') created_at;
@belongsTo('user', { inverse: 'messages', async: false, as: 'message' }) user;
@belongsTo('message', { polymorphic: true, async: false, inverse: null }) message;
}
class Post extends Message {
class Post extends Model {
@attr('string') title;
@attr('date') created_at;
@belongsTo('user', { inverse: 'messages', async: false, as: 'message' }) user;
@hasMany('comment', { async: false, inverse: null }) comments;
}

Expand All @@ -296,23 +300,23 @@ module('integration/relationship/belongs_to Belongs-To Relationships', function
@hasMany('chapter', { async: false, inverse: 'book' }) chapters;
}

const Book1 = Model.extend({
name: attr('string'),
});
class Book1 extends Model {
@attr name;
}

class Chapter extends Model {
@attr title;
@belongsTo('book', { async: false, inverse: 'chapters' }) book;
}

const Author = Model.extend({
name: attr('string'),
books: hasMany('book', { async: false, inverse: 'author' }),
});
class Author extends Model {
@attr name;
@hasMany('book', { async: false, inverse: 'author' }) books;
}

const Section = Model.extend({
name: attr('string'),
});
class Section extends Model {
@attr name;
}

this.owner.register('model:user', User);
this.owner.register('model:post', Post);
Expand Down Expand Up @@ -428,16 +432,12 @@ module('integration/relationship/belongs_to Belongs-To Relationships', function

test('The store can materialize a non loaded monomorphic belongsTo association', function (assert) {
assert.expect(1);
class Message extends Model {
@belongsTo('user', { inverse: 'messages', async: false }) user;
@attr('date') created_at;
}
class Post extends Message {

class Post extends Model {
@attr('string') title;
@hasMany('comment', { async: false, inverse: null }) comments;
@belongsTo('user', { async: true, inverse: 'messages' }) user;
@belongsTo('user', { async: true, inverse: 'messages', as: 'message' }) user;
}
this.owner.register('model:message', Message);
this.owner.register('model:post', Post);

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

0 comments on commit 4dd2342

Please sign in to comment.