<commit_before>var linz = require('../../'),
    formist = require('formist'),
	async = require('async'),
    theme = require('./theme'),
    utils = require('../utils');

var padWithZero = function (val) {
	return String(val).length < 2 ? '0' + String(val) : val;
};

var addToFormOrFieldset = function (form, fieldsets, fieldsetKey, element) {

    var container = form;

    if (fieldsetKey !== undefined) {

        // create the fieldset if it currently doesn't exist
        if (fieldsets[fieldsetKey] === undefined) {
            fieldsets[fieldsetKey] = form.add(new formist.Fieldset({
                legend: fieldsetKey
            }));
        }

        container = fieldsets[fieldsetKey];

    }

    container.add(element);

};

var generateFormFromModel = exports.generateFormFromModel = function (m, r, formType, cb) {

	var formFields = {},
        form = new formist.Form({
            renderTag: false,
            theme: theme,
            attributes: {
                'class': 'form-horizontal'
            }
        }),
        fieldsets = {};

    // defaults to 'create'
    formType = formType || 'create';

    async.eachSeries(Object.keys(m.form), function (fieldName, fn) {

        var field = utils.clone(m.form[fieldName]); // we're going to make changes to this object, clone don't reference the original value
        var fieldOptions = field.options;
        var formField;
        var choices = {};
        var defaultValue = null;

        // merge form.edit or form.create onto form
        utils.merge(field, field[formType] || {});
        delete field.edit;
        delete field.create;

        // determine if the field has enumValues
        if (Array.isArray(field.list) && field.list.length) {

            // add the options to the choices object to be passed to the select box
            if (field.type !== 'array') field.type = 'enum';

            // list can contain an aray of strings or objects
            field.list.forEach(function (val) {
                if(typeof val === 'object') choices[val.value] = val.name;
                else choices[val] = val;
            });

        }

        if (field.default !== undefined && field.type !== 'array') {

            // work out the default value and use it
            if (typeof field.default !== 'function') {
                defaultValue = field.default;
            } else if (typeof field.default === 'function') {
                defaultValue = field.default();
            }

        } else if (field.default === undefined && field.type === 'datetime') {

            field.default = function () {

                var d = new Date();
                return d.getFullYear() + '-' +
                        padWithZero(d.getMonth()+1) + '-' +
                        padWithZero(d.getDate()) + 'T' +
                        padWithZero(d.getHours()) + ':' +
                        padWithZero(d.getMinutes()) + ':' +
                        '00.000';
            };

            defaultValue = field.default();

        }

        async.series([
            function (callback) {

                // if the field has a reference, we need to grab the values for that type and add them in
                if (m.schema.tree[fieldName].ref) {

                    linz.mongoose.models[m.schema.tree[fieldName].ref].find({}, function (err, docs) {

                        field.type = 'enum';
                        choices = {};

                        docs.forEach(function (doc) {
                            choices[doc._id.toString()] = doc.title || doc.label || doc.name || doc.toString();
                        });

                        callback(null);

                    });

                } else {

                    callback(null);

                }

            },
            function (callback) {

                if (field.visible === false) {
                    return callback(null);
                }

                var fieldValue;
<commit_msg>Ref property values now appear in the select lists.<commit_after>var linz = require('../../'),
    formist = require('formist'),
	async = require('async'),
    theme = require('./theme'),
    utils = require('../utils');

var padWithZero = function (val) {
	return String(val).length < 2 ? '0' + String(val) : val;
};

var addToFormOrFieldset = function (form, fieldsets, fieldsetKey, element) {

    var container = form;

    if (fieldsetKey !== undefined) {

        // create the fieldset if it currently doesn't exist
        if (fieldsets[fieldsetKey] === undefined) {
            fieldsets[fieldsetKey] = form.add(new formist.Fieldset({
                legend: fieldsetKey
            }));
        }

        container = fieldsets[fieldsetKey];

    }

    container.add(element);

};

var generateFormFromModel = exports.generateFormFromModel = function (m, r, formType, cb) {

	var formFields = {},
        form = new formist.Form({
            renderTag: false,
            theme: theme,
            attributes: {
                'class': 'form-horizontal'
            }
        }),
        fieldsets = {};

    // defaults to 'create'
    formType = formType || 'create';

    async.eachSeries(Object.keys(m.form), function (fieldName, fn) {

        var field = utils.clone(m.form[fieldName]); // we're going to make changes to this object, clone don't reference the original value
        var fieldOptions = field.options;
        var formField;
        var choices = {};
        var defaultValue = null;

        // merge form.edit or form.create onto form
        utils.merge(field, field[formType] || {});
        delete field.edit;
        delete field.create;

        // determine if the field has enumValues
        if (Array.isArray(field.list) && field.list.length) {

            // add the options to the choices object to be passed to the select box
            if (field.type !== 'array') field.type = 'enum';

            // list can contain an aray of strings or objects
            field.list.forEach(function (val) {
                if(typeof val === 'object') choices[val.value] = val.name;
                else choices[val] = val;
            });

        }

        if (field.default !== undefined && field.type !== 'array') {

            // work out the default value and use it
            if (typeof field.default !== 'function') {
                defaultValue = field.default;
            } else if (typeof field.default === 'function') {
                defaultValue = field.default();
            }

        } else if (field.default === undefined && field.type === 'datetime') {

            field.default = function () {

                var d = new Date();
                return d.getFullYear() + '-' +
                        padWithZero(d.getMonth()+1) + '-' +
                        padWithZero(d.getDate()) + 'T' +
                        padWithZero(d.getHours()) + ':' +
                        padWithZero(d.getMinutes()) + ':' +
                        '00.000';
            };

            defaultValue = field.default();

        }

        async.series([
            function (callback) {

                // if the field has a reference, we need to grab the values for that type and add them in
                if (m.schema.tree[fieldName].ref) {

                    linz.mongoose.models[m.schema.tree[fieldName].ref].find({}, function (err, docs) {

                        field.type = 'enum';
                        choices = [];

                        docs.forEach(function (doc) {
                            choices.push({
                                value: doc._id.toString(),
                                label: doc.title || doc.label || doc.name || doc.toString()
                            });
                        });

                        field.list = choices;

                        callback(null);

                    });

                } else {

                    callback(null);

                }

            },
            function (callback) {

                if (field.visible === false) {
                    return callback(null);
                }

                var fieldValue;

{'fraction_non_alphanumeric': 0.1236697247706422, 'fraction_numerical': 0.0019571865443425078, 'mean_word_length': 1.4633925881289545, 'pattern_counts': {'":': 0, '<': 5, '<?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': '730522', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2030, 'n_words': 549}