mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Implement has_one's embed objects
This commit is contained in:
parent
d756ae4a70
commit
52bb3f6929
@ -75,8 +75,8 @@ module ActiveModel
|
|||||||
if association.embed_ids?
|
if association.embed_ids?
|
||||||
hash[association.key] = serialize_ids association
|
hash[association.key] = serialize_ids association
|
||||||
elsif association.embed_objects?
|
elsif association.embed_objects?
|
||||||
# TODO
|
associated_data = send(association.name)
|
||||||
hash
|
hash[association.embedded_key] = association.build_serializer(associated_data).serializable_hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -37,6 +37,10 @@ module ActiveModel
|
|||||||
def key
|
def key
|
||||||
"#{name}_id"
|
"#{name}_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def embedded_key
|
||||||
|
name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -17,19 +17,33 @@ module ActiveModel
|
|||||||
assert_equal 'model', associations[0].name
|
assert_equal 'model', associations[0].name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_serialization_using_serializable_hash
|
def test_associations_embedding_ids_serialization_using_serializable_hash
|
||||||
@model_serializer.class._associations[0].embed = :ids
|
@model_serializer.class._associations[0].embed = :ids
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
||||||
}, @model_serializer.serializable_hash)
|
}, @model_serializer.serializable_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_serialization_using_as_json
|
def test_associations_embedding_ids_serialization_using_as_json
|
||||||
@model_serializer.class._associations[0].embed = :ids
|
@model_serializer.class._associations[0].embed = :ids
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
||||||
}, @model_serializer.as_json)
|
}, @model_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||||
|
@model_serializer.class._associations[0].embed = :objects
|
||||||
|
assert_equal({
|
||||||
|
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||||
|
}, @model_serializer.serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_objects_serialization_using_as_json
|
||||||
|
@model_serializer.class._associations[0].embed = :objects
|
||||||
|
assert_equal({
|
||||||
|
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||||
|
}, @model_serializer.as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user