From 8f9a864ddba213ec1aca64a5fba788131c45b6d5 Mon Sep 17 00:00:00 2001 From: Jonas Cosandey Date: Fri, 14 Aug 2020 16:58:41 +0200 Subject: [PATCH] feat(meta-filter): add meta filter config --- README.md | 50 ++++ addon/components/category-nav/category.hbs | 5 +- addon/components/document-upload-button.js | 6 +- addon/controllers/application.js | 21 ++ addon/engine.js | 2 +- addon/models/category.js | 1 + addon/models/document.js | 1 + addon/models/file.js | 1 + addon/models/tag.js | 1 + addon/routes/application.js | 10 + addon/services/config.js | 17 ++ addon/templates/application.hbs | 2 +- app/services/config.js | 1 + tests/dummy/app/app.js | 8 +- tests/dummy/app/router.js | 5 +- tests/dummy/app/services/alexandria-config.js | 19 ++ tests/dummy/app/styles/app.scss | 8 +- tests/dummy/app/templates/application.hbs | 214 ++---------------- 18 files changed, 174 insertions(+), 198 deletions(-) create mode 100644 addon/routes/application.js create mode 100644 addon/services/config.js create mode 100644 app/services/config.js create mode 100644 tests/dummy/app/services/alexandria-config.js diff --git a/README.md b/README.md index ce3120c2..3652d902 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,56 @@ constructor(...args) { } ``` +Configuration +------------------------------------------------------------------------------ + +You can configure if the models should be filtered by meta and what the default +meta value for a model should be. Each configuration field is scoped by model name +(check out the example to understand what is meant by this). + +For this you need to create a service extending from +`ember-alexandria/services/config` which you then have to pass as `config` to +alexandria. + +If you mounted alexandria with query params +`this.mount("ember-alexandria", {path: "/:your_query_param/documents/"});` +you can access the query params in you config service (as shown in the example +above) with `this.emeisQueryParams.your_query_param`. + + +If you need to access the `emeisQueryParams` inside your config check that you define `modelMetaFilters` +and/or `defaultModelMeta` as getters. If you dont need `emeisQueryParams` you +can ignore the getters and just define the field as usual. + +__Example__: +```js +import ConfigService from "ember-alexandria/services/config"; + +export default class AlexandriaConfigService extends ConfigService { + get modelMetaFilters() { + return { + document: [ + { key: "your_meta_field", value: this.emeisQueryParams.your_query_param +}, + ], + }; + } + + get defaultModelMeta() { + return { + document: { + your_meta_field: this.emeisQueryParams.your_query_param, + }, + file: { + is_alexandria_file: true + } + }; + } +} +``` + + + Contributing ------------------------------------------------------------------------------ diff --git a/addon/components/category-nav/category.hbs b/addon/components/category-nav/category.hbs index 615b9ddc..46b999eb 100644 --- a/addon/components/category-nav/category.hbs +++ b/addon/components/category-nav/category.hbs @@ -1,5 +1,8 @@
  • - +
    { const documentModel = this.store.createRecord("document", { category, + meta: this.config.defaultModelMeta.document, }); documentModel.title = file.name; await documentModel.save(); @@ -37,7 +39,9 @@ export default class DocumentUploadButtonComponent extends Component { body: file, }); - console.error("Needs error handling here", response); + if (!response.ok) { + throw new Error("The request returned an error status code"); + } }) ); this.notification.success( diff --git a/addon/controllers/application.js b/addon/controllers/application.js index 42ae74fd..e6ca8912 100644 --- a/addon/controllers/application.js +++ b/addon/controllers/application.js @@ -1,11 +1,32 @@ import Controller from "@ember/controller"; +import { inject as service } from "@ember/service"; import { tracked } from "@glimmer/tracking"; export default class ApplicationController extends Controller { queryParams = ["category", "tags", "search", "document"]; + + @service config; + @tracked category; // Cant use @tracked tags = []; because of https://github.com/emberjs/ember.js/issues/19078 @tracked tags; @tracked search; @tracked document; + + get documentFilters() { + let filters = { + category: this.category, + tags: this.tags, + search: this.serach, + }; + + if (this.config && this.config.modelMetaFilters.document) { + filters = { + ...filters, + meta: JSON.stringify(this.config.modelMetaFilters.document), + }; + } + + return filters; + } } diff --git a/addon/engine.js b/addon/engine.js index 9bab7cd1..7b029f73 100644 --- a/addon/engine.js +++ b/addon/engine.js @@ -11,7 +11,7 @@ export default class EmberAlexandriaEngine extends Engine { Resolver = Resolver; dependencies = { - services: ["store", "intl", "notification", "router"], + services: ["store", "intl", "notification", "router", "config"], }; } diff --git a/addon/models/category.js b/addon/models/category.js index bfbd2589..0c20e9ae 100644 --- a/addon/models/category.js +++ b/addon/models/category.js @@ -5,6 +5,7 @@ export default class CategoryModel extends LocalizedModel { @localizedAttr name; @attr description; @attr color; + @attr meta; @hasMany documents; } diff --git a/addon/models/document.js b/addon/models/document.js index 7e26a63c..3913bead 100644 --- a/addon/models/document.js +++ b/addon/models/document.js @@ -7,6 +7,7 @@ export default class DocumentModel extends LocalizedModel { @attr createdAt; @attr createdByUser; @attr createdByGroup; + @attr meta; @belongsTo category; @hasMany tags; diff --git a/addon/models/file.js b/addon/models/file.js index d8676a2e..c8c18a5b 100644 --- a/addon/models/file.js +++ b/addon/models/file.js @@ -6,6 +6,7 @@ export default class FileModel extends Model { @attr uploadUrl; @attr downloadUrl; @attr objectName; + @attr meta; @belongsTo document; diff --git a/addon/models/tag.js b/addon/models/tag.js index 835fcc76..abf05885 100644 --- a/addon/models/tag.js +++ b/addon/models/tag.js @@ -3,6 +3,7 @@ import Model, { attr, hasMany } from "@ember-data/model"; export default class TagModel extends Model { @attr name; @attr description; + @attr meta; @hasMany documents; } diff --git a/addon/routes/application.js b/addon/routes/application.js new file mode 100644 index 00000000..431b0a8e --- /dev/null +++ b/addon/routes/application.js @@ -0,0 +1,10 @@ +import Route from "@ember/routing/route"; +import { inject as service } from "@ember/service"; + +export default class ApplicationRoute extends Route { + @service config; + + model(_, transition) { + this.config.emeisQueryParams = transition.to.parent.params; + } +} diff --git a/addon/services/config.js b/addon/services/config.js new file mode 100644 index 00000000..d375b2b9 --- /dev/null +++ b/addon/services/config.js @@ -0,0 +1,17 @@ +import Service from "@ember/service"; +import { tracked } from "@glimmer/tracking"; + +export default class ConfigService extends Service { + @tracked emeisQueryParams = {}; + + /* Defaults so we can lookup + * `this.config.modelMetaFilters.document` + * without an exeption on modelMetaFilters. + */ + get modelMetaFilters() { + return {}; + } + get defaultModelMeta() { + return {}; + } +} diff --git a/addon/templates/application.hbs b/addon/templates/application.hbs index c0026de3..8f6ce3c7 100644 --- a/addon/templates/application.hbs +++ b/addon/templates/application.hbs @@ -4,7 +4,7 @@ diff --git a/app/services/config.js b/app/services/config.js new file mode 100644 index 00000000..513bc8b3 --- /dev/null +++ b/app/services/config.js @@ -0,0 +1 @@ +export { default } from "ember-alexandria/services/config"; diff --git a/tests/dummy/app/app.js b/tests/dummy/app/app.js index 9a0b4284..8fbf2f90 100644 --- a/tests/dummy/app/app.js +++ b/tests/dummy/app/app.js @@ -15,7 +15,13 @@ export default class App extends Application { this.engines = { emberAlexandria: { dependencies: { - services: ["store", "intl", "notification", "router"], + services: [ + "store", + "intl", + "notification", + "router", + { config: "alexandria-config" }, + ], }, }, }; diff --git a/tests/dummy/app/router.js b/tests/dummy/app/router.js index b922a6b2..b8992bf2 100644 --- a/tests/dummy/app/router.js +++ b/tests/dummy/app/router.js @@ -9,5 +9,8 @@ export default class Router extends EmberRouter { /* eslint-disable-next-line array-callback-return */ Router.map(function () { - this.mount("ember-alexandria", { path: "/" }); + this.mount("ember-alexandria", { + path: "/:instance_id/documents/", + as: "alexandria", + }); }); diff --git a/tests/dummy/app/services/alexandria-config.js b/tests/dummy/app/services/alexandria-config.js new file mode 100644 index 00000000..b6af952b --- /dev/null +++ b/tests/dummy/app/services/alexandria-config.js @@ -0,0 +1,19 @@ +import ConfigService from "ember-alexandria/services/config"; + +export default class AlexandriaConfigService extends ConfigService { + get modelMetaFilters() { + return { + document: [ + { key: "instance_id", value: this.emeisQueryParams.instance_id }, + ], + }; + } + + get defaultModelMeta() { + return { + document: { + instance_id: this.emeisQueryParams.instance_id, + }, + }; + } +} diff --git a/tests/dummy/app/styles/app.scss b/tests/dummy/app/styles/app.scss index 666bf770..37413b84 100644 --- a/tests/dummy/app/styles/app.scss +++ b/tests/dummy/app/styles/app.scss @@ -1,4 +1,8 @@ -$global-primary-background: #c00; - @import "ember-uikit"; @import "ember-alexandria"; + +#container { + height: 100vh; + max-height: 100vh; + overflow: hidden; +} diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index c7fc86b5..496af29e 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -1,194 +1,28 @@ -{{!template-lint-disable}} - - - - - -
    - -
    -
    -
    - - -
    - -
    - -
    - -
    +
    +
    - -
    -
    -
    - -
    -
    - {{outlet}} -
    -
    +
    + {{outlet}}