From c311738dd1867e897c6ff299507555131d184227 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 6 Jan 2015 13:22:40 -0300 Subject: [PATCH] Move application field manipulations to the model --- lib/actions/app.coffee | 5 ++--- lib/resin/models/application.coffee | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index 6e90fdb1..5efc4e52 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -34,9 +34,8 @@ exports.list = permissions.user -> errors.handle(error) if error? log.out ui.widgets.table.horizontal applications, (application) -> - application.device_type = resin.models.device.getDisplayName(application.device_type) - application['Online Devices'] = _.where(application.device, is_online: 1).length - application['All Devices'] = application.device?.length or 0 + application.device_type = application.device_display_name + application.all_devices = application.devices_length return application , [ 'ID', 'Name', 'Device Type', 'Online Devices', 'All Devices' ] diff --git a/lib/resin/models/application.coffee b/lib/resin/models/application.coffee index 38fc2a56..32d7f4bf 100644 --- a/lib/resin/models/application.coffee +++ b/lib/resin/models/application.coffee @@ -1,5 +1,6 @@ _ = require('lodash-contrib') pine = require('./_pine') +deviceModel = require('./device') errors = require('../_errors/errors') server = require('../_server/server') settings = require('../settings') @@ -25,6 +26,14 @@ exports.getAll = (callback) -> if _.isEmpty(applications) return callback(new errors.NotAny('applications')) + # TODO: It might be worth to do all these handy + # manipulations server side directly. + applications = _.map applications, (application) -> + application.device_display_name = deviceModel.getDisplayName(application.device_type) + application.online_devices = _.where(application.device, is_online: 1).length + application.devices_length = application.device?.length or 0 + return application + return callback(null, applications) .catch (error) ->