Skip to content

models/version: Add published_by relationship #3252

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

Merged
merged 2 commits into from
Feb 8, 2021
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
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' },
},
});
1 change: 1 addition & 0 deletions mirage/models/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { belongsTo, Model } from 'ember-cli-mirage';

export default Model.extend({
crate: belongsTo(),
publishedBy: belongsTo('user'),
});
21 changes: 18 additions & 3 deletions mirage/serializers/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable ember/avoid-leaking-state-in-ember-objects */

import BaseSerializer from './application';

export default BaseSerializer.extend({
Expand All @@ -15,6 +17,8 @@ export default BaseSerializer.extend({
'crate_size',
],

include: ['publishedBy'],

links(version) {
return {
authors: `/api/v1/crates/${version.crateId}/${version.num}/authors`,
Expand All @@ -28,18 +32,29 @@ export default BaseSerializer.extend({

if (Array.isArray(hash)) {
for (let resource of hash) {
this._adjust(resource);
this._adjust(resource, addToIncludes);
}
} else {
this._adjust(hash);
this._adjust(hash, addToIncludes);
}

addToIncludes = addToIncludes.filter(it => it.modelName !== 'user');

return [hash, addToIncludes];
},

_adjust(hash) {
_adjust(hash, includes) {
hash.dl_path = `/api/v1/crates/${hash.crate_id}/${hash.num}/download`;
hash.crate = hash.crate_id;

if (hash.published_by_id) {
let user = includes.find(it => it.modelName === 'user' && it.id === hash.published_by_id);
hash.published_by = this.getHashForIncludedResource(user)[0].users[0];
} else {
hash.published_by = null;
}

delete hash.crate_id;
delete hash.published_by_id;
},
});
18 changes: 17 additions & 1 deletion tests/mirage/crates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/rand/1.0.0-beta.1/downloads',
},
num: '1.0.0-beta.1',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand Down Expand Up @@ -303,6 +304,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/rand/1.0.0/downloads',
},
num: '1.0.0',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand All @@ -320,6 +322,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/rand/1.1.0/downloads',
},
num: '1.1.0',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand All @@ -337,6 +340,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/rand/1.2.0/downloads',
},
num: '1.2.0',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand Down Expand Up @@ -540,9 +544,10 @@ module('Mirage | Crates', function (hooks) {
});

test('returns all versions belonging to the specified crate', async function (assert) {
let user = this.server.create('user');
this.server.create('crate', { name: 'rand' });
this.server.create('version', { crateId: 'rand', num: '1.0.0' });
this.server.create('version', { crateId: 'rand', num: '1.1.0' });
this.server.create('version', { crateId: 'rand', num: '1.1.0', publishedBy: user });
this.server.create('version', { crateId: 'rand', num: '1.2.0' });

let response = await fetch('/api/v1/crates/rand/versions');
Expand All @@ -565,6 +570,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/rand/1.0.0/downloads',
},
num: '1.0.0',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand All @@ -582,6 +588,13 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/rand/1.1.0/downloads',
},
num: '1.1.0',
published_by: {
id: 1,
avatar: 'https://avatars1.githubusercontent.com/u/14631425?v=4',
login: 'user-1',
name: 'User 1',
url: 'https://github.com/user-1',
},
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand All @@ -599,6 +612,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/rand/1.2.0/downloads',
},
num: '1.2.0',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand Down Expand Up @@ -995,6 +1009,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/bar/1.0.0/downloads',
},
num: '1.0.0',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand All @@ -1012,6 +1027,7 @@ module('Mirage | Crates', function (hooks) {
version_downloads: '/api/v1/crates/baz/1.0.1/downloads',
},
num: '1.0.1',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand Down
1 change: 1 addition & 0 deletions tests/mirage/me-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ module('Mirage | /me', function (hooks) {
version_downloads: '/api/v1/crates/foo/1.2.3/downloads',
},
num: '1.2.3',
published_by: null,
updated_at: '2017-02-24T12:34:56Z',
yanked: false,
},
Expand Down
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');
});
});