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
|
# File 'lib/mm_to_view_model.rb', line 10
def to_view_model
fields = keys.inject({}) do |memo, (k, v)|
memo[k.to_s] = if v.type == ObjectId
nil
else
begin
unless v.default_value.nil?
v.default_value
else
v.type.new
end
rescue
nil
end
end
begin
memo[k.to_s] = memo[k.to_s].class.from_mongo(memo[k.to_s])
rescue
end
memo
end
relations = associations.inject({}) do |memo, (k, v)|
memo[k.to_s] = [v.klass.to_view_model] if ['OneAssociation', 'ManyAssociation'].include?(v.class.to_s.split(/::/).last)
memo
end
fields.merge(relations)
end
|