Correct relationships data

This commit is contained in:
Benjamin Fleischer 2017-05-15 10:22:13 -05:00
parent 2d116f6455
commit f885ee8d7d
3 changed files with 22 additions and 15 deletions

View File

@ -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)

View File

@ -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

View File

@ -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