Skip to content

Commit

Permalink
feat(config): add resolver for user and group IDs
Browse files Browse the repository at this point in the history
The config service has an identity function as default resolvers.
  • Loading branch information
fkm-adfinis committed Feb 9, 2021
1 parent 042d24d commit 7364345
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 4 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ If you mounted alexandria with query params
you can access the query params in you config service (as shown in the example
above) with `this.alexandriaQueryParams.your_query_param`.


If you need to access the `alexandriaQueryParams` inside your config check that you define `modelMetaFilters`
and/or `defaultModelMeta` as getters. If you don't need `alexandriaQueryParams` you
can ignore the getters and just define the field as usual.
Expand Down Expand Up @@ -106,7 +105,23 @@ export default class AlexandriaConfigService extends ConfigService {
}
```

As the Alexandria backend returns IDs for the user and groups you can add a
resolver from your project to turn the IDs to something the user can relate to.

Just replace the identity functions on the config service.

__Example__:
```js
import ConfigService from "ember-alexandria/services/config";
import { inject as service } from "@ember/service";

export default class AlexandriaConfigService extends ConfigService {
@service store;

resolveUser(id) { return this.store.peekRecord("user", id); }
resolveGroup(id) { return this.store.peekRecord("group", id); }
}
```

Contributing
------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions addon/components/document-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@
</p>
<p data-test-created-by-user>
<FaIcon @icon="user" class="uk-margin-small-right" />
{{@document.createdByUser}}
{{resolve-user @document.createdByUser}}
</p>
<p data-test-created-by-group>
<FaIcon @icon="users" class="uk-margin-small-right" />
{{@document.createdByGroup}}
{{resolve-group @document.createdByGroup}}
</p>
</span>

Expand Down Expand Up @@ -191,7 +191,7 @@
{{moment-format file.createdAt "DD.MM.YYYY HH:mm"}}
</span>
<span class="uk-width-expand">
{{~file.createdByUser.fullName~}}
{{~resolve-user file.createdByUser~}}
</span>
<a
uk-icon="download"
Expand Down
10 changes: 10 additions & 0 deletions addon/helpers/resolve-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Helper from "@ember/component/helper";
import { inject as service } from "@ember/service";

export default class ResolveGroupHelper extends Helper {
@service config;

compute([id]) {
return this.config.resolveGroup(id);
}
}
10 changes: 10 additions & 0 deletions addon/helpers/resolve-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Helper from "@ember/component/helper";
import { inject as service } from "@ember/service";

export default class ResolveUserHelper extends Helper {
@service config;

compute([id]) {
return this.config.resolveUser(id);
}
}
3 changes: 3 additions & 0 deletions addon/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default class ConfigService extends Service {
*/
@tracked activeGroup = null;

resolveUser = (id) => id;
resolveGroup = (id) => id;

/**
* Defaults so we can lookup
* `this.config.modelMetaFilters.document`
Expand Down
1 change: 1 addition & 0 deletions app/helpers/resolve-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/helpers/resolve-group";
1 change: 1 addition & 0 deletions app/helpers/resolve-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/helpers/resolve-user";
16 changes: 16 additions & 0 deletions tests/integration/helpers/resolve-group-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { render } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupRenderingTest } from "ember-qunit";
import { module, test } from "qunit";

module("Integration | Helper | resolve-group", function (hooks) {
setupRenderingTest(hooks);

test("it renders", async function (assert) {
this.id = "1234";

await render(hbs`{{resolve-group this.id}}`);

assert.equal(this.element.textContent.trim(), "1234");
});
});
16 changes: 16 additions & 0 deletions tests/integration/helpers/resolve-user-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { render } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupRenderingTest } from "ember-qunit";
import { module, test } from "qunit";

module("Integration | Helper | resolve-user", function (hooks) {
setupRenderingTest(hooks);

test("it renders", async function (assert) {
this.id = "1234";

await render(hbs`{{resolve-group this.id}}`);

assert.equal(this.element.textContent.trim(), "1234");
});
});

0 comments on commit 7364345

Please sign in to comment.