From d5c00c2863ef500cced0710cd5ee09fb61cb9eb8 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 18 May 2020 12:29:33 -0400 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20support=20for=20e?= =?UTF-8?q?xclusively=20org-scoped=20resources?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/mixins/scope-adapter.js | 16 +++++++++++++++- .../api/tests/unit/mixins/scope-adapter-test.js | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/addons/api/addon/mixins/scope-adapter.js b/addons/api/addon/mixins/scope-adapter.js index 291eebd5f..705e5f1c0 100644 --- a/addons/api/addon/mixins/scope-adapter.js +++ b/addons/api/addon/mixins/scope-adapter.js @@ -7,8 +7,22 @@ import { inject as service } from '@ember/service'; * reported by `ScopeService`. */ export default Mixin.create({ + // =services + scope: service(), + // =properties + + /** + * If true and the current scope has a project, will include the project in + * the resource path. Set to false for resources that are scoped exclusively + * to org. + * @type {boolean} + */ + includeProject: true, + + // =methods + /** * Appends a scope path to the URL prefix, if a scope is set. * @override @@ -33,7 +47,7 @@ export default Mixin.create({ const { org, project } = this.scope; const scopePath = []; if (org) scopePath.push('orgs', org.id); - if (project) scopePath.push('projects', project.id); + if (project && this.includeProject) scopePath.push('projects', project.id); return scopePath.join('/'); }, }); diff --git a/addons/api/tests/unit/mixins/scope-adapter-test.js b/addons/api/tests/unit/mixins/scope-adapter-test.js index 7f0c9386b..5e042ae93 100644 --- a/addons/api/tests/unit/mixins/scope-adapter-test.js +++ b/addons/api/tests/unit/mixins/scope-adapter-test.js @@ -50,4 +50,19 @@ module('Unit | Mixin | scope-adapter', function (hooks) { url = adapter._buildURL('model', 1); assert.equal(url, '/api/v1/models/1'); }); + + test('it does not add a project scope to basic URLs when includeProject is false', function (assert) { + assert.expect(1); + const service = this.owner.lookup('service:scope'); + service.org = { id: 1 }; + service.project = { id: 2 }; + const ScopeAdapter = RESTAdapter.extend(ScopeAdapterMixin, { + namespace: 'api/v1', + includeProject: false, + }); + this.owner.register('adapter:scope', ScopeAdapter); + const adapter = this.owner.lookup('adapter:scope'); + let url = adapter._buildURL('model', 1); + assert.equal(url, '/api/v1/orgs/1/models/1'); + }); });