<commit_before>var keystone = require('keystone'),
    async = require('async'),
    atriumscreen = require('rfr')('./lib');


/**
 * The decision stack is basically a series of middleware that will set a whole bunch of information that will ultimately end up at the layout
 * @memberof atriumscreen.Decider
 * @alias DecisionStack
 */
var DecisionStack = module.exports = new keystone.List('DecisionStack');

DecisionStack.add({

    name: {
        type: String,
        required: true,
        initial: true
    },

    deciders: {
        type: keystone.Field.Types.Relationship,
        ref: 'Decider',
        many: true
    }
});


/**
 * Executes the decision stack (acts as an express valid middleware)
 */
DecisionStack.schema.methods.execute = function(req, res, next) {
    var self = this;
    var loop = function() {
        //Loop asyncronously over the controllers

        async.each(self.deciders, function(decider, callback) {
            //Let the middleware do its work
            console.log(decider);
            req.currentSettings = decider.settings;
            decider.controllerObject.middleware(req, res, callback);
        }, function(err) {
            if (err) {
                next(err);
                return;
            }

            //It's out of our hands
            if (res.layout) {
                atriumscreen.Layout.model.findById(res.layout, function(err, layout) {
                    if ((err) || (!layout)) {
                        next();
                        return;
                    }
                    layout.controllerFunction(req, res, next);
                });
            } else {
                next();
            }
        });
    };
    if (this.populated('deciders')) {
        loop();
    } else {
        this.populate('deciders', function(err) {
            if (err) {
                console.error(err);
                next(new Error('Could not get decider'));
                return;
            }
            loop();
        });
    }
};

DecisionStack.register();<commit_msg>Fix auto population in decision stacks<commit_after>var keystone = require('keystone'),
    async = require('async'),
    atriumscreen = require('rfr')('./lib'),
    _ = require('lodash');


/**
 * The decision stack is basically a series of middleware that will set a whole bunch of information that will ultimately end up at the layout
 * @memberof atriumscreen.Decider
 * @alias DecisionStack
 */
var DecisionStack = module.exports = new keystone.List('DecisionStack');

DecisionStack.add({

    name: {
        type: String,
        required: true,
        initial: true
    },

    deciders: {
        type: keystone.Field.Types.Relationship,
        ref: 'Decider',
        many: true
    }
});


/**
 * Executes the decision stack (acts as an express valid middleware)
 */
DecisionStack.schema.methods.execute = function(req, res, next) {
    var self = this;
    console.log(self.get('deciders.0'));
    var loop = function() {
        //Loop asyncronously over the controllers

        async.each(self.deciders, function(decider, callback) {
            //Let the middleware do its work
            console.log(decider);
            req.currentSettings = decider.settings;
            decider.controllerObject.middleware(req, res, callback);
        }, function(err) {
            if (err) {
                next(err);
                return;
            }

            //It's out of our hands
            if (res.layout) {
                if (res.layout.constructor.name === 'model') {
                    res.layout.controllerFunction(req, res, next);
                } else {
                    atriumscreen.Layout.model.findById(res.layout, function(err, layout) {
                        if ((err) || (!layout)) {
                            next();
                            return;
                        }
                        layout.controllerFunction(req, res, next);
                    });
                }
            } else {
                next();
            }
        });
    };

    if (_.filter(this.deciders, function(d, i) { return d.constructor.name !== 'model' }).length === 0) {
        loop();
    } else {
        this.populate('deciders', function(err) {
            if (err) {
                console.error(err);
                next(new Error('Could not get decider'));
                return;
            }
            loop();
        });
    }
};

DecisionStack.register();
{'fraction_non_alphanumeric': 0.11894666369114038, 'fraction_numerical': 0.0004463289444320464, 'mean_word_length': 1.6073298429319371, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1837610', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1154, 'n_words': 296}