refactor: 💡 model grants as a fragment-array for statefulness
This commit is contained in:
8 files changed
+98
-10
No files matched your search
@@ -33,4 +33,10 @@ export default class GeneratedRoleModel extends BaseModel {
|
||||
description: 'Whether the resource is disabled',
|
||||
})
|
||||
disabled;
|
||||
|
||||
@attr('number', {
|
||||
description: 'Current version number of this resource.',
|
||||
})
|
||||
version;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import Fragment from 'ember-data-model-fragments/fragment';
|
||||
import { attr } from '@ember-data/model';
|
||||
|
||||
/**
|
||||
* Represents an item in an array of strings.
|
||||
*/
|
||||
export default class FragmentStringModel extends Fragment {
|
||||
|
||||
// =attributes
|
||||
|
||||
@attr('string', {
|
||||
description: 'The value of the string.'
|
||||
})
|
||||
value;
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import GeneratedRoleModel from '../generated/models/role';
|
||||
import { array } from 'ember-data-model-fragments/attributes';
|
||||
import { fragmentArray } from 'ember-data-model-fragments/attributes';
|
||||
|
||||
export default class RoleModel extends GeneratedRoleModel {
|
||||
|
||||
@@ -9,7 +9,7 @@ export default class RoleModel extends GeneratedRoleModel {
|
||||
* Grants is read-only _most_ under normal circumstances. But grants can
|
||||
* be persisted via a dedicated call to `saveGrants()`.
|
||||
*/
|
||||
@array({readOnly: true}) grants;
|
||||
@fragmentArray('fragment-string', {readOnly: true}) grants;
|
||||
|
||||
// =methods
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ export default class RoleSerializer extends ApplicationSerializer {
|
||||
|
||||
/**
|
||||
* If `adapterOptions.serializeGrants` is true, the serialization should
|
||||
* include **only grants** and nothing else. Normally, grants are not
|
||||
* include **only grants** and the version. Normally, grants are not
|
||||
* serialized.
|
||||
* @override
|
||||
* @param {Snapshot} snapshot
|
||||
@@ -27,8 +27,25 @@ export default class RoleSerializer extends ApplicationSerializer {
|
||||
*/
|
||||
serializeWithGrants(snapshot) {
|
||||
return {
|
||||
grants: snapshot.attr('grants')
|
||||
version: snapshot.attr('version'),
|
||||
grants: snapshot.attr('grants').map(grant => grant.attr('value'))
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts string arrays to object arrays for use with `fragment-string`.
|
||||
* E.g. `"foo"` -> `{value: "foo"}`
|
||||
* @override
|
||||
* @param {Model} typeClass
|
||||
* @param {Object} hash
|
||||
* @return {Object}
|
||||
*/
|
||||
normalize(modelClass, resourceHash) {
|
||||
// TODO: can fragment string normalization be handled generically?
|
||||
if (resourceHash.grants) {
|
||||
resourceHash.grants = resourceHash.grants.map(value => ({ value }));
|
||||
}
|
||||
return super.normalize(modelClass, resourceHash);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user