Skip to content

Commit

Permalink
Merge pull request #659 from marmelab/reference_map
Browse files Browse the repository at this point in the history
[RFR] Fix inconsistency in remote reference map
  • Loading branch information
fzaninotto committed Sep 3, 2015
2 parents cc6a89d + 06ae688 commit ea57d23
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 55 deletions.
5 changes: 1 addition & 4 deletions examples/blog/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
.map(truncate),
nga.field('post_id', 'reference')
.label('Post')
.map(truncate)
.targetEntity(post)
.targetField(nga.field('title').map(truncate))
])
Expand Down Expand Up @@ -207,9 +206,8 @@
nga.field('body', 'wysiwyg'),
nga.field('post_id', 'reference')
.label('Post')
.map(truncate)
.targetEntity(post)
.targetField(nga.field('title'))
.targetField(nga.field('title').map(truncate))
.sortField('title')
.sortDir('ASC')
.validation({ required: true })
Expand Down Expand Up @@ -354,7 +352,6 @@
.isDetailLink(true),
nga.field('post_id', 'reference')
.label('Post')
.map(truncate)
.targetEntity(post)
.targetField(nga.field('title').map(truncate))
])
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require('es6-promise').polyfill(); // for IE
require('./ng-admin/Main/MainModule');
require('./ng-admin/Crud/CrudModule');

var Factory = require('admin-config/lib/Factory');
import Factory from 'admin-config/lib/Factory';

var factory = angular.module('AdminDescriptionModule', []);
factory.constant('AdminDescription', new Factory());
Expand Down
20 changes: 14 additions & 6 deletions src/javascripts/ng-admin/Crud/repository/ReferenceRefresher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Entry from 'admin-config/lib/Entry'

