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
|
||||
|
||||
def #{relation_name}
|
||||
related_#{relation_name}_ids.map do |id|
|
||||
relationship_object(id, "#{type}")
|
||||
end
|
||||
relationship_object(related_#{relation_name}_ids, "#{type}")
|
||||
end
|
||||
METHOD
|
||||
end
|
||||
@ -183,10 +181,19 @@ module AMS
|
||||
self.class._relations
|
||||
end
|
||||
|
||||
def relationship_object(id, type)
|
||||
{
|
||||
"data": { "id": id, "type": type }, # resource linkage
|
||||
}
|
||||
def relationship_object(id_or_ids, type)
|
||||
data =
|
||||
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
|
||||
|
||||
def dump(obj)
|
||||
|
||||
@ -30,7 +30,7 @@ module AMS
|
||||
id: 1, type: :profiles,
|
||||
attributes: { name: "name", summary: "description" },
|
||||
relationships:
|
||||
{ child_models: [{ data: { id: 2, type: "comments" } }] }
|
||||
{ child_models: { data: [{ id: 2, type: "comments" }] } }
|
||||
}
|
||||
assert_equal expected, @serializer_instance.as_json
|
||||
end
|
||||
@ -40,7 +40,7 @@ module AMS
|
||||
id: 1, type: :profiles,
|
||||
attributes: { name: "name", summary: "description" },
|
||||
relationships:
|
||||
{ child_models: [{ data: { id: 2, type: "comments" } }] }
|
||||
{ child_models: { data: [{ id: 2, type: "comments" }] } }
|
||||
}.to_json
|
||||
assert_equal expected, @serializer_instance.to_json
|
||||
end
|
||||
|
||||
@ -20,18 +20,18 @@ module AMS
|
||||
|
||||
def test_model_instance_relations
|
||||
expected_relations = {
|
||||
child_models: [{
|
||||
data: { type: "comments", id: 2 }
|
||||
}]
|
||||
child_models: {
|
||||
data: [{ type: "comments", id: 2 }]
|
||||
}
|
||||
}
|
||||
assert_equal expected_relations, @serializer_instance.relations
|
||||
end
|
||||
|
||||
def test_model_instance_relationship_object
|
||||
def test_model_instance_relationship_data
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user