Skip to content

Commit

Permalink
Extracting List.prototype.add into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
JedWatson committed Aug 6, 2015
1 parent ebe2374 commit abde339
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 76 deletions.
77 changes: 1 addition & 76 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function List(key, options) {
// Add prototype methods
List.prototype.set = require('./list/set');
List.prototype.get = List.prototype.set;
List.prototype.add = require('./list/add');
List.prototype.relationship = require('./list/relationship');
List.prototype.underscoreMethod = require('./list/underscoreMethod');
List.prototype.register = require('./list/register');
Expand Down Expand Up @@ -151,82 +152,6 @@ List.prototype.automap = function(field) {
};


/**
* Adds one or more fields to the List
* Based on Mongoose's Schema.add
*/
List.prototype.add = function() {
var add = function(obj, prefix) {
prefix = prefix || '';
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (!obj[key]) {
throw new Error(
'Invalid value for schema path `' + prefix + key + '` in `' + this.key + '`.\n' +
'Did you misspell the field type?\n'
);
}
if (utils.isObject(obj[key]) && (!obj[key].constructor || 'Object' === obj[key].constructor.name) && (!obj[key].type || obj[key].type.type)) {
if (Object.keys(obj[key]).length) {
// nested object, e.g. { last: { name: String }}
// matches logic in mongoose/Schema:add
this.schema.nested[this.path] = true;
add(obj[key], prefix + key + '.');
} else {
addField(prefix + key, obj[key]); // mixed type field
}
} else {
addField(prefix + key, obj[key]);
}
}
}.bind(this);

var addField = function(path, options) {
if (this.isReserved(path)) {
throw new Error('Path ' + path + ' on list ' + this.key + ' is a reserved path');
}
this.uiElements.push({
type: 'field',
field: this.field(path, options)
});
}.bind(this);

_.each(arguments, function(def) {
this.schemaFields.push(def);
if ('string' === typeof def) {
if (def === '>>>') {
this.uiElements.push({
type: 'indent'
});
} else if (def === '<<<') {
this.uiElements.push({
type: 'outdent'
});
} else {
this.uiElements.push({
type: 'heading',
heading: def,
options: {}
});
}
} else {
if (def.heading && 'string' === typeof def.heading) {
this.uiElements.push({
type: 'heading',
heading: def.heading,
options: def
});
} else {
add(def);
}
}
}, this);

return this;
};


/**
* Creates a new field at the specified path, with the provided options.
* If no options are provides, returns the field at the specified path.
Expand Down
79 changes: 79 additions & 0 deletions lib/list/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var _ = require('underscore');
var utils = require('keystone-utils');

/**
* Adds one or more fields to the List
* Based on Mongoose's Schema.add
*/
function add () {
var add = function(obj, prefix) {
prefix = prefix || '';
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (!obj[key]) {
throw new Error(
'Invalid value for schema path `' + prefix + key + '` in `' + this.key + '`.\n' +
'Did you misspell the field type?\n'
);
}
if (utils.isObject(obj[key]) && (!obj[key].constructor || 'Object' === obj[key].constructor.name) && (!obj[key].type || obj[key].type.type)) {
if (Object.keys(obj[key]).length) {
// nested object, e.g. { last: { name: String }}
// matches logic in mongoose/Schema:add
this.schema.nested[this.path] = true;
add(obj[key], prefix + key + '.');
} else {
addField(prefix + key, obj[key]); // mixed type field
}
} else {
addField(prefix + key, obj[key]);
}
}
}.bind(this);

var addField = function(path, options) {
if (this.isReserved(path)) {
throw new Error('Path ' + path + ' on list ' + this.key + ' is a reserved path');
}
this.uiElements.push({
type: 'field',
field: this.field(path, options)
});
}.bind(this);

_.each(arguments, function(def) {
this.schemaFields.push(def);
if ('string' === typeof def) {
if (def === '>>>') {
this.uiElements.push({
type: 'indent'
});
} else if (def === '<<<') {
this.uiElements.push({
type: 'outdent'
});
} else {
this.uiElements.push({
type: 'heading',
heading: def,
options: {}
});
}
} else {
if (def.heading && 'string' === typeof def.heading) {
this.uiElements.push({
type: 'heading',
heading: def.heading,
options: def
});
} else {
add(def);
}
}
}, this);

return this;
}

module.exports = add;

0 comments on commit abde339

Please sign in to comment.