Module: Mongoid::Document

Included in:
ActivityNotification::ORM::Mongoid::Notification, ActivityNotification::ORM::Mongoid::Subscription
Defined in:
lib/activity_notification/orm/mongoid.rb

Overview

Monkey patching for Mongoid::Document as_json

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Hash

Monkey patching for Mongoid::Document as_json

Parameters:

  • options (Hash) (defaults to: {})

    Options parameter

Returns:

  • (Hash)

    Hash representing the model



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/activity_notification/orm/mongoid.rb', line 89

def as_json(options = {})
  json = super(options)
  json["id"] = json["_id"].to_s.start_with?("{\"$oid\"=>") ? self.id.to_s : json["_id"].to_s
  if options.has_key?(:include)
    case options[:include]
    when Symbol then json[options[:include].to_s] = self.send(options[:include]).as_json
    when Array  then options[:include].each {|model| json[model.to_s] = self.send(model).as_json }
    when Hash   then options[:include].each {|model, options| json[model.to_s] = self.send(model).as_json(options) }
    end
  end
  json
end