Moved table config in a service

Change-Id: Ide54d7489894535cacaa4b2d839b7bc7eb23e5e4
diff --git a/src/app/core/services/helpers/config.helpers.spec.ts b/src/app/core/services/helpers/config.helpers.spec.ts
index af424e4..2431ccb 100644
--- a/src/app/core/services/helpers/config.helpers.spec.ts
+++ b/src/app/core/services/helpers/config.helpers.spec.ts
@@ -4,8 +4,37 @@
 
 import {IXosConfigHelpersService} from './config.helpers';
 import {xosCore} from '../../index';
+import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
+import {IXosTableCfg} from '../../table/table';
 
 let service: IXosConfigHelpersService;
+
+const model: IModeldef = {
+  name: 'Test',
+  fields: [
+    {
+      type: 'number',
+      name: 'id',
+      validators: {}
+    },
+    {
+      type: 'string',
+      name: 'name',
+      validators: {}
+    },
+    {
+      type: 'string',
+      name: 'something',
+      validators: {}
+    },
+    {
+      type: 'date',
+      name: 'updated',
+      validators: {}
+    },
+  ]
+};
+
 describe('The ConfigHelpers service', () => {
 
   beforeEach(angular.mock.module(xosCore));
@@ -60,4 +89,32 @@
     });
   });
 
+  describe('the modelFieldsToColumnsCfg method', () => {
+    it('should return an array of columns', () => {
+      const cols = service.modelFieldsToColumnsCfg(model.fields, 'testUrl/:id?');
+      expect(cols[0].label).toBe('Id');
+      expect(cols[0].prop).toBe('id');
+      expect(cols[0].link).toBeDefined();
+
+      expect(cols[1].label).toBe('Name');
+      expect(cols[1].prop).toBe('name');
+      expect(cols[1].link).toBeDefined();
+
+      expect(cols[2].label).toBe('Something');
+      expect(cols[2].prop).toBe('something');
+      expect(cols[2].link).not.toBeDefined();
+
+      expect(cols[3]).not.toBeDefined();
+    });
+  });
+
+  describe('the modelToTableCfg method', () => {
+    it('should return a table config', () => {
+      const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
+      expect(cfg.columns).toBeDefined();
+      expect(cfg.filter).toBe('fulltext');
+      expect(cfg.order).toEqual({field: 'id', reverse: false});
+      expect(cfg.actions.length).toBe(1);
+    });
+  });
 });
diff --git a/src/app/core/services/helpers/config.helpers.ts b/src/app/core/services/helpers/config.helpers.ts
index 9b8d63c..332c60c 100644
--- a/src/app/core/services/helpers/config.helpers.ts
+++ b/src/app/core/services/helpers/config.helpers.ts
@@ -1,15 +1,18 @@
 import * as _ from 'lodash';
 import * as pluralize from 'pluralize';
-import {IXosTableColumn} from '../../table/table';
+import {IXosTableColumn, IXosTableCfg} from '../../table/table';
+import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
 
 export interface IXosModelDefsField {
   name: string;
   type: string;
+  validators?: any;
 }
 
 export interface IXosConfigHelpersService {
   excluded_fields: string[];
-  modeldefToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): any[]; // TODO use a proper interface
+  modelToTableCfg(model: IModeldef, baseUrl: string): IXosTableCfg;
+  modelFieldsToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[]; // TODO use a proper interface
   pluralize(string: string, quantity?: number, count?: boolean): string;
   toLabel(string: string, pluralize?: boolean): string;
   toLabels(string: string[], pluralize?: boolean): string[];
@@ -34,7 +37,9 @@
     'password'
   ];
 
-  constructor() {
+  constructor(
+    private toastr: ng.toastr.IToastrService
+  ) {
     pluralize.addIrregularRule('xos', 'xosses');
     pluralize.addPluralRule(/slice$/i, 'slices');
   }
@@ -64,7 +69,38 @@
     return this.capitalizeFirst(string);
   }
 
-  modeldefToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] {
+  modelToTableCfg(model: IModeldef, baseUrl: string): IXosTableCfg {
+    const cfg = {
+      columns: this.modelFieldsToColumnsCfg(model.fields, baseUrl),
+      filter: 'fulltext',
+      order: {field: 'id', reverse: false},
+      actions: [
+        {
+          label: 'delete',
+          icon: 'remove',
+          color: 'red',
+          cb: (item) => {
+            let obj = angular.copy(item);
+
+            item.$delete()
+              .then((res) => {
+                if (res.status === 404) {
+                  // TODO understand why it does not go directly in catch
+                  throw new Error();
+                }
+                this.toastr.info(`${model.name} ${obj.name} succesfully deleted`);
+              })
+              .catch(() => {
+                this.toastr.error(`Error while deleting ${obj.name}`);
+              });
+          }
+        }
+      ]
+    };
+    return cfg;
+  }
+
+  modelFieldsToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] {
 
     const columns =  _.map(fields, (f) => {
       if (this.excluded_fields.indexOf(f.name) > -1) {
diff --git a/src/app/datasources/rest/modeldefs.rest.ts b/src/app/datasources/rest/modeldefs.rest.ts
index d05aa6d..d5a5541 100644
--- a/src/app/datasources/rest/modeldefs.rest.ts
+++ b/src/app/datasources/rest/modeldefs.rest.ts
@@ -1,13 +1,9 @@
 import {AppConfig} from '../../config/app.config';
-
-interface IModeldefField {
-  name: string;
-  type: string;
-}
+import {IXosModelDefsField} from '../../core/services/helpers/config.helpers';
 
 export interface IModeldef {
-  fields: IModeldefField[];
-  relations: string[];
+  fields: IXosModelDefsField[];
+  relations?: string[];
   name: string;
 }
 
diff --git a/src/index.ts b/src/index.ts
index 0760b26..2a3129a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -92,34 +92,7 @@
             data: {
               model: m.name,
               related: m.relations,
-              xosTableCfg: {
-                columns: ConfigHelpers.modeldefToColumnsCfg(m.fields, stateUrl),
-                filter: 'fulltext',
-                order: {field: 'id', reverse: false}, // TODO understand dynamic interfaces
-                actions: [
-                  {
-                    label: 'delete',
-                    icon: 'remove',
-                    color: 'red',
-                    cb: (item) => {
-                      let obj = angular.copy(item);
-
-                      item.$delete()
-                        .then((res) => {
-                          if (res.status === 404) {
-                            // TODO understand why it does not go directly in catch
-                            throw new Error();
-                          }
-                          toastr.info(`${m.name} ${obj.name} succesfully deleted`);
-                        })
-                        .catch(() => {
-                          console.log(obj);
-                          toastr.error(`Error while deleting ${obj.name}`);
-                        });
-                    }
-                  }
-                ]
-              },
+              xosTableCfg: ConfigHelpers.modelToTableCfg(m, stateUrl)
               // TODO add form config
             }
           };