Skip to content

Commit

Permalink
Add smartflields when getting a single entity
Browse files Browse the repository at this point in the history
  • Loading branch information
gcoombe committed Mar 22, 2017
1 parent 903fd07 commit c9b3afa
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions routes/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ module.exports = function (app, model, Implementation, integrator, opts) {
var modelName = Implementation.getModelName(model);
var schema = Schemas.schemas[Implementation.getModelName(model)];

function addSmartFields (record) {
return P.each(schema.fields, function (field) {
if (!record[field.field]) {
if (field.value) {
var value = field.value(record);
if (value && _.isFunction(value.then)) {
return value.then(function (value) {
record[field.field] = value;
});
} else {
record[field.field] = value;
}
} else if (_.isArray(field.type)) {
record[field.field] = [];
}
}
}).thenReturn(record);
}

this.list = function (req, res, next) {
return new Implementation.ResourcesGetter(model, opts, req.query)
.perform()
Expand All @@ -20,24 +39,7 @@ module.exports = function (app, model, Implementation, integrator, opts) {

// Inject smart fields.
return P
.map(records, function (record) {
return P.each(schema.fields, function (field) {
if (!record[field.field]) {
if (field.value) {
var value = field.value(record);
if (value && _.isFunction(value.then)) {
return value.then(function (value) {
record[field.field] = value;
});
} else {
record[field.field] = value;
}
} else if (_.isArray(field.type)) {
record[field.field] = [];
}
}
}).thenReturn(record);
})
.map(records, addSmartFields)
.then(function (records) {
return new ResourceSerializer(Implementation, model, records,
integrator, opts, { count: count }).perform();
Expand All @@ -52,6 +54,7 @@ module.exports = function (app, model, Implementation, integrator, opts) {
this.get = function (req, res, next) {
return new Implementation.ResourceGetter(model, req.params)
.perform()
.then(addSmartFields)
.then(function (record) {
return new ResourceSerializer(Implementation, model, record,
integrator, opts).perform();
Expand Down

0 comments on commit c9b3afa

Please sign in to comment.