class ReferenceRefresher {
constructor(ReadQueries) {
this.ReadQueries = ReadQueries;
Expand Down Expand Up @@ -38,13 +40,19 @@ class ReferenceRefresher {
}

_transformRecords(field, records) {
var valueFieldName = field.targetEntity().identifier().name();
var labelFieldName = field.targetField().name();

return records.map(function(r) {
var targetEntity = field.targetEntity();
var targetField = field.targetField();
var valueFieldName = targetEntity.identifier().name();
var labelFieldName = targetField.name();
return Entry.createArrayFromRest(
records,
[targetField],
targetEntity.name(),
valueFieldName
).map(function(r) {
return {
value: r[valueFieldName],
label: field.getMappedValue(r[labelFieldName], r)
value: r.values[valueFieldName],
label: r.values[labelFieldName]
};
});
}
Expand Down
64 changes: 24 additions & 40 deletions src/javascripts/ng-admin/Crud/routing.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var listLayoutTemplate = require('./list/listLayout.html'),
listTemplate = require ('./list/list.html'),
showTemplate = require('./show/show.html'),
createTemplate = require('./form/create.html'),
editTemplate = require('./form/edit.html'),
deleteTemplate = require('./delete/delete.html'),
batchDeleteTemplate = require('./delete/batchDelete.html');
import Entry from 'admin-config/lib/Entry';
import DataStore from 'admin-config/lib/DataStore/DataStore';
import listLayoutTemplate from './list/listLayout.html';
import listTemplate from './list/list.html';
import showTemplate from './show/show.html';
import createTemplate from './form/create.html';
import editTemplate from './form/edit.html';
import deleteTemplate from './delete/delete.html';
import batchDeleteTemplate from './delete/batchDelete.html';

function templateProvider(viewName, defaultView) {
return ['$stateParams', 'NgAdminConfiguration', function ($stateParams, Configuration) {
Expand Down Expand Up @@ -35,19 +37,6 @@ function viewProvider(viewName) {
}];
}

function dataStoreProvider() {
return ['AdminDescription', function (AdminDescription) {
return AdminDescription.getDataStore();
}];
}


function entryConstructorProvider() {
return ['AdminDescription', function (AdminDescription) {
return AdminDescription.getEntryConstructor();
}];
}

function routing($stateProvider) {

$stateProvider
Expand All @@ -62,13 +51,12 @@ function routing($stateProvider) {
controllerAs: 'llCtrl',
templateProvider: templateProvider('ListView', listLayoutTemplate),
resolve: {
dataStore: dataStoreProvider(),
Entry: entryConstructorProvider(),
dataStore: () => new DataStore(),
view: viewProvider('ListView'),
filterData: ['ReadQueries', 'view', function (ReadQueries, view) {
return ReadQueries.getAllReferencedData(view.getFilterReferences(false));
}],
filterEntries: ['dataStore', 'view', 'filterData', 'Entry', function (dataStore, view, filterData, Entry) {
filterEntries: ['dataStore', 'view', 'filterData', function (dataStore, view, filterData) {
var filters = view.getFilterReferences(false);
var filterEntries;

Expand Down Expand Up @@ -105,8 +93,7 @@ function routing($stateProvider) {
controllerAs: 'listController',
template: listTemplate,
resolve: {
dataStore: dataStoreProvider(),
Entry: entryConstructorProvider(),
dataStore: () => new DataStore(),
view: viewProvider('ListView'),
response: ['$stateParams', 'ReadQueries', 'view', function ($stateParams, ReadQueries, view) {
var page = $stateParams.page,
Expand All @@ -125,7 +112,7 @@ function routing($stateProvider) {
optimizedReferencedData: ['ReadQueries', 'view', 'response', function (ReadQueries, view, response) {
return ReadQueries.getOptimizedReferencedData(view.getOptimizedReferences(), response.data);
}],
referencedEntries: ['dataStore', 'Entry', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, Entry, view, nonOptimizedReferencedData, optimizedReferencedData) {
referencedEntries: ['dataStore', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, view, nonOptimizedReferencedData, optimizedReferencedData) {
var references = view.getReferences(),
referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData),
referencedEntries;
Expand Down Expand Up @@ -177,8 +164,7 @@ function routing($stateProvider) {
sortDir: null
},
resolve: {
dataStore: dataStoreProvider(),
Entry: entryConstructorProvider(),
dataStore: () => new DataStore(),
view: viewProvider('ShowView'),
rawEntry: ['$stateParams', 'ReadQueries', 'view', function ($stateParams, ReadQueries, view) {
return ReadQueries.getOne(view.getEntity(), view.type, $stateParams.id, view.identifier(), view.getUrl());
Expand All @@ -192,7 +178,7 @@ function routing($stateProvider) {
optimizedReferencedData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) {
return ReadQueries.getOptimizedReferencedData(view.getOptimizedReferences(), [entry.values]);
}],
referencedEntries: ['dataStore', 'Entry', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, Entry, view, nonOptimizedReferencedData, optimizedReferencedData) {
referencedEntries: ['dataStore', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, view, nonOptimizedReferencedData, optimizedReferencedData) {
var references = view.getReferences(),
referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData),
referencedEntries;
Expand All @@ -216,7 +202,7 @@ function routing($stateProvider) {

return ReadQueries.getReferencedListData(referencedLists, sortField, sortDir, entry.identifierValue);
}],
referencedListEntries: ['dataStore', 'Entry', 'view', 'referencedListData', function (dataStore, Entry, view, referencedListData) {
referencedListEntries: ['dataStore', 'view', 'referencedListData', function (dataStore, view, referencedListData) {
var referencedLists = view.getReferencedLists();
var referencedList;
var referencedListEntries;
Expand Down Expand Up @@ -262,10 +248,9 @@ function routing($stateProvider) {
sortDir: null
},
resolve: {
dataStore: dataStoreProvider(),
dataStore: () => new DataStore(),
view: viewProvider('CreateView'),
Entry: entryConstructorProvider(),
entry: ['dataStore', 'Entry', 'view', function (dataStore, Entry, view) {
entry: ['dataStore', 'view', function (dataStore, view) {
var entry = Entry.createForFields(view.getFields(), view.entity.name());
dataStore.addEntry(view.getEntity().uniqueId, entry);

Expand All @@ -274,7 +259,7 @@ function routing($stateProvider) {
choiceData: ['ReadQueries', 'view', function (ReadQueries, view) {
return ReadQueries.getAllReferencedData(view.getReferences(false));
}],
choiceEntries: ['dataStore', 'Entry', 'view', 'choiceData', function (dataStore, Entry, view, filterData) {
choiceEntries: ['dataStore', 'view', 'choiceData', function (dataStore, view, filterData) {
var choices = view.getReferences(false);
var choiceEntries;

Expand Down Expand Up @@ -313,8 +298,7 @@ function routing($stateProvider) {
sortDir: null
},
resolve: {
dataStore: dataStoreProvider(),
Entry: entryConstructorProvider(),
dataStore: () => new DataStore(),
view: viewProvider('EditView'),
rawEntry: ['$stateParams', 'ReadQueries', 'view', function ($stateParams, ReadQueries, view) {
return ReadQueries.getOne(view.getEntity(), view.type, $stateParams.id, view.identifier(), view.getUrl());
Expand All @@ -328,7 +312,7 @@ function routing($stateProvider) {
optimizedReferencedData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) {
return ReadQueries.getOptimizedReferencedData(view.getOptimizedReferences(), [entry.values]);
}],
referencedEntries: ['dataStore', 'Entry', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, Entry, view, nonOptimizedReferencedData, optimizedReferencedData) {
referencedEntries: ['dataStore', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, view, nonOptimizedReferencedData, optimizedReferencedData) {
var references = view.getReferences(),
referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData),
referencedEntries;
Expand All @@ -352,7 +336,7 @@ function routing($stateProvider) {

return ReadQueries.getReferencedListData(referencedLists, sortField, sortDir, entry.identifierValue);
}],
referencedListEntries: ['dataStore', 'Entry', 'view', 'referencedListData', function (dataStore, Entry, view, referencedListData) {
referencedListEntries: ['dataStore', 'view', 'referencedListData', function (dataStore, view, referencedListData) {
var referencedLists = view.getReferencedLists();
var referencedList;
var referencedListEntries;
Expand Down Expand Up @@ -383,7 +367,7 @@ function routing($stateProvider) {
choiceData: ['ReadQueries', 'view', function (ReadQueries, view) {
return ReadQueries.getAllReferencedData(view.getReferences(false));
}],
choiceEntries: ['dataStore', 'Entry', 'view', 'choiceData', function (dataStore, Entry, view, filterData) {
choiceEntries: ['dataStore', 'view', 'choiceData', function (dataStore, view, filterData) {
var choices = view.getReferences(false);
var choiceEntries;

Expand Down Expand Up @@ -420,7 +404,7 @@ function routing($stateProvider) {
sortDir: null
},
resolve: {
dataStore: dataStoreProvider(),
dataStore: () => new DataStore(),
view: viewProvider('DeleteView'),
params: ['$stateParams', function ($stateParams) {
return $stateParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('ReferenceRefresher', function() {
var fakeEntity, fakeField;
beforeEach(function() {
fakeEntity = {
name: () => 'fooEntity',
identifier: () => {
return {
name: () => 'id'
Expand All @@ -19,9 +20,12 @@ describe('ReferenceRefresher', function() {
name: () => 'post',
targetEntity: () => fakeEntity,
targetField: () => {
return { name: () => 'title' }
return {
name: () => 'title',
flattenable: () => false,
getMappedValue: v => v
}
},
getMappedValue: v => v,
type: () => 'reference'
};
});
Expand Down Expand Up @@ -108,7 +112,18 @@ describe('ReferenceRefresher', function() {
]
}));

fakeField.getMappedValue = (v, e) => `${e.title} (#${e.id})`;
fakeField = {
name: () => 'post',
targetEntity: () => fakeEntity,
targetField: () => {
return {
name: () => 'title',
flattenable: () => false,
getMappedValue: (v, e) => `${e.title} (#${e.id})`
}
},
type: () => 'reference'
};

var refresher = new ReferenceRefresher(readQueries);
refresher.refresh(fakeField, null).then(function(results) {
Expand Down Expand Up @@ -146,7 +161,18 @@ describe('ReferenceRefresher', function() {
});

it('should return mapped values for labels', function(done) {
fakeField.getMappedValue = (v, e) => `${e.title} (#${e.id})`;
fakeField = {
name: () => 'post',
targetEntity: () => fakeEntity,
targetField: () => {
return {
name: () => 'title',
flattenable: () => false,
getMappedValue: (v, e) => `${e.title} (#${e.id})`
}
},
type: () => 'reference'
};
refresher.getInitialChoices(fakeField, [1, 2]).then(function(results) {
expect(results).toEqual([
{ value: 1, label: 'Discover some awesome stuff (#1)' },
Expand Down

0 comments on commit ea57d23

Please sign in to comment.