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

@@ -1,5 +1,6 @@
class ForumTopicsController < ApplicationController
respond_to :html, :xml, :json
respond_to :atom, only: [:index, :show]
before_action :member_only, :except => [:index, :show]
before_action :moderator_only, :only => [:new_merge, :create_merge]
before_action :normalize_search, :only => :index
@@ -24,21 +25,10 @@ class ForumTopicsController < ApplicationController
@query = ForumTopic.active.search(search_params)
@forum_topics = @query.paginate(params[:page], :limit => per_page, :search_count => params[:search])
@forum_topics = @forum_topics.includes(:creator, :updater).load if request.format.html?
@forum_topics = @forum_topics.includes(:creator, :original_post).load if request.format.atom?
respond_with(@forum_topics) do |format|
format.html do
@forum_topics = @forum_topics.includes(:creator, :updater).load
end
format.atom do
@forum_topics = @forum_topics.includes(:creator, :original_post).load
end
format.json do
render :json => @forum_topics.to_json
end
format.xml do
render :xml => @forum_topics.to_xml(:root => "forum-topics")
end
end
respond_with(@forum_topics)
end
def show
@@ -46,12 +36,9 @@ class ForumTopicsController < ApplicationController
@forum_topic.mark_as_read!(CurrentUser.user)
end
@forum_posts = ForumPost.search(:topic_id => @forum_topic.id).reorder("forum_posts.id").paginate(params[:page])
@forum_posts = @forum_posts.reverse_order.includes(:creator).load if request.format.atom?
@original_forum_post_id = @forum_topic.original_post.id
respond_with(@forum_topic) do |format|
format.atom do
@forum_posts = @forum_posts.reverse_order.includes(:creator).load
end
end
respond_with(@forum_topic)
end
def create