Skip to content

Commit

Permalink
adding computed macros for lazy capability fetching and using them in…
Browse files Browse the repository at this point in the history
… the identity models
  • Loading branch information
meirish committed May 23, 2018
1 parent f317154 commit feba7a9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 64 deletions.
9 changes: 9 additions & 0 deletions ui/app/macros/identity-capabilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';

export default function() {
return lazyCapabilities(
apiPath`identity/${'identityType'}/id/${'id'}`,
'id',
'identityType'
);
}
25 changes: 25 additions & 0 deletions ui/app/macros/lazy-capabilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { queryRecord } from 'ember-computed-query';

export function apiPath(strings, ...keys) {
return (function(data) {
let dict = data || {};
let result = [strings[0]];
keys.forEach((key, i) => {
result.push(dict[key], strings[i + 1]);
});
return result.join('');
});
};

export default function() {
let [templateFn, ...keys] = arguments;
return queryRecord(
'capabilities',
context => {
return {
id: templateFn(context.getProperties(...keys))
}
},
...keys
);
}
16 changes: 3 additions & 13 deletions ui/app/models/identity/entity-alias.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import IdentityModel from './_base';
import DS from 'ember-data';
import Ember from 'ember';
import { queryRecord } from 'ember-computed-query';
import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';
import identityCapabilities from 'vault/macros/identity-capabilities';
const { attr, belongsTo } = DS;
const { computed } = Ember;

Expand Down Expand Up @@ -33,18 +34,7 @@ export default IdentityModel.extend({
}),
mergedFromCanonicalIds: attr(),

updatePath: queryRecord(
'capabilities',
context => {
const { identityType, id } = context.getProperties('identityType', 'id');
//identity/entity-alias/id/efb8b562-77fd-335f-a754-740373a778e6
return {
id: `identity/${identityType}/id/${id}`,
};
},
'id',
'identityType'
),
updatePath: identityCapabilities(),
canDelete: computed.alias('updatePath.canDelete'),
canEdit: computed.alias('updatePath.canUpdate'),
});
24 changes: 5 additions & 19 deletions ui/app/models/identity/entity.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Ember from 'ember';
import IdentityModel from './_base';
import DS from 'ember-data';
import { queryRecord } from 'ember-computed-query';
import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';
import identityCapabilities from 'vault/macros/identity-capabilities';

const { computed } = Ember;

const { attr, hasMany } = DS;
Expand Down Expand Up @@ -38,26 +40,10 @@ export default IdentityModel.extend({
readOnly: true,
}),

updatePath: queryRecord(
'capabilities',
context => {
const { identityType, id } = context.getProperties('identityType', 'id');
//identity/entity/id/efb8b562-77fd-335f-a754-740373a778e6
return {
id: `identity/${identityType}/id/${id}`,
};
},
'id',
'identityType'
),
updatePath: identityCapabilities(),
canDelete: computed.alias('updatePath.canDelete'),
canEdit: computed.alias('updatePath.canUpdate'),
aliasPath: queryRecord('capabilities', () => {
//identity/entity-alias
return {
id: `identity/entity-alias`,
};
}),

aliasPath: lazyCapabilities(apiPath`identity/entity-alias`),
canAddAlias: computed.alias('aliasPath.canCreate'),
});
16 changes: 3 additions & 13 deletions ui/app/models/identity/group-alias.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import IdentityModel from './_base';
import DS from 'ember-data';
import Ember from 'ember';
import { queryRecord } from 'ember-computed-query';
import identityCapabilities from 'vault/macros/identity-capabilities';

const { attr, belongsTo } = DS;
const { computed } = Ember;
Expand Down Expand Up @@ -31,18 +31,8 @@ export default IdentityModel.extend({
lastUpdateTime: attr('string', {
readOnly: true,
}),
updatePath: queryRecord(
'capabilities',
context => {
const { identityType, id } = context.getProperties('identityType', 'id');
//identity/entity-alias/id/efb8b562-77fd-335f-a754-740373a778e6
return {
id: `identity/${identityType}/id/${id}`,
};
},
'id',
'identityType'
),

updatePath: identityCapabilities(),
canDelete: computed.alias('updatePath.canDelete'),
canEdit: computed.alias('updatePath.canUpdate'),

Expand Down
23 changes: 4 additions & 19 deletions ui/app/models/identity/group.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Ember from 'ember';
import IdentityModel from './_base';
import DS from 'ember-data';
import { queryRecord } from 'ember-computed-query';
import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';
import identityCapabilities from 'vault/macros/identity-capabilities';

const { computed } = Ember;
const { attr, belongsTo } = DS;
Expand Down Expand Up @@ -53,27 +54,11 @@ export default IdentityModel.extend({
),

alias: belongsTo('identity/group-alias', { async: false, readOnly: true }),
updatePath: queryRecord(
'capabilities',
context => {
const { identityType, id } = context.getProperties('identityType', 'id');
//identity/group/id/efb8b562-77fd-335f-a754-740373a778e6
return {
id: `identity/${identityType}/id/${id}`,
};
},
'id',
'identityType'
),
updatePath: identityCapabilities(),
canDelete: computed.alias('updatePath.canDelete'),
canEdit: computed.alias('updatePath.canUpdate'),
aliasPath: queryRecord('capabilities', () => {
//identity/entity-alias
return {
id: `identity/group-alias`,
};
}),

aliasPath: lazyCapabilities(apiPath`identity/group-alias`),
canAddAlias: computed('aliasPath.canCreate', 'type', 'alias', function() {
let type = this.get('type');
let alias = this.get('alias');
Expand Down

0 comments on commit feba7a9

Please sign in to comment.