diff --git a/addons/api/addon/adapters/scope.js b/addons/api/addon/adapters/scope.js new file mode 100644 index 000000000..6d4549844 --- /dev/null +++ b/addons/api/addon/adapters/scope.js @@ -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 }); + } + +} diff --git a/addons/api/addon/serializers/scope.js b/addons/api/addon/serializers/scope.js deleted file mode 100644 index 5767a8015..000000000 --- a/addons/api/addon/serializers/scope.js +++ /dev/null @@ -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; - } - -} diff --git a/addons/api/app/adapters/scope.js b/addons/api/app/adapters/scope.js new file mode 100644 index 000000000..10878851f --- /dev/null +++ b/addons/api/app/adapters/scope.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/scope'; diff --git a/addons/api/app/serializers/scope.js b/addons/api/app/serializers/scope.js deleted file mode 100644 index cdc15f3a2..000000000 --- a/addons/api/app/serializers/scope.js +++ /dev/null @@ -1 +0,0 @@ -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 new file mode 100644 index 000000000..7dcd482c7 --- /dev/null +++ b/addons/api/tests/unit/adapters/scope-test.js @@ -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); + }); +}); diff --git a/addons/api/tests/unit/serializers/scope-test.js b/addons/api/tests/unit/serializers/scope-test.js deleted file mode 100644 index 3993238fe..000000000 --- a/addons/api/tests/unit/serializers/scope-test.js +++ /dev/null @@ -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' - }); - }); -}); diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 6e554c34a..ed6859c83 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -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); });