Skip to content

Commit

Permalink
models/version: Add published_by relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Feb 7, 2021
1 parent 1be7108 commit dfbf0dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class Version extends Model {

@belongsTo('crate', { async: false }) crate;

@belongsTo('user', { async: false }) published_by;
@hasMany('users', { async: true }) authors;
@hasMany('dependency', { async: true }) dependencies;
@hasMany('version-download', { async: true }) version_downloads;
Expand Down
9 changes: 9 additions & 0 deletions app/serializers/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';

import ApplicationSerializer from './application';

export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
attrs: {
published_by: { embedded: 'always' },
},
});
28 changes: 28 additions & 0 deletions tests/models/version-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';

import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

module('Model | Version', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

hooks.beforeEach(function () {
this.store = this.owner.lookup('service:store');
});

test('`published_by` relationship is assigned correctly', async function (assert) {
let user = this.server.create('user', { name: 'JD' });

let crate = this.server.create('crate');
this.server.create('version', { crate, publishedBy: user });

let crateRecord = await this.store.findRecord('crate', crate.id);
assert.ok(crateRecord);
let versions = (await crateRecord.versions).toArray();
assert.equal(versions.length, 1);
let version = versions[0];
assert.ok(version.published_by);
assert.equal(version.published_by.name, 'JD');
});
});

0 comments on commit dfbf0dc

Please sign in to comment.