[CORD-1927] Fixing generate url for service models

Change-Id: I08d0d853ba2ab041626e133d079ab4562d73171e
(cherry picked from commit 6349847ecd1040111de5e7def26304e9e093cb92)
diff --git a/src/app/views/crud/crud.relations.service.spec.ts b/src/app/views/crud/crud.relations.service.spec.ts
index 771c9a8..768c039 100644
--- a/src/app/views/crud/crud.relations.service.spec.ts
+++ b/src/app/views/crud/crud.relations.service.spec.ts
@@ -22,13 +22,14 @@
 } from './crud.relations.service';
 import {BehaviorSubject} from 'rxjs';
 import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
+import {XosFormHelpers} from '../../core/form/form-helpers';
 
 const XosModelStoreMock = {
   get: null,
   query: null
 };
 
-const XosModelDiscovererMock = {
+const XosModeldefsCacheMock = {
   get: null
 };
 
@@ -40,8 +41,9 @@
       .module('test', ['ui.router', 'toastr'])
       .service('XosCrudRelation', XosCrudRelationService)
       .value('XosModelStore', XosModelStoreMock)
-      .value('XosModelDiscoverer', XosModelDiscovererMock)
-      .service('ConfigHelpers', ConfigHelpers);
+      .value('XosModeldefsCache', XosModeldefsCacheMock)
+      .service('ConfigHelpers', ConfigHelpers)
+      .service('XosFormHelpers', XosFormHelpers);
 
     angular.mock.module('test');
   });
@@ -133,7 +135,7 @@
         subject.next(resModel);
         return subject.asObservable();
       });
-      spyOn(XosModelDiscovererMock, 'get').and.returnValue({formCfg: resFormCfg});
+      spyOn(XosModeldefsCacheMock, 'get').and.returnValue({formCfg: resFormCfg});
 
       service.getModel(relation, '5')
         .then((res: IXosCrudRelationFormTabData) => {
@@ -164,7 +166,7 @@
         const subject = new BehaviorSubject(resModels);
         return subject.asObservable();
       });
-      spyOn(XosModelDiscovererMock, 'get').and.returnValue({tableCfg: resTableCfg});
+      spyOn(XosModeldefsCacheMock, 'get').and.returnValue({tableCfg: resTableCfg});
 
       service.getModels(relation, 5)
         .then((res: IXosCrudRelationTableTabData) => {
@@ -196,7 +198,7 @@
         const subject = new BehaviorSubject(resModels);
         return subject.asObservable();
       });
