Module: MongoMapper::Plugins::RadMicelaneous::InstanceMethods

Defined in:
lib/rad/mongo_mapper/rad_micelaneous.rb

Instance Method Summary collapse

Instance Method Details

#to_rson(options = {}) ⇒ Object



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
# File 'lib/rad/mongo_mapper/rad_micelaneous.rb', line 8

def to_rson options = {}
  with_errors = if options.include?('errors')
    options.delete 'errors'
  elsif options.include?(:errors)
    options.delete :errors
  else
    true
  end
  
  # standard MM as_json conversion
  hash = as_json(options)
  
  # MM fix
  hash['id'] = hash.delete :id if hash.include? :id
            
  # adding errors
  if with_errors            
    errors = {}
    errors.each do |name, list|
      errors[name.to_s] = list
    end
    hash['errors'] = errors unless errors.empty?
  end
  
  hash
end