Skip to content

Commit

Permalink
Merge 6458caf into 919bb38
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwalker committed Oct 10, 2013
2 parents 919bb38 + 6458caf commit 94cf359
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,31 @@ this:
}
```

You can sideload the objects without embedding the ids by using the :embed_root option and settings :embed to false. You may wish to do this if your id references are already custom serialized in another embedded object.

```ruby
class PostSerializer < ActiveModel::Serializer

attributes :id, :title, :body
has_many :comments, embed: false, embed_root: true
end
```

```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!"
},
"comments": [
{ "id": 1, "body": "what a dumb post", "tag_ids": [ 1, 2 ] },
{ "id": 2, "body": "i liked it", "tag_ids": [ 1, 3 ] },
]
}
```


You can also specify a different root for the embedded objects than the key
used to reference them:

Expand Down
12 changes: 6 additions & 6 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,14 @@ def include!(name, options={})
options[:value] ||= send(name)
association = association_class.new(name, options, self.options)

if association.embed_in_root? && hash.nil?
raise IncludeError.new(self.class, association.name)
elsif association.embed_in_root? && association.embeddable?
merge_association hash, association.root, association.serializables, unique_values
end

if association.embed_ids?
node[association.key] = association.serialize_ids

if association.embed_in_root? && hash.nil?
raise IncludeError.new(self.class, association.name)
elsif association.embed_in_root? && association.embeddable?
merge_association hash, association.root, association.serializables, unique_values
end
elsif association.embed_objects?
node[association.key] = association.serialize
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def initialize(name, options={}, serializer_options={})
@embed_ids = embed == :id || embed == :ids
@embed_objects = embed == :object || embed == :objects
@embed_key = options[:embed_key] || :id
@embed_in_root = options[:include]
@embed_in_root = options[:embed_root] || (options[:include] && @embed_ids)

serializer = options[:serializer]
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
Expand Down
10 changes: 10 additions & 0 deletions test/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ def test_include_bang_with_embed_ids_include_false
assert_equal({}, @root_hash)
end

def test_include_bang_with_embed_false_embed_root_true
include! :comments, value: @post.comments, embed: false, include: false, embed_root: true

assert_equal({}, @hash)

assert_equal({
comments: [{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }]
}, @root_hash)
end

def test_include_bang_has_one_associations
include! :comment, value: @post.comment

Expand Down

0 comments on commit 94cf359

Please sign in to comment.