diff --git a/addons/api/addon/adapters/scope.js b/addons/api/addon/adapters/scope.js index f078e89df..27aa02e57 100644 --- a/addons/api/addon/adapters/scope.js +++ b/addons/api/addon/adapters/scope.js @@ -1,5 +1,4 @@ import ApplicationAdapter from './application'; -import { serializeIntoHash } from '@ember-data/adapter/-private'; export default class ScopeAdapter extends ApplicationAdapter { @@ -11,27 +10,4 @@ export default class ScopeAdapter extends ApplicationAdapter { */ hasScopePrefix = false; - // =methods - - /** - * 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 }); - } - } diff --git a/addons/api/addon/serializers/scope.js b/addons/api/addon/serializers/scope.js new file mode 100644 index 000000000..5e336e65f --- /dev/null +++ b/addons/api/addon/serializers/scope.js @@ -0,0 +1,11 @@ +import ApplicationSerializer from './application'; + +export default class ScopeSerializer extends ApplicationSerializer { + + /** + * Serialize the scope ID into scope_id. + * @type {boolean} + */ + serializeScopeID = true; + +} diff --git a/addons/api/app/serializers/scope.js b/addons/api/app/serializers/scope.js new file mode 100644 index 000000000..cdc15f3a2 --- /dev/null +++ b/addons/api/app/serializers/scope.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/scope'; diff --git a/addons/api/tests/unit/adapters/scope-test.js b/addons/api/tests/unit/adapters/scope-test.js index b38f491ce..f62f074a8 100644 --- a/addons/api/tests/unit/adapters/scope-test.js +++ b/addons/api/tests/unit/adapters/scope-test.js @@ -16,18 +16,4 @@ module('Unit | Adapter | scope', function(hooks) { assert.equal(prefix, `v1`); }); - 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); - }); }); diff --git a/addons/api/tests/unit/serializers/scope-test.js b/addons/api/tests/unit/serializers/scope-test.js new file mode 100644 index 000000000..d8b783344 --- /dev/null +++ b/addons/api/tests/unit/serializers/scope-test.js @@ -0,0 +1,31 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | scope', function(hooks) { + setupTest(hooks); + + test('it serializes scope_id into payloads', function (assert) { + assert.expect(2); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('scope'); + serializer.serializeScopeID = true; + const record = store.createRecord('scope', { + name: 'My Scope', + description: 'Foo', + type: 'org', + scope: { + scope_id: 'global', + type: 'global' + } + }); + const serializedRecord = record.serialize(); + assert.equal(serializer.serializeScopeID, true); + assert.deepEqual(serializedRecord, { + name: 'My Scope', + description: 'Foo', + type: 'org', + scope_id: 'global' + }); + }); + +}); diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index bbb7d46d9..cd9b48928 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -26,12 +26,12 @@ export default function() { }); return results; }); - this.post('/scopes', function ({ scopes }, request) { + this.post('/scopes', function ({ scopes }) { // Parent scope comes through the payload via `scope_id`, but this needs // 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(request.queryParams.scope_id)); + const parentScopeAttrs = this.serialize(scopes.find(attrs.scopeId)); attrs.scope = parentScopeAttrs; return scopes.create(attrs); });