chore: 🤖 Refactor role CRUD+L under /scopes/:scope_id

This commit is contained in:
Susmitha Girumala committed 2020-07-27 16:51:41 -04:00
1 parent 79d024c07c
commit 3ac5f766ff
18 files changed
+286 -1

No files matched your search

+4
View File
@@ -21,6 +21,10 @@ Router.map(function () {
this.route('group', { path: ':group_id' }, function () {});
this.route('new');
});
this.route('roles', function () {
this.route('role', { path: ':role_id' }, function () {});
this.route('new');
});
});
});
+72
View File
@@ -0,0 +1,72 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class ScopesScopeRolesRoute extends Route {
// =services
@service intl;
@service notify;
// =methods
/**
* Load all roles under current scope.
* @return {Promise{[RoleModel]}}
*/
async model() {
return this.store.findAll('role', this.scopeAdapterOptions());
}
// =actions
/**
* Rollback changes on an role.
* @param {RoleModel} role
*/
@action
cancel(role) {
const { isNew } = role;
role.rollbackAttributes();
if (isNew) this.transitionTo('scopes.scope.roles');
}
/**
* Save an role in current scope.
* @param {RoleModel} role
*/
@action
async save(role) {
try {
await role.save(this.scopeAdapterOptions());
this.refresh();
this.notify.success(this.intl.t('notify.save-success'));
this.transitionTo('scopes.scope.roles.role', role);
} catch (error) {
// TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
}
}
/**
* Delete role in current scope and redirect to index.
* @param {RoleModel} role
*/
@action
async delete(role) {
try {
await role.destroyRecord(this.scopeAdapterOptions());
this.refresh();
this.notify.success(this.intl.t('notify.role.delete-success'));
this.transitionTo('scopes.scope.roles');
} catch (error) {
// TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
}
}
scopeAdapterOptions() {
const { id: scope_id } = this.modelFor('scopes.scope');
const adapterOptions = { scopeID: scope_id };
return { adapterOptions };
}
}
@@ -0,0 +1,3 @@
import Route from '@ember/routing/route';
export default class ScopesScopeRolesIndexRoute extends Route {}
@@ -0,0 +1,13 @@
import Route from '@ember/routing/route';
export default class ScopesScopeRolesNewRoute extends Route {
// =methods
/**
* Creates a new unsaved role.
* @return {RoleModel}
*/
model() {
return this.store.createRecord('role');
}
}
@@ -0,0 +1,17 @@
import Route from '@ember/routing/route';
export default class ScopesScopeRolesRoleRoute extends Route {
// =methods
/**
* Load a role in current scope.
* @param {object} params
* @param {string} params.role_id
* @return {RoleModel}
*/
async model({ role_id }) {
const { id: scope_id } = this.modelFor('scopes.scope');
const adapterOptions = { scopeID: scope_id };
return this.store.findRecord('role', role_id, { adapterOptions });
}
}
@@ -0,0 +1,3 @@
import Route from '@ember/routing/route';
export default class ScopesScopeRolesRoleIndexRoute extends Route {}
+1 -1
View File
@@ -58,7 +58,7 @@ export default class ScopesScopeUsersRoute extends Route {
await user.destroyRecord(this.scopeAdapterOptions());
this.refresh();
this.transitionTo('scopes.scope.users');
this.notify.success(this.intl.t('notify.delete-success'));
this.notify.success(this.intl.t('notify.user.delete-success'));
} catch (error) {
// TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
+3
View File
@@ -15,6 +15,9 @@
<nav.link @route="scopes.scope.groups">
{{t "resources.groups"}}
</nav.link>
<nav.link @route="scopes.scope.roles">
{{t "resources.roles"}}
</nav.link>
</Rose::Nav::Sidebar>
{{outlet "sidebar"}}
@@ -0,0 +1,3 @@
{{title (t "resources.roles")}}
{{outlet}}
@@ -0,0 +1,63 @@
<Rose::Layout::Page as |page|>
<page.breadcrumbs>
<BreadCrumbs />
</page.breadcrumbs>
<page.header>
<h2>{{t "resources.roles"}}</h2>
</page.header>
<page.actions>
{{#if @model}}
<Rose::LinkButton @route="scopes.scope.roles.new" @style="primary">{{t 'titles.new'}}</Rose::LinkButton>
{{/if}}
</page.actions>
<page.body>
{{#if @model}}
<Rose::Table as |table|>
<table.header as |header|>
<header.row as |row|>
<row.cell>{{t 'form.label.name'}}</row.cell>
<row.cell>{{t 'form.label.description'}}</row.cell>
</header.row>
</table.header>
<table.body as |body|>
{{#each @model as |role|}}
<body.row as |row|>
<row.cell>
<LinkTo
@route="scopes.scope.roles.role"
@model={{role}}
>
{{role.displayName}}
</LinkTo>
</row.cell>
<row.cell>{{role.description}}</row.cell>
</body.row>
{{/each}}
</table.body>
</Rose::Table>
{{else}}
<Rose::Layout::Centered>
<Rose::Message @title={{t 'titles.roles-welcome'}} as |message|>
<message.description>
{{t 'descriptions.roles-welcome'}}
</message.description>
<message.link @route="scopes.scope.roles.new">
<Rose::Icon @name="plus-circle-outline" />
{{t 'titles.new'}}
</message.link>
</Rose::Message>
</Rose::Layout::Centered>
{{/if}}
</page.body>
</Rose::Layout::Page>
@@ -0,0 +1,19 @@
{{title (t 'titles.new')}}
<Rose::Layout::Page as |page|>
<page.breadcrumbs>
<BreadCrumbs />
</page.breadcrumbs>
<page.header>
<h2>{{t "titles.new"}}</h2>
</page.header>
<page.body>
<Form::Role
@model={{@model}}
@submit={{route-action "save" @model}}
@cancel={{route-action "cancel" @model}}
/>
</page.body>
</Rose::Layout::Page>
@@ -0,0 +1,3 @@
{{title @model.displayName}}
{{outlet}}
@@ -0,0 +1,27 @@
<Rose::Layout::Page as |page|>
<page.breadcrumbs>
<BreadCrumbs />
</page.breadcrumbs>
<page.header>
<h2>{{t "resources.role"}}</h2>
</page.header>
<page.body>
<Form::Role
@model={{@model}}
@submit={{route-action "save" @model}}
@cancel={{route-action "cancel" @model}}
/>
<Rose::Separator />
<Rose::Button
@style="warning"
@disabled={{@model.canSave}}
{{on "click" (route-action "delete" @model)}}
>
{{t 'actions.delete'}}
</Rose::Button>
</page.body>
</Rose::Layout::Page>
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/roles', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let route = this.owner.lookup('route:scopes/scope/roles');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/roles/index', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let route = this.owner.lookup('route:scopes/scope/roles/index');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/roles/new', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let route = this.owner.lookup('route:scopes/scope/roles/new');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/roles/role', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let route = this.owner.lookup('route:scopes/scope/roles/role');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/roles/role/index', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let route = this.owner.lookup('route:scopes/scope/roles/role/index');
assert.ok(route);
});
});