Skip to content

Commit

Permalink
[-] Smart Fields - Do the Smart Fields values injection in the Serial…
Browse files Browse the repository at this point in the history
…izer to simplify Smart Relationships implementation (#120)
  • Loading branch information
arnaudbesnier committed Oct 30, 2017
1 parent 86ac593 commit 20f1ad5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Change Log

## [Unreleased]
### Changed
- Smart Fields - Do the Smart Fields values injection in the Serializer to simplify Smart Relationships implementation.

## RELEASE 1.4.0 - 2017-10-26
### Added
- Types Support - Support Point field type.

### Changed
- Smart Relationships - Add a warning if a Smart Collection does not define the "idField" attribute necessary for Smart Relationships.
- Smart Relationships - Add a warning if a Smart Collection does not define the "idField" attribute necessary for Smart Relationships.
- Smart Fields - Prevent the Smart Fields computation errors to generate a crash and handle it letting the value empty.

## RELEASE 1.3.6 - 2017-10-11
Expand Down
13 changes: 2 additions & 11 deletions routes/associations.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';
var _ = require('lodash');
var P = require('bluebird');
var SchemaUtil = require('../utils/schema');
var auth = require('../services/auth');
var path = require('../services/path');
var ResourceSerializer = require('../serializers/resource');
var Schemas = require('../generators/schemas');
var SmartFieldsValuesInjector = require('../services/smart-fields-values-injector');
var CSVExporter = require('../services/csv-exporter');

module.exports = function (app, model, Implementation, integrator, opts) {
Expand Down Expand Up @@ -43,15 +41,8 @@ module.exports = function (app, model, Implementation, integrator, opts) {
var count = results[0];
var records = results[1];

return P
.map(records, function (record) {
return new SmartFieldsValuesInjector(record, associationField)
.perform();
})
.then(function (records) {
return new ResourceSerializer(Implementation, associationModel,
records, integrator, opts, { count: count }).perform();
});
return new ResourceSerializer(Implementation, associationModel,
records, integrator, opts, { count: count }).perform();
})
.then(function (records) { response.send(records); })
.catch(next);
Expand Down
15 changes: 2 additions & 13 deletions routes/resources.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
var P = require('bluebird');
var auth = require('../services/auth');
var path = require('../services/path');
var ResourceSerializer = require('../serializers/resource');
var ResourceDeserializer = require('../deserializers/resource');
var SmartFieldsValuesInjector = require('../services/smart-fields-values-injector');
var CSVExporter = require('../services/csv-exporter');

module.exports = function (app, model, Implementation, integrator, opts) {
Expand All @@ -17,14 +15,8 @@ module.exports = function (app, model, Implementation, integrator, opts) {
var count = results[0];
var records = results[1];

return P
.map(records, function (record) {
return new SmartFieldsValuesInjector(record, modelName).perform();
})
.then(function (records) {
return new ResourceSerializer(Implementation, model, records,
integrator, opts, { count: count }).perform();
});
return new ResourceSerializer(Implementation, model, records,
integrator, opts, { count: count }).perform();
})
.then(function (records) {
response.send(records);
Expand All @@ -44,9 +36,6 @@ module.exports = function (app, model, Implementation, integrator, opts) {
this.get = function (request, response, next) {
return new Implementation.ResourceGetter(model, request.params)
.perform()
.then(function(records) {
return new SmartFieldsValuesInjector(records, modelName).perform();
})
.then(function (record) {
return new ResourceSerializer(Implementation, model, record,
integrator, opts).perform();
Expand Down
20 changes: 17 additions & 3 deletions serializers/resource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';
var _ = require('lodash');
var P = require('bluebird');
var moment = require('moment');
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
var SmartFieldsValuesInjector = require('../services/smart-fields-values-injector');
var Schemas = require('../generators/schemas');
var logger = require('../services/logger');

Expand All @@ -13,7 +15,8 @@ function toKebabCase(string) {

function ResourceSerializer(Implementation, model, records, integrator,
opts, meta) {
var schema = Schemas.schemas[Implementation.getModelName(model)];
var modelName = Implementation.getModelName(model);
var schema = Schemas.schemas[modelName];

var reservedWords = ['meta'];
var fieldNamesDateonly = [];
Expand Down Expand Up @@ -142,8 +145,19 @@ function ResourceSerializer(Implementation, model, records, integrator,
formatFields(records);
}

var typeName = toKebabCase(schema.name);
return new JSONAPISerializer(typeName, records, serializationOptions);
return new P(function (resolve) {
if (_.isArray(records)) {
resolve(P.map(records, function (record) {
return new SmartFieldsValuesInjector(record, modelName).perform();
}));
} else {
resolve(new SmartFieldsValuesInjector(records, modelName).perform());
}
})
.then(function (recordsWithSmartFields) {
var typeName = toKebabCase(schema.name);
return new JSONAPISerializer(typeName, recordsWithSmartFields, serializationOptions);
});
};
}

Expand Down

0 comments on commit 20f1ad5

Please sign in to comment.