Permalink
Browse files

Passing reference to schema directly to Field.addToSchema

Helps separate concerns between the field types and the list they
belong to
  • Loading branch information...
JedWatson committed Aug 4, 2016
1 parent 3498ab6 commit 4da9ca7629377a9c56084f530573166c79d3a07f
View
@@ -79,7 +79,7 @@ function Field (list, path, options) {
}
// Add the field to the schema
this.addToSchema();
this.addToSchema(this.list.schema);
// Add pre-save handler to the list if this field watches others
if (this.options.watch) {
@@ -237,9 +237,9 @@ definePrototypeGetters(Field, {
* Default method to register the field on the List's Mongoose Schema.
* Overridden by some fieldType Classes
*/
Field.prototype.addToSchema = function () {
Field.prototype.addToSchema = function (schema) {
var ops = (this._nativeType) ? _.defaults({ type: this._nativeType }, this.options) : this.options;
this.list.schema.path(this.path, ops);
schema.path(this.path, ops);
this.bindUnderscoreMethods();
};
@@ -80,12 +80,11 @@ Object.defineProperty(azurefile.prototype, 'azurefileconfig', {
/**
* Registers the field on the List's Mongoose Schema.
*/
azurefile.prototype.addToSchema = function () {
azurefile.prototype.addToSchema = function (schema) {
var azure = require('azure');
var field = this;
var schema = this.list.schema;
var paths = this.paths = {
// fields
@@ -70,12 +70,11 @@ cloudinaryimage.prototype.getFolder = function () {
/**
* Registers the field on the List's Mongoose Schema.
*/
cloudinaryimage.prototype.addToSchema = function () {
cloudinaryimage.prototype.addToSchema = function (schema) {
var cloudinary = require('cloudinary');
var field = this;
var schema = this.list.schema;
var paths = this.paths = {
// cloudinary fields
@@ -70,12 +70,11 @@ cloudinaryimages.prototype.getFolder = function () {
/**
* Registers the field on the List's Mongoose Schema.
*/
cloudinaryimages.prototype.addToSchema = function () {
cloudinaryimages.prototype.addToSchema = function (schema) {
var cloudinary = require('cloudinary');
var mongoose = keystone.mongoose;
var field = this;
var schema = this.list.schema;
this.paths = {
// virtuals
@@ -53,10 +53,9 @@ util.inherits(embedly, FieldType);
*
* @api public
*/
embedly.prototype.addToSchema = function () {
embedly.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
this.paths = {
exists: this._path.append('.exists'),
@@ -24,10 +24,9 @@ util.inherits(file, FieldType);
/**
* Registers the field on the List's Mongoose Schema.
*/
file.prototype.addToSchema = function () {
file.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
this.paths = {};
// add field paths from the storage schema
@@ -24,8 +24,8 @@ util.inherits(geopoint, FieldType);
* Registers the field on the List's Mongoose Schema.
* Adds a 2dsphere indexed lat/lng pair
*/
geopoint.prototype.addToSchema = function () {
this.list.schema.path(this.path, _.defaults({ type: [Number], index: '2dsphere' }, this.options));
geopoint.prototype.addToSchema = function (schema) {
schema.path(this.path, _.defaults({ type: [Number], index: '2dsphere' }, this.options));
this.bindUnderscoreMethods();
};
@@ -63,10 +63,9 @@ util.inherits(localfile, FieldType);
*
* @api public
*/
localfile.prototype.addToSchema = function () {
localfile.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
var paths = this.paths = {
// fields
@@ -71,10 +71,9 @@ util.inherits(localfiles, FieldType);
/**
* Registers the field on the List's Mongoose Schema.
*/
localfiles.prototype.addToSchema = function () {
localfiles.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
var mongoose = keystone.mongoose;
var paths = this.paths = {
@@ -48,10 +48,9 @@ util.inherits(location, FieldType);
/**
* Registers the field on the List's Mongoose Schema.
*/
location.prototype.addToSchema = function () {
location.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
var options = this.options;
var paths = this.paths = {
@@ -34,9 +34,7 @@ markdown.prototype.validateRequiredInput = TextType.prototype.validateRequiredIn
* Adds String properties for .md and .html markdown, and a setter for .md
* that generates html when it is updated.
*/
markdown.prototype.addToSchema = function () {
var schema = this.list.schema;
markdown.prototype.addToSchema = function (schema) {
var paths = this.paths = {
md: this._path.append('.md'),
@@ -26,8 +26,7 @@ util.inherits(name, FieldType);
*
* @api public
*/
name.prototype.addToSchema = function () {
var schema = this.list.schema;
name.prototype.addToSchema = function (schema) {
var paths = this.paths = {
first: this._path.append('.first'),
last: this._path.append('.last'),
@@ -54,9 +54,8 @@ util.inherits(password, FieldType);
*
* @api public
*/
password.prototype.addToSchema = function () {
password.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
var needs_hashing = '__' + field.path + '_needs_hashing';
this.paths = {
@@ -67,9 +67,8 @@ relationship.prototype.getExpandedData = function (item) {
/**
* Registers the field on the List's Mongoose Schema.
*/
relationship.prototype.addToSchema = function () {
relationship.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
var def = {
type: this._nativeType,
ref: this.options.ref,
@@ -73,11 +73,10 @@ Object.defineProperty(s3file.prototype, 's3config', {
/**
* Registers the field on the List's Mongoose Schema.
*/
s3file.prototype.addToSchema = function () {
s3file.prototype.addToSchema = function (schema) {
var knox = require('knox');
var field = this;
var schema = this.list.schema;
var paths = this.paths = {
// fields
@@ -52,9 +52,8 @@ util.inherits(select, FieldType);
* and statics to the Schema for converting a value to a label,
* and retrieving all of the defined options.
*/
select.prototype.addToSchema = function () {
select.prototype.addToSchema = function (schema) {
var field = this;
var schema = this.list.schema;
this.paths = {
data: this.options.dataPath || this._path.append('Data'),
label: this.options.labelPath || this._path.append('Label'),

0 comments on commit 4da9ca7

Please sign in to comment.