-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.ls
78 lines (61 loc) · 1.63 KB
/
conversions.ls
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* User: gisborne
* Date: 9/15/14
* Time: 22:36
*/
require! dust: 'dustjs-linkedin'
require! './functionalism'
require! fs
require! _: 'prelude-ls'
require! path
require! uuid: 'node-uuid'
define_fn = functionalism.define_fn
resolve = path.resolve
keys = _.keys
types_map =
'integer' => 'number'
'float' => 'number step=any'
'string' => 'text'
templates = {}
getInputType = (t) ->
types_map[t] || t
getTemplateFromFile = (name) ->
p = resolve __dirname, '../templates/' + name
if fs.existsSync p
f = fs.readFileSync p, {encoding: 'utf8'}
else
p = resolve __dirname, '../templates/' + name + '.dust'
if fs.existsSync p
f = fs.readFileSync p, {encoding: 'utf8'}
else
throw new Error 'Template not found' unless f
templates[name] = dust.compileFn f
export renderTemplate = (name, vals, res) ->
t = (templates[name] || getTemplateFromFile name)
t vals, (err, out) ->
raise new Error err if err
res out
getFormVals = (model, fields, record) ->
result = {fields: []}
_.each ((k) ->
unless k == 'id'
if record
result.fields ++= [{name: k, type: getInputType(fields[k]), value: record[k]}]
else
result.fields ++= [{name: k, type: getInputType(fields[k])}]),
_.Obj.keys(fields)
if record
id = record.id
else
id = uuid.v4()
result.id = id
result.model = model
result
/*
Convert a relation to a form
*/
define_fn 'D0B16C5A-40DC-40F0-9CE0-F7B692F4598D', {name: 'new'}, (model, rel, req, res, next) ->
fields = rel.rows[0].fields
context = getFormVals model, fields
renderTemplate 'relation_form', context, (result) ->
res.send result