refactor: send scope_id query param on scope create, rather than serializing into the payload
This commit is contained in:
7 files changed
+52
-71
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import ApplicationAdapter from './application';
|
||||
import { serializeIntoHash } from '@ember-data/adapter/-private';
|
||||
|
||||
export default class ScopeAdapter extends ApplicationAdapter {
|
||||
|
||||
/**
|
||||
* Identical to default method except that we pass a query parameter
|
||||
* indicating the parent scope.
|
||||
* @override
|
||||
* @param {Store} store
|
||||
* @param {Model} type
|
||||
* @param {Snapshot} snapshot
|
||||
* @return {Promise} promise
|
||||
*/
|
||||
createRecord(store, type, snapshot) {
|
||||
const parentScopeID = snapshot.attr('scope').attr('scope_id');
|
||||
let url =
|
||||
this.buildURL(type.modelName, null, snapshot, 'createRecord');
|
||||
// TODO this should be generalized into `buildURL`, but currently Ember Data
|
||||
// doesn't provide a way to manage query parameters. They are passed on
|
||||
// to the underlying ajax/fetch service.
|
||||
url = `${url}?scope_id=${parentScopeID}`;
|
||||
const data = serializeIntoHash(store, type, snapshot);
|
||||
return this.ajax(url, 'POST', { data });
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import ApplicationSerializer from './application';
|
||||
|
||||
export default class ScopeSerializer extends ApplicationSerializer {
|
||||
|
||||
// =attributes
|
||||
|
||||
attrs = {
|
||||
scope: { serialize: true }
|
||||
};
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* While most resources do not serialize scope into a request body (scope
|
||||
* is expressed in the URL), scopes themselves _do serialize scope_ as a
|
||||
* string field `scope_id`.
|
||||
* Otherwise delegate to default attribute serializer.
|
||||
*
|
||||
* @override
|
||||
* @method serializeAttribute
|
||||
* @param {Snapshot} snapshot
|
||||
* @param {Object} json
|
||||
* @param {String} key
|
||||
* @param {Object} attribute
|
||||
*/
|
||||
serializeAttribute(snapshot, json, key) {
|
||||
const value = super.serializeAttribute(...arguments);
|
||||
const parentScope = snapshot.attr('scope');
|
||||
if (key === 'scope' && this._canSerialize(key) && parentScope) {
|
||||
json.scope_id = parentScope.attr('scope_id');
|
||||
delete json[key];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'api/adapters/scope';
|
||||
@@ -1 +0,0 @@
|
||||
export { default } from 'api/serializers/scope';
|
||||
@@ -0,0 +1,23 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
||||
|
||||
module('Unit | Adapter | scope', function(hooks) {
|
||||
setupTest(hooks);
|
||||
setupMirage(hooks);
|
||||
|
||||
test('it generates correct createRecord URLs with a scope_id query parameter', function (assert) {
|
||||
assert.expect(1);
|
||||
this.server.post('/v1/scopes', (schema, { queryParams }) =>{
|
||||
assert.equal(queryParams.scope_id, 'o_1');
|
||||
});
|
||||
const store = this.owner.lookup('service:store');
|
||||
const scope = store.createRecord('scope', {
|
||||
scopeID: 'o_1'
|
||||
});
|
||||
const type = scope.constructor;
|
||||
const snapshot = scope._createSnapshot();
|
||||
const adapter = this.owner.lookup('adapter:scope');
|
||||
adapter.createRecord(store, type, snapshot);
|
||||
});
|
||||
});
|
||||
@@ -1,32 +0,0 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Serializer | scope', function(hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
// Replace this with your real tests.
|
||||
test('it exists', function(assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let serializer = store.serializerFor('scope');
|
||||
|
||||
assert.ok(serializer);
|
||||
});
|
||||
|
||||
test('it serializes records', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const record = store.createRecord('scope', {
|
||||
type: 'org',
|
||||
name: 'User',
|
||||
description: 'Description',
|
||||
scopeID: 'global'
|
||||
});
|
||||
const serializedRecord = record.serialize();
|
||||
assert.deepEqual(serializedRecord, {
|
||||
type: 'org',
|
||||
name: 'User',
|
||||
description: 'Description',
|
||||
scope_id: 'global'
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -29,8 +29,7 @@ export default function() {
|
||||
// to be expanded to a scope object `scope: {id:..., type:...}` before
|
||||
// saving, which is the gist of this function.
|
||||
const attrs = this.normalizedRequestAttrs();
|
||||
const parentScopeAttrs = this.serialize(scopes.find(attrs.scopeId));
|
||||
delete attrs.scopeId;
|
||||
const parentScopeAttrs = this.serialize(scopes.find(request.queryParams.scope_id));
|
||||
attrs.scope = parentScopeAttrs;
|
||||
return scopes.create(attrs);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user