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

@@ -7,12 +7,8 @@ class NotesController < ApplicationController
def index
@notes = Note.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@notes) do |format|
format.html { @notes = @notes.includes(:creator) }
format.xml do
render :xml => @notes.to_xml(:root => "notes")
end
end
@notes = @notes.includes(:creator) if request.format.html?
respond_with(@notes)
end
def show