api: refactor default options for xml responses.

In xml responses, if the result is an empty array we want the response
to look like this:

   <posts type="array"/>

not like this (the default):

   <nil-classes type="array"/>

This refactors controllers so that this is done automatically instead of
having to manually call `@things.to_xml(root: "things")` everywhere. We
do this by overriding the behavior of `respond_with` in `ApplicationResponder`
to set the `root` option by default in xml responses.
This commit is contained in:
evazion
2019-09-08 15:32:31 -05:00
parent 32343303d2
commit 3f7e05316d
30 changed files with 56 additions and 143 deletions

View File

@@ -191,5 +191,13 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest
assert_equal(1, response.parsed_body.size)
assert_equal(tags.first.id, response.parsed_body.first.fetch("id"))
end
should "return the correct root element name for empty xml responses" do
get tags_path, as: :xml
assert_response :success
assert_equal("tags", response.parsed_body.root.name)
assert_equal(0, response.parsed_body.root.children.size)
end
end
end