Refactor model

This commit is contained in:
Carlos Alcaide Corvo committed 2021-11-05 08:55:37 -07:00
1 parent 322828ccc4
commit 360bf0ffdd
5 files changed
+31 -135

No files matched your search

@@ -44,4 +44,10 @@ export default class GeneratedManagedGroupModel extends BaseModel {
'The ID of the Auth Method that is associated with this ManagedGroup',
})
auth_method_id;
@attr('array', {
description:
'The IDs of the current set of members (accounts) that are associated with this ManagedGroup.',
})
member_ids
}
+1 -87
View File
@@ -1,89 +1,3 @@
import GeneratedManagedGroupModel from '../generated/models/managed-groups';
import { attr } from '@ember-data/model';
import { computed } from '@ember/object';
import { A } from '@ember/array';
export default class ManagedGroupsModel extends GeneratedManagedGroupModel {
// =attributes
/**
* Members is read-only under normal circumstances. But members can
* be persisted via calls to `addMembers()` or `removeMembers()`.
*/
@attr({
readOnly: true,
defaultValue: () => A(),
emptyArrayIfMissing: true,
})
member_ids;
/**
* An array of resolved user instances.
* Unresolvable instances are excluded from the array.
* @type {[UserModel]}
*/
// TODO: Use fragments array to read cross scope members.
@computed('member_ids.[]', 'store')
get members() {
return this.member_ids
.map((id) => this.store.peekRecord('user', id))
.filter(Boolean);
}
// =methods
/**
* Adds members via the `add-members` method.
* See serializer and adapter for more information.
* @param {[string]} memberIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
addMembers(memberIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'add-members',
memberIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Delete members via the `remove-members` method.
* See serializer and adapter for more information.
* @param {[string]} memberIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
removeMembers(memberIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'remove-members',
memberIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Delete a single member via the `remove-members` method.
* @param {number} memberID
* @param {object} options
* @return {Promise}
*/
removeMember(memberID, options) {
return this.removeMembers([memberID], options);
}
}
export default class ManagedGroupsModel extends GeneratedManagedGroupModel {}
+1 -30
View File
@@ -1,32 +1,3 @@
import ApplicationSerializer from './application';
export default class ManagedGroupsSerializer extends ApplicationSerializer {
// =methods
/**
* If `adapterOptions.memberIDs` is set (to an array of user IDs),
* then the payload is serialized via `serializewithMembers`.
* @override
* @param {Snapshot} snapshot
* @return {object}
*/
serialize(snapshot) {
let serialized = super.serialize(...arguments);
const memberIDs = snapshot?.adapterOptions?.memberIDs;
if (memberIDs) serialized = this.serializewithMembers(snapshot, memberIDs);
return serialized;
}
/**
* Returns a payload containing only the `member_ids` array (and version).
* @param {Snapshot} snapshot
* @param {[string]} memberIDs
* @return {object}
*/
serializewithMembers(snapshot, memberIDs) {
return {
version: snapshot.attr('version'),
member_ids: memberIDs,
};
}
}
export default class ManagedGroupsSerializer extends ApplicationSerializer {}
@@ -4,7 +4,6 @@ import { setupTest } from 'ember-qunit';
module('Unit | Model | managed groups', function (hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function (assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('managed-groups', {});
@@ -4,10 +4,8 @@ import { setupTest } from 'ember-qunit';
module('Unit | Serializer | Managed groups', function (hooks) {
setupTest(hooks);
test('it serializes managed groups normally, without members', function (assert) {
test('it serializes correctly on create', function (assert) {
assert.expect(1);
// TODO: move store declaration at module level. Before doing so, to make sure it works, generate one out, one in and deep compare.
// TODO: if moving store out works, move serializer too.
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-groups');
const record = store.createRecord('managed-groups', {
@@ -24,33 +22,41 @@ module('Unit | Serializer | Managed groups', function (hooks) {
assert.deepEqual(serializedRecord, {
name: 'Group',
description: 'Description',
member_ids: ['1', '2'],
version: 1,
auth_method_id: '1234',
type: '80',
});
});
test('it seralizes only members associated with this group and version when a `memberIDs` array is provided', function (assert) {
test('it serializes correctly on update', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-groups');
const record = store.createRecord('managed-groups', {
name: 'Group',
description: 'Description',
store.push({
data: {
id: '1',
type: 'managed-groups',
attributes: {
name: 'Group Test',
description: 'Description for the test',
member_ids: ['1', '2'],
version: 1,
auth_method_id: '1234',
type: '80',
},
},
});
const record = store.peekRecord('managed-groups', '1');
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'Group Test',
description: 'Description for the test',
member_ids: ['1', '2'],
version: 1,
auth_method_id: '1234',
type: '80',
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
memberIDs: ['4', '5'],
};
const serializedRecord = serializer.serialize(snapshot);
console.log(JSON.stringify(serializedRecord));
assert.deepEqual(serializedRecord, {
member_ids: ['4', '5'],
version: 1,
});
});
});