Skip to content

Commit

Permalink
feat(emeisOptions): add custom column option
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabauke committed Jun 30, 2022
1 parent 3d1bfce commit 9a2b6e2
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export default class EmeisOptionsService extends Service {
city: "optional",
zip: "optional",
},
// adds additional custom columns to this model's list view. *Scopes are not supported, since they are represented as a tree!*
customColumns: [
{
heading: "Funktion", // ember-intl or string
slug: "additional_column_function", // relative to "model.metainfo[slug]"
sortable: true, // whether sorting is supported for this column
localized: true, // whether to expect a plain value or a object with localized values
},
],
/*
On each model edit view (e.g. users) you can define a custom component. The component will be rendered at the bottom of the edit view, but above the primary form buttons. Each component can be designed freely and the model will be passed into the component as `@model` argument. For a working demo have a look at our "dummy-button" at "dummy/app/components/dummy-button".
*/
Expand Down
10 changes: 9 additions & 1 deletion addon/controllers/permissions/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { inject as service } from "@ember/service";

import PaginationController from "ember-emeis/-private/controllers/pagination";

export default class PermissionsIndexController extends PaginationController {}
export default class PermissionsIndexController extends PaginationController {
@service emeisOptions;

get customColumns() {
return this.emeisOptions.permission?.customColumns;
}
}
10 changes: 9 additions & 1 deletion addon/controllers/roles/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { inject as service } from "@ember/service";

import PaginationController from "ember-emeis/-private/controllers/pagination";

export default class RolesIndexController extends PaginationController {}
export default class RolesIndexController extends PaginationController {
@service emeisOptions;

get customColumns() {
return this.emeisOptions.role?.customColumns;
}
}
4 changes: 4 additions & 0 deletions addon/controllers/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default class UsersIndexController extends PaginationController {
@service notification;
@service intl;

get customColumns() {
return this.emeisOptions.user?.customColumns;
}

get emailAsUsername() {
return this.emeisOptions.emailAsUsername;
}
Expand Down
20 changes: 20 additions & 0 deletions addon/helpers/optional-localized-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Helper from "@ember/component/helper";
import { inject as service } from "@ember/service";

export default class OptionalLocalizedValue extends Helper {
@service intl;
@service emeisOptions;

getFieldLocale(modelName) {
return modelName
? this.emeisOptions.forceLocale?.[modelName]
: undefined || this.intl.localizedFieldLocale || this.intl.primaryLocale;
}

compute([field, modelName]) {
if (typeof field === "string") {
return field;
}
return field[this.getFieldLocale(modelName)];
}
}
10 changes: 10 additions & 0 deletions addon/templates/permissions/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<th>
{{t "emeis.permissions.headings.description"}}
</th>
{{#each this.customColumns as |extraColumn|}}
<Column @sort={{if extraColumn.sort extraColumn.slug}}>
{{optional-translate extraColumn.heading}}
</Column>
{{/each}}
</table.head>
<table.body as |body|>
<body.row>
Expand All @@ -36,6 +41,11 @@
<td class="uk-width-expand" data-test-permission-desc={{permission.id}}>
{{permission.description}}
</td>
{{#each this.customColumns as |extraColumn|}}
<td class="uk-width-expand" data-test-custom-column={{extraColumn.slug}}>
{{optional-localized-value (get permission.metainfo extraColumn.slug) "permission"}}
</td>
{{/each}}
{{/let}}
</body.row>
</table.body>
Expand Down
11 changes: 10 additions & 1 deletion addon/templates/roles/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<th>
{{t "emeis.roles.headings.description"}}
</th>
{{#each this.customColumns as |extraColumn|}}
<Column @sort={{if extraColumn.sort extraColumn.slug}}>
{{optional-translate extraColumn.heading}}
</Column>
{{/each}}
</table.head>
<table.body as |body|>
<body.row>
Expand All @@ -28,10 +33,14 @@
<td class="uk-width-1-4" data-test-role-slug={{role.id}}>
{{role.slug}}
</td>

<td class="uk-width-expand" data-test-role-desc={{role.id}}>
{{role.description}}
</td>
{{#each this.customColumns as |extraColumn|}}
<td class="uk-width-expand" data-test-custom-column={{extraColumn.slug}}>
{{optional-localized-value (get role.metainfo extraColumn.slug) "role"}}
</td>
{{/each}}
{{/let}}
</body.row>
</table.body>
Expand Down
10 changes: 10 additions & 0 deletions addon/templates/users/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<Column @sort="email">
{{t "emeis.users.headings.email"}}
</Column>
{{#each this.customColumns as |extraColumn|}}
<Column @sort={{if extraColumn.sort extraColumn.slug}}>
{{optional-translate extraColumn.heading}}
</Column>
{{/each}}
<Column>
{{t "emeis.roles.title"}}
</Column>
Expand Down Expand Up @@ -56,6 +61,11 @@
<td class="uk-link-text {{unless user.isActive "text-line-through"}}" data-test-user-email={{user.id}}>
{{user.email}}
</td>
{{#each this.customColumns as |extraColumn|}}
<td class="uk-link-text {{unless user.isActive "text-line-through"}}" data-test-custom-column={{extraColumn.slug}}>
{{optional-localized-value (get user.metainfo extraColumn.slug) "user"}}
</td>
{{/each}}
<td class="uk-link-text {{unless user.isActive "text-line-through"}}">
<ul class="uk-list uk-list-divider">
{{#each user.acls as |acl|}}
Expand Down
1 change: 1 addition & 0 deletions app/helpers/optional-localized-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-emeis/helpers/optional-localized-value";
8 changes: 8 additions & 0 deletions tests/dummy/app/services/emeis-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export default class EmeisOptionsService extends Service {
// phone: "optional",
// language: "optional",
// }
customColumns: [
{
heading: "Funktion", // ember-intl or string
slug: "additional_column_function", // relative to "model.metainfo[slug]"
sortable: true, // whether sorting is supported for this column
localized: true, // whether to expect a plain value or a object with localized values
},
],
customComponent: TestButtonComponent,
metaFields: [
{
Expand Down
6 changes: 5 additions & 1 deletion tests/dummy/mirage/factories/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default Factory.extend({
address: () => faker.address.streetAddress(),
city: () => localize(faker.address.city()),
zip: () => faker.datatype.number(),
metainfo: () => {},
metainfo: () => {
return {
additional_column_function: faker.company.bsNoun(),
};
},
isActive: () => faker.datatype.boolean(),
});
25 changes: 25 additions & 0 deletions tests/integration/helpers/optional-localized-value-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { render } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupIntl } from "ember-intl/test-support";
import { setupRenderingTest } from "ember-qunit";
import { module, test } from "qunit";

module("Integration | Helper | optional-localized-value", function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, ["en"]);

test("it renders a localized field", async function (assert) {
this.set("inputValue", { en: "english", de: "deutsch" });

await render(hbs`{{optional-localized-value this.inputValue}}`);

assert.dom(this.element).hasText("english");
});
test("it renders a unlocalized field", async function (assert) {
this.set("inputValue", "1234-string");

await render(hbs`{{optional-localized-value this.inputValue}}`);

assert.dom(this.element).hasText("1234-string");
});
});

0 comments on commit 9a2b6e2

Please sign in to comment.