Method: Hypermodel::Serializers::Mongoid#embedded_resources

Defined in:
lib/hypermodel/serializers/mongoid.rb

#embedded_resourcesObject

Public: Returns a Hash with the embedded resources attributes. It will be used by Hypermodel::Resource.

An example of an embedded resource could be the reviews of a post, or the addresses of a company. But you can really embed whatever you like.

An example of the returning Hash could be the following:

{"comments"=>
  [
    {"_id"=>"4fb941cb82b4d46162000007", "body"=>"Comment 1"},
    {"_id"=>"4fb941cb82b4d46162000008", "body"=>"Comment 2"}
  ]
}


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hypermodel/serializers/mongoid.rb', line 71

def embedded_resources
  return {} if embedded_relations.empty?

  embedded_relations.inject({}) do |acc, (name, )|
    if attributes = extract_embedded_attributes(name)
      @attributes.delete(name)
      acc.update(name => attributes)
    end
    acc
  end
end