-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentity_info.js
56 lines (47 loc) · 1.63 KB
/
entity_info.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* global _ */
const Sugar = require('sugar')
const UrlPattern = require('url-pattern')
require('sugar-inflections')
// const cloudinary = require('./cloudinary')
module.exports = function (name, options = {}) {
this.name = name
this.singularDisplay = Sugar.String.capitalize(name)
this.pluralDisplay = Sugar.String.capitalize(Sugar.String.pluralize(name))
this.fields = []
Object.assign(this, options)
this.configField = function (fieldName, entityData) {
var fieldData = {
label: entityData.label || Sugar.String.titleize(fieldName),
name: fieldName,
type: entityData.type || 'text',
adminList: entityData.adminList
}
_.extend(fieldData, entityData)
this.fields.push(fieldData)
}
this.setValues = function (obj, input) {
this.fields.forEach((field) => {
if (field.type === 'text' ||
field.type === 'select' ||
field.type === 'boolean'
) {
if (input[field.name] !== undefined) obj[field.name] = input[field.name]
} else if (field.type === 'cloudinary') {
// var identifierField = field.name + '_cloudinaryIdentifier'
// var identifierValue = input[identifierField]
// if (!identifierValue) return
// var preloadedFile = new cloudinary.PreloadedFile(identifierValue)
// if (preloadedFile.is_valid()) obj[field.name] = preloadedFile.identifier()
// else console.log('invalid sig')
}
})
}
this.interpolateLink = function (obj) {
this.fields.forEach(f => {
if (f.link) {
const pattern = new UrlPattern(f.link.url)
obj._link = pattern.stringify(obj)
}
})
}
}