diff --git a/addons/api/addon/models/scope.js b/addons/api/addon/models/scope.js index abf1824eb..b62ca94ba 100644 --- a/addons/api/addon/models/scope.js +++ b/addons/api/addon/models/scope.js @@ -1,5 +1,12 @@ import GeneratedScopeModel from '../generated/models/scope'; import { belongsTo, hasMany } from '@ember-data/model'; +import { computed } from '@ember/object'; + +export const scopeTypes = { + global: 'global', + org: 'org', + project: 'project' +}; export default class ScopeModel extends GeneratedScopeModel { @@ -17,4 +24,38 @@ export default class ScopeModel extends GeneratedScopeModel { */ @hasMany('scope', {inverse: 'parentScope'}) childrenScopes; + // =computed + + /** + * @type {boolean} + */ + @computed('type') + get isGlobal() { + return this.type === scopeTypes.global; + } + // There is only one global scope and it cannot be created by clients, + // thus no set. + + /** + * @type {boolean} + */ + @computed('type') + get isOrg() { + return this.type === scopeTypes.org; + } + set isOrg(value) { + if (value) this.type = scopeTypes.org; + } + + /** + * @type {boolean} + */ + @computed('type') + get isProject() { + return this.type === scopeTypes.project; + } + set isProject(value) { + if (value) this.type = scopeTypes.project; + } + } diff --git a/addons/api/tests/unit/models/scope-test.js b/addons/api/tests/unit/models/scope-test.js index 2e6fcdd60..07cf265ec 100644 --- a/addons/api/tests/unit/models/scope-test.js +++ b/addons/api/tests/unit/models/scope-test.js @@ -7,8 +7,9 @@ module('Unit | Model | scope', function(hooks) { setupMirage(hooks); test('it exists', function(assert) { - let store = this.owner.lookup('service:store'); - let model = store.createRecord('scope', {}); + assert.expect(1); + const store = this.owner.lookup('service:store'); + const model = store.createRecord('scope', {}); assert.ok(model); }); @@ -38,4 +39,21 @@ module('Unit | Model | scope', function(hooks) { assert.equal(await scopes.objectAt(5).get('parentScope.id'), 'o_2', 'Project 3 parent scope is org 2'); }); + test('it has isType boolean getters and setters', async function(assert) { + assert.expect(9); + const store = this.owner.lookup('service:store'); + const model = store.createRecord('scope', {type: 'global'}); + assert.ok(model.isGlobal); + assert.notOk(model.isOrg); + assert.notOk(model.isProject); + model.isOrg = true; + assert.notOk(model.isGlobal); + assert.ok(model.isOrg); + assert.notOk(model.isProject); + model.isProject = true; + assert.notOk(model.isGlobal); + assert.notOk(model.isOrg); + assert.ok(model.isProject); + }); + });