Module: MongoMapper::Plugins::ToViewModel::InstanceMethods

Defined in:
lib/mm_to_view_model.rb

Instance Method Summary collapse

Instance Method Details

#to_view_modelObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mm_to_view_model.rb', line 45

def to_view_model
  fields = keys.inject({}) do |memo, (k, v)|
    memo[k.to_s] = send(k)
    
    begin
      memo[k.to_s] = memo[k.to_s].class.from_mongo(memo[k.to_s])
    rescue
      # keep the unconverted version
    end
    
    memo
  end
  
  relations = associations.inject({}) do |memo, (k, v)|
    begin
      memo[k.to_s] = send(k).collect(&:to_view_model)
    rescue
      # Don't add to hash
    end
    memo
  end
  
  fields.merge(relations)
end