Chore/cleanup filter attribute (#1500)
* refactor: 💡 Rename attributes called filter * refactor: 💡 Remove adapter handling of old attribute errors * refactor: 💡 Check for undefined in serializer instead of falsy
This commit is contained in:
1 parent
b1f14e5c62
commit
f8429b15e9
11 files changed
+160
-53
No files matched your search
@@ -263,36 +263,17 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
|
||||
source: { pointer: '/data' },
|
||||
};
|
||||
// Normalize field-specific errors, if any.
|
||||
const errors = fieldErrors.flatMap((error) => {
|
||||
const newValue = [
|
||||
{
|
||||
detail: error.description,
|
||||
// Ember Data cannot handle errors on nested attributes, so these are
|
||||
// flattened (e.g. `attributes.foobar` becomes `attributes_foobar`).
|
||||
// To capture these errors, add read-only fields on the model
|
||||
// corresponding to the underscored field name.
|
||||
source: {
|
||||
pointer: `/data/attributes/${error.name.replace('.', '_')}`,
|
||||
},
|
||||
},
|
||||
];
|
||||
if (error.name.includes('attributes.')) {
|
||||
newValue.push({
|
||||
detail: error.description,
|
||||
// Going forward, nested attributes will be hoisted. Thus "attributes."
|
||||
// must be stripped from the error name key. Until all models are
|
||||
// transitioned away from model fragments, however, both this and the
|
||||
// prior error key mappings are necessary.
|
||||
source: {
|
||||
pointer: `/data/attributes/${error.name.replace(
|
||||
'attributes.',
|
||||
''
|
||||
)}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
return newValue;
|
||||
});
|
||||
const errors = fieldErrors.map((error) => ({
|
||||
detail: error.description,
|
||||
// Going forward, nested attributes will be hoisted. Thus "attributes."
|
||||
// must be stripped from the error name key. Until all models are
|
||||
// transitioned away from model fragments, however, both this and the
|
||||
// prior error key mappings are necessary.
|
||||
source: {
|
||||
pointer: `/data/attributes/${error.name.replace('attributes.', '')}`,
|
||||
},
|
||||
}));
|
||||
|
||||
// Return a list of JSON API errors rooted under the `errors` key.
|
||||
return {
|
||||
errors: [baseError, ...errors],
|
||||
|
||||
@@ -85,10 +85,10 @@ export default class GeneratedHostSetModel extends BaseModel {
|
||||
})
|
||||
filters;
|
||||
|
||||
// Azure specific
|
||||
// Azure specific, comes in from API as "filter". This is to avoid collisions with filter
|
||||
@attr('string', {
|
||||
description: '',
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
filter;
|
||||
filter_string;
|
||||
}
|
||||
@@ -50,7 +50,7 @@ export default class GeneratedManagedGroupModel extends BaseModel {
|
||||
@attr('string', {
|
||||
isNestedAttribute: true,
|
||||
description:
|
||||
'The boolean expression filter to use to determine membership.',
|
||||
'The boolean expression filter to use to determine membership. Comes in from API as "filter".',
|
||||
})
|
||||
filter;
|
||||
filter_string;
|
||||
}
|
||||
@@ -5,8 +5,6 @@ import { A } from '@ember/array';
|
||||
export default class ManagedGroupModel extends GeneratedManagedGroupModel {
|
||||
// =attributes
|
||||
|
||||
@attr('string', { readOnly: true }) attributes_filter;
|
||||
|
||||
/**
|
||||
* Members is read-only under normal circumstances. But members can
|
||||
* be persisted via calls to `addMembers()` or `removeMembers()`.
|
||||
|
||||
@@ -3,7 +3,7 @@ import { copy } from 'ember-copy';
|
||||
|
||||
const fieldByType = {
|
||||
aws: ['preferred_endpoints', 'filters', 'sync_interval_seconds'],
|
||||
azure: ['preferred_endpoints', 'filter', 'sync_interval_seconds'],
|
||||
azure: ['preferred_endpoints', 'filter_string', 'sync_interval_seconds'],
|
||||
};
|
||||
export default class HostSetSerializer extends ApplicationSerializer {
|
||||
// =properties
|
||||
@@ -69,6 +69,15 @@ export default class HostSetSerializer extends ApplicationSerializer {
|
||||
delete json.attributes[key];
|
||||
}
|
||||
}
|
||||
|
||||
if (key === 'filter_string') {
|
||||
const { filter_string } = json.attributes;
|
||||
if (filter_string) {
|
||||
json.attributes.filter = json.attributes.filter_string;
|
||||
}
|
||||
delete json.attributes.filter_string;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -80,10 +89,18 @@ export default class HostSetSerializer extends ApplicationSerializer {
|
||||
*/
|
||||
normalize(typeClass, hash, ...rest) {
|
||||
const normalizedHash = copy(hash, true);
|
||||
|
||||
if (typeof normalizedHash?.attributes?.filters === 'string') {
|
||||
normalizedHash.attributes.filters = [normalizedHash.attributes.filters];
|
||||
}
|
||||
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
|
||||
return normalized;
|
||||
|
||||
// Change name from filter to filter_string
|
||||
if (normalizedHash?.attributes?.filter) {
|
||||
normalizedHash.attributes.filter_string =
|
||||
normalizedHash.attributes.filter;
|
||||
delete normalizedHash.attributes.filter;
|
||||
}
|
||||
|
||||
return super.normalize(typeClass, normalizedHash, ...rest);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import ApplicationSerializer from './application';
|
||||
import { copy } from 'ember-copy';
|
||||
|
||||
export default class ManagedGroupSerializer extends ApplicationSerializer {
|
||||
// =properties
|
||||
@@ -7,4 +8,27 @@ export default class ManagedGroupSerializer extends ApplicationSerializer {
|
||||
* @type {boolean}
|
||||
*/
|
||||
serializeScopeID = false;
|
||||
|
||||
serialize() {
|
||||
const serialized = super.serialize(...arguments);
|
||||
|
||||
if (serialized.attributes.filter_string !== undefined) {
|
||||
serialized.attributes.filter = serialized.attributes.filter_string;
|
||||
}
|
||||
delete serialized.attributes.filter_string;
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
normalize(typeClass, hash, ...rest) {
|
||||
const normalizedHash = copy(hash, true);
|
||||
|
||||
if (normalizedHash?.attributes?.filter) {
|
||||
normalizedHash.attributes.filter_string =
|
||||
normalizedHash.attributes.filter;
|
||||
delete normalizedHash.attributes.filter;
|
||||
}
|
||||
|
||||
return super.normalize(typeClass, normalizedHash, ...rest);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user