Skip to content

Commit

Permalink
feat(meta-filter): add meta filter config
Browse files Browse the repository at this point in the history
  • Loading branch information
velrest authored and czosel committed Sep 1, 2020
1 parent f3344ec commit 8f9a864
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 198 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------------------------------------------------------------

Expand Down
5 changes: 4 additions & 1 deletion addon/components/category-nav/category.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<li class="category-nav--category">
<LinkTo @query={{hash category=@category.id search=undefined document=undefined}} class={{if @selected "active"}}>
<LinkTo
@query={{hash category=@category.id search=undefined document=undefined}}
class="uk-link-reset {{if @selected "active"}}"
>
<div class="uk-flex uk-flex-middle">
<div>
<FaIcon
Expand Down
6 changes: 5 additions & 1 deletion addon/components/document-upload-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class DocumentUploadButtonComponent extends Component {
@service notification;
@service intl;
@service store;
@service config;

@lastValue("fetchCategories") categories;

Expand All @@ -21,6 +22,7 @@ export default class DocumentUploadButtonComponent extends Component {
Array.from(files).map(async (file) => {
const documentModel = this.store.createRecord("document", {
category,
meta: this.config.defaultModelMeta.document,
});
documentModel.title = file.name;
await documentModel.save();
Expand All @@ -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(
Expand Down
21 changes: 21 additions & 0 deletions addon/controllers/application.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion addon/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class EmberAlexandriaEngine extends Engine {
Resolver = Resolver;

dependencies = {
services: ["store", "intl", "notification", "router"],
services: ["store", "intl", "notification", "router", "config"],
};
}

Expand Down
1 change: 1 addition & 0 deletions addon/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class CategoryModel extends LocalizedModel {
@localizedAttr name;
@attr description;
@attr color;
@attr meta;

@hasMany documents;
}
1 change: 1 addition & 0 deletions addon/models/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class DocumentModel extends LocalizedModel {
@attr createdAt;
@attr createdByUser;
@attr createdByGroup;
@attr meta;

@belongsTo category;
@hasMany tags;
Expand Down
1 change: 1 addition & 0 deletions addon/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class FileModel extends Model {
@attr uploadUrl;
@attr downloadUrl;
@attr objectName;
@attr meta;

@belongsTo document;

Expand Down
1 change: 1 addition & 0 deletions addon/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions addon/routes/application.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions addon/services/config.js
Original file line number Diff line number Diff line change
@@ -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 {};
}
}
2 changes: 1 addition & 1 deletion addon/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Search @search={{this.search}} />

<DocumentGrid
@filters={{hash category=this.category tags=this.tags search=this.search}}
@filters={{this.documentFilters}}
@selectedDocumentId={{this.document}}
class="uk-border-top"
/>
Expand Down
1 change: 1 addition & 0 deletions app/services/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/services/config";
8 changes: 7 additions & 1 deletion tests/dummy/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
],
},
},
};
Expand Down
5 changes: 4 additions & 1 deletion tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
19 changes: 19 additions & 0 deletions tests/dummy/app/services/alexandria-config.js
Original file line number Diff line number Diff line change
@@ -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,
},
};
}
}
8 changes: 6 additions & 2 deletions tests/dummy/app/styles/app.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
$global-primary-background: #c00;

@import "ember-uikit";
@import "ember-alexandria";

#container {
height: 100vh;
max-height: 100vh;
overflow: hidden;
}
214 changes: 24 additions & 190 deletions tests/dummy/app/templates/application.hbs

Large diffs are not rendered by default.

0 comments on commit 8f9a864

Please sign in to comment.