all files / keystone/lib/list/ relationship.js

18.18% Statements 4/22
0% Branches 0/12
0% Functions 0/3
18.18% Lines 4/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                                          
var keystone = require('../../');
var utils = require('keystone-utils');
 
/**
 * Registers relationships to this list defined on others
 */
function relationship (def) {
	if (arguments.length > 1) {
		for (var i = 0; i < arguments.length; i++) {
			this.relationship(arguments[i]);
		}
		return this;
	}
	if (typeof def === 'string') {
		def = { ref: def };
	}
	if (!def.ref) {
		throw new Error('List Relationships must be specified with an object containing ref (' + this.key + ')');
	}
	if (!def.refPath) {
		def.refPath = utils.downcase(this.key);
	}
	if (!def.path) {
		def.path = utils.keyToProperty(def.ref, true);
	}
	Object.defineProperty(def, 'refList', {
		get: function () {
			return keystone.list(def.ref);
		},
	});
	Object.defineProperty(def, 'isValid', {
		get: function () {
			return keystone.list(def.ref) ? true : false;
		},
	});
	this.relationships[def.path] = def;
	return this;
}
 
module.exports = relationship;