feat: 🎸 support isNestedAttribute in models

This commit is contained in:
Randall Morey committed 2022-01-18 13:44:27 -05:00
1 parent ec8a55be31
commit ac14e8786d
6 files changed
+79 -19

No files matched your search

@@ -1,9 +0,0 @@
import Fragment from 'ember-data-model-fragments/fragment';
import { attr } from '@ember-data/model';
export default class FragmentTargetAttributesModel extends Fragment {
@attr('number', {
description: 'The default port a target should use if present.',
})
default_port;
}
+7 -6
View File
@@ -1,15 +1,16 @@
import GeneratedTargetModel from '../generated/models/target';
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
import { attr } from '@ember-data/model';
import { fragmentArray } from 'ember-data-model-fragments/attributes';
import { computed } from '@ember/object';
export default class TargetModel extends GeneratedTargetModel {
// =attributes
/**
* Attributes of this resource, if any, represented as a JSON fragment.
* @type {FragmentTargetAttributesModel}
*/
@fragment('fragment-target-attributes', { defaultValue: {} }) attributes;
@attr('number', {
isNestedAttribute: true,
description: 'The default port a target should use if present.',
})
default_port;
/**
* @type {[FragmentHostSourceModel]}
+35 -1
View File
@@ -50,6 +50,12 @@ export default class ApplicationSerializer extends RESTSerializer {
// Do not serialize `disabled` fields.
// TODO: disabled is temporarily disabled
if (key === 'disabled') delete json[key];
// Push nested attributes down into the attributes key
if (options.isNestedAttribute && json[key]) {
if (!json.attributes) json.attributes = {};
json.attributes[key] = json[key];
delete json[key];
}
return value;
}
@@ -195,9 +201,37 @@ export default class ApplicationSerializer extends RESTSerializer {
* @return {Object}
*/
normalize(typeClass, hash) {
const normalizedHash = copy(hash, true);
let normalizedHash = copy(hash, true);
const scopeID = get(normalizedHash, 'scope.id');
if (scopeID) normalizedHash.scope.scope_id = scopeID;
normalizedHash = this.normalizeNestedAttributes(typeClass, normalizedHash);
return super.normalize(typeClass, normalizedHash);
}
/**
* Certain typed resources in the API have type-specific fields nested under
* an `attributes` key in the payload
* (e.g. `attributes: { fieldName: '123' }`). Since Ember Data lacks
* first-class support for nested attributes, these fields must be hoisted up
* into the main body of the payload.
*
* In order to annotate a field as being a nested attribute, add the key name
* to the `nestedAttributes` field on the serializer:
* `@attr('string', { isNestedAttribute: true }) fieldName;`
*
* @param {Model} typeClass
* @param {Object} hash
* @return {Object}
*/
normalizeNestedAttributes(typeClass, hash) {
const normalizedHash = copy(hash, true);
typeClass.attributes.forEach((attribute) => {
const { isNestedAttribute } = attribute.options;
const attributeValue = normalizedHash.attributes?.[attribute.name];
if (isNestedAttribute && attributeValue) {
normalizedHash[attribute.name] = attributeValue;
}
});
return normalizedHash;
}
}
@@ -1 +0,0 @@
export { default } from 'api/models/fragment-target-attributes';
@@ -20,6 +20,7 @@ module('Unit | Serializer | target', function (hooks) {
scope_id: 'org_1',
type: 'org',
},
default_port: 1234,
version: 1,
type: 'tcp',
});
@@ -35,7 +36,7 @@ module('Unit | Serializer | target', function (hooks) {
session_max_seconds: 28800,
session_connection_limit: 1,
attributes: {
default_port: null,
default_port: 1234,
},
});
});
@@ -123,6 +124,40 @@ module('Unit | Serializer | target', function (hooks) {
});
});
test('it normalizes records with nested attributes', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const targetModelClass = store.createRecord('target').constructor;
const payload = {
id: '1',
name: 'Target 1',
attributes: {
default_port: 1234,
},
};
const normalized = serializer.normalizeSingleResponse(
store,
targetModelClass,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'target',
attributes: {
application_credential_source_ids: [],
authorized_actions: [],
host_sources: [],
name: 'Target 1',
default_port: 1234,
},
relationships: {},
},
});
});
test('it normalizes missing host_sources and application_credential_source_ids to empty array', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
@@ -94,7 +94,7 @@
<form.input
@name='default_port'
@type='number'
@value={{@model.attributes.default_port}}
@value={{@model.default_port}}
@label={{t 'form.default_port.label'}}
@error={{@model.errors.default_port}}
@helperText={{t 'form.default_port.help'}}