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

View File

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

View File

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