mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Correct relationships data
This commit is contained in:
parent
2d116f6455
commit
f885ee8d7d
@ -108,9 +108,7 @@ module AMS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def #{relation_name}
|
def #{relation_name}
|
||||||
related_#{relation_name}_ids.map do |id|
|
relationship_object(related_#{relation_name}_ids, "#{type}")
|
||||||
relationship_object(id, "#{type}")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
end
|
end
|
||||||
@ -183,10 +181,19 @@ module AMS
|
|||||||
self.class._relations
|
self.class._relations
|
||||||
end
|
end
|
||||||
|
|
||||||
def relationship_object(id, type)
|
def relationship_object(id_or_ids, type)
|
||||||
{
|
data =
|
||||||
"data": { "id": id, "type": type }, # resource linkage
|
if id_or_ids.respond_to?(:to_ary)
|
||||||
}
|
id_or_ids.map { |id| relationship_data(id, type) }
|
||||||
|
else
|
||||||
|
relationship_data(id_or_ids, type)
|
||||||
|
end
|
||||||
|
{ "data": data }
|
||||||
|
end
|
||||||
|
|
||||||
|
# resource linkage
|
||||||
|
def relationship_data(id, type)
|
||||||
|
{ "id": id, "type": type }
|
||||||
end
|
end
|
||||||
|
|
||||||
def dump(obj)
|
def dump(obj)
|
||||||
|
|||||||
@ -30,7 +30,7 @@ module AMS
|
|||||||
id: 1, type: :profiles,
|
id: 1, type: :profiles,
|
||||||
attributes: { name: "name", summary: "description" },
|
attributes: { name: "name", summary: "description" },
|
||||||
relationships:
|
relationships:
|
||||||
{ child_models: [{ data: { id: 2, type: "comments" } }] }
|
{ child_models: { data: [{ id: 2, type: "comments" }] } }
|
||||||
}
|
}
|
||||||
assert_equal expected, @serializer_instance.as_json
|
assert_equal expected, @serializer_instance.as_json
|
||||||
end
|
end
|
||||||
@ -40,7 +40,7 @@ module AMS
|
|||||||
id: 1, type: :profiles,
|
id: 1, type: :profiles,
|
||||||
attributes: { name: "name", summary: "description" },
|
attributes: { name: "name", summary: "description" },
|
||||||
relationships:
|
relationships:
|
||||||
{ child_models: [{ data: { id: 2, type: "comments" } }] }
|
{ child_models: { data: [{ id: 2, type: "comments" }] } }
|
||||||
}.to_json
|
}.to_json
|
||||||
assert_equal expected, @serializer_instance.to_json
|
assert_equal expected, @serializer_instance.to_json
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,18 +20,18 @@ module AMS
|
|||||||
|
|
||||||
def test_model_instance_relations
|
def test_model_instance_relations
|
||||||
expected_relations = {
|
expected_relations = {
|
||||||
child_models: [{
|
child_models: {
|
||||||
data: { type: "comments", id: 2 }
|
data: [{ type: "comments", id: 2 }]
|
||||||
}]
|
}
|
||||||
}
|
}
|
||||||
assert_equal expected_relations, @serializer_instance.relations
|
assert_equal expected_relations, @serializer_instance.relations
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_model_instance_relationship_object
|
def test_model_instance_relationship_data
|
||||||
expected = {
|
expected = {
|
||||||
data: { type: :bananas, id: 5 }
|
type: :bananas, id: 5
|
||||||
}
|
}
|
||||||
assert_equal expected, @serializer_instance.relationship_object(5, :bananas)
|
assert_equal expected, @serializer_instance.relationship_data(5, :bananas)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user