-      spyOn(XosModelDiscovererMock, 'get').and.returnValue({tableCfg: resTableCfg});
+      spyOn(XosModeldefsCacheMock, 'get').and.returnValue({tableCfg: resTableCfg});
 
       service.getModels(relation, 5)
         .then((res: IXosCrudRelationTableTabData) => {
diff --git a/src/app/views/crud/crud.relations.service.ts b/src/app/views/crud/crud.relations.service.ts
index 092dda8..9dd3656 100644
--- a/src/app/views/crud/crud.relations.service.ts
+++ b/src/app/views/crud/crud.relations.service.ts
@@ -18,12 +18,12 @@
 
 import {IXosModelRelation} from './crud';
 import {IXosModelStoreService} from '../../datasources/stores/model.store';
-import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
 import * as _ from 'lodash';
 import {IXosFormCfg} from '../../core/form/form';
 import {IXosTableCfg} from '../../core/table/table';
 import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers';
 import {Subscription} from 'rxjs';
+import {IXosModeldefsCache} from '../../datasources/helpers/modeldefs.service';
 
 interface IXosCrudRelationBaseTabData {
   model: any;
@@ -51,16 +51,16 @@
     '$log',
     '$q',
     'XosModelStore',
-    'XosModelDiscoverer',
-    'ConfigHelpers'
+    'ConfigHelpers',
+    'XosModeldefsCache'
   ];
 
   constructor (
     private $log: ng.ILogService,
     private $q: ng.IQService,
     private XosModelStore: IXosModelStoreService,
-    private XosModelDiscovererService: IXosModelDiscovererService,
-    private ConfigHelpers: IXosConfigHelpersService
+    private ConfigHelpers: IXosConfigHelpersService,
+    private XosModeldefsCache: IXosModeldefsCache
   ) {}
 
   public getModel (r: IXosModelRelation, id: string | number): Promise<IXosCrudRelationFormTabData> {
@@ -72,7 +72,7 @@
 
           const data: IXosCrudRelationFormTabData = {
             model: item,
-            formConfig: this.XosModelDiscovererService.get(r.model).formCfg,
+            formConfig: this.XosModeldefsCache.get(r.model).formCfg,
             class: angular ? 'full' : 'empty'
           };
 
@@ -96,7 +96,7 @@
           match[`${r.on_field.toLowerCase()}_id`] = source_id;
           const filtered = _.filter(items, match);
           // removing search bar from table
-          const tableCfg = this.XosModelDiscovererService.get(r.model).tableCfg;
+          const tableCfg = this.XosModeldefsCache.get(r.model).tableCfg;
           tableCfg.filter = null;
 
           const data: IXosCrudRelationTableTabData = {
diff --git a/src/app/views/crud/crud.ts b/src/app/views/crud/crud.ts
index b4434c8..0fe9407 100644
--- a/src/app/views/crud/crud.ts
+++ b/src/app/views/crud/crud.ts
@@ -28,6 +28,7 @@
 import {IXosDebugService, IXosDebugStatus} from '../../core/debug/debug.service';
 import {IXosKeyboardShortcutService} from '../../core/services/keyboard-shortcut';
 import {Subscription} from 'rxjs';
+import {IXosModeldefsCache} from '../../datasources/helpers/modeldefs.service';
 
 export interface IXosModelRelation {
   model: string;
@@ -48,7 +49,8 @@
     'XosModelDiscoverer',
     'XosCrudRelation',
     'XosDebug',
-    'XosKeyboardShortcut'
+    'XosKeyboardShortcut',
+    'XosModeldefsCache'
   ];
 
   // bindings
@@ -88,12 +90,13 @@
     private XosModelDiscovererService: IXosModelDiscovererService,
     private XosCrudRelation: IXosCrudRelationService,
     private XosDebug: IXosDebugService,
-    private XosKeyboardShortcut: IXosKeyboardShortcutService
+    private XosKeyboardShortcut: IXosKeyboardShortcutService,
+    private XosModeldefsCache: IXosModeldefsCache
   ) {
     this.$log.info('[XosCrud] Setup', $state.current.data);
 
     this.data = this.$state.current.data;
-    this.modelDef = this.XosModelDiscovererService.get(this.data.model);
+    this.modelDef = this.XosModeldefsCache.get(this.data.model);
     this.modelName = this.modelDef.verbose_name ? this.modelDef.verbose_name : this.modelDef.name;
     this.pluralTitle = this.ConfigHelpers.pluralize(this.modelName);
     this.singularTitle = this.ConfigHelpers.pluralize(this.modelName, 1);
@@ -106,6 +109,18 @@
     this.tableCfg = this.modelDef.tableCfg;
     this.formCfg = this.modelDef.formCfg;
 
+    // attach a redirect to the $save method
+    const originalSave = this.formCfg.actions[0].cb;
+    this.formCfg.actions[0].cb = (item, form: angular.IFormController) => {
+      originalSave(item, form)
+        .then(res => {
+          this.$state.go(this.$state.current, {id: res.id});
+        })
+        .catch(err => {
+          this.$log.error(`[XosCrud] Error while saving:`, item, err);
+        });
+    };
+
     this.debugTab = this.XosDebug.status.modelsTab;
     this.$scope.$on('xos.debug.status', (e, status: IXosDebugStatus) => {
       this.debugTab = status.modelsTab;
@@ -119,7 +134,7 @@
       // if it is the create page
       if ($stateParams['id'] === 'add') {
         // generate a resource for an empty model
-        const endpoint = this.XosModelDiscovererService.getApiUrlFromModel(this.XosModelDiscovererService.get(this.data.model));
+        const endpoint = this.XosModelDiscovererService.getApiUrlFromModel(this.XosModeldefsCache.get(this.data.model));
         const resource = this.ModelRest.getResource(endpoint);
         this.model = new resource({});
       }