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,6 +1,8 @@
class ApplicationController < ActionController::Base
class ApiLimitError < StandardError; end
self.responder = ApplicationResponder
skip_forgery_protection if: -> { SessionLoader.new(request).has_api_authentication? }
before_action :reset_current_user
before_action :set_current_user

View File

@@ -4,11 +4,7 @@ class ArtistCommentariesController < ApplicationController
def index
@commentaries = ArtistCommentary.search(search_params).paginate(params[:page], :limit => params[:limit])
respond_with(@commentaries) do |format|
format.xml do
render :xml => @commentaries.to_xml(:root => "artist-commentaries")
end
end
respond_with(@commentaries)
end
def search

View File

@@ -3,10 +3,6 @@ class ArtistCommentaryVersionsController < ApplicationController
def index
@commentary_versions = ArtistCommentaryVersion.search(search_params).paginate(params[:page], :limit => params[:limit])
respond_with(@commentary_versions) do |format|
format.xml do
render :xml => @commentary_versions.to_xml(:root => "artist-commentary-versions")
end
end
respond_with(@commentary_versions)
end
end

View File

@@ -3,11 +3,7 @@ class ArtistVersionsController < ApplicationController
def index
@artist_versions = ArtistVersion.includes(:updater).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@artist_versions) do |format|
format.xml do
render :xml => @artist_versions.to_xml(:root => "artist-versions")
end
end
respond_with(@artist_versions)
end
end

View File

@@ -78,11 +78,7 @@ private
@posts = @posts.includes(comments: [:creator])
@posts = @posts.includes(comments: [:votes]) if CurrentUser.is_member?
respond_with(@posts) do |format|
format.xml do
render :xml => @posts.to_xml(:root => "posts")
end
end
respond_with(@posts)
end
def index_by_comment
@@ -91,9 +87,6 @@ private
format.atom do
@comments = @comments.includes(:post, :creator).load
end
format.xml do
render :xml => @comments.to_xml(:root => "comments")
end
end
end

View File

@@ -21,11 +21,7 @@ class DmailsController < ApplicationController
end
@query = Dmail.active.visible.search(search_params)
@dmails = @query.paginate(params[:page], :limit => params[:limit])
respond_with(@dmails) do |format|
format.xml do
render :xml => @dmails.to_xml(:root => "dmails")
end
end
respond_with(@dmails)
end
def show

View File

@@ -4,11 +4,7 @@ class FavoriteGroupsController < ApplicationController
def index
@favorite_groups = FavoriteGroup.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@favorite_groups) do |format|
format.xml do
render :xml => @favorite_groups.to_xml(:root => "favorite-groups")
end
end
respond_with(@favorite_groups)
end
def show

View File

@@ -26,11 +26,7 @@ class ForumPostsController < ApplicationController
def index
@query = ForumPost.search(search_params)
@forum_posts = @query.includes(:topic).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@forum_posts) do |format|
format.xml do
render :xml => @forum_posts.to_xml(:root => "forum-posts")
end
end
respond_with(@forum_posts)
end
def search

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

View File

@@ -3,11 +3,7 @@ class NoteVersionsController < ApplicationController
def index
@note_versions = NoteVersion.search(search_params).paginate(params[:page], :limit => params[:limit])
respond_with(@note_versions) do |format|
format.html { @note_versions = @note_versions.includes(:updater) }
format.xml do
render :xml => @note_versions.to_xml(:root => "note-versions")
end
end
@note_versions = @note_versions.includes(:updater) if request.format.html?
respond_with(@note_versions)
end
end

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

View File

@@ -8,11 +8,7 @@ class PoolVersionsController < ApplicationController
end
@pool_versions = PoolArchive.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@pool_versions) do |format|
format.xml do
render :xml => @pool_versions.to_xml(:root => "pool-versions")
end
end
respond_with(@pool_versions)
end
def diff

View File

@@ -19,9 +19,6 @@ class PoolsController < ApplicationController
def index
@pools = Pool.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@pools) do |format|
format.xml do
render :xml => @pools.to_xml(:root => "pools")
end
format.json do
render json: @pools.to_json
expires_in params[:expiry].to_i.days if params[:expiry]

View File

@@ -10,11 +10,7 @@ class PostAppealsController < ApplicationController
def index
@post_appeals = PostAppeal.includes(:creator).search(search_params).includes(post: [:appeals, :uploader, :approver])
@post_appeals = @post_appeals.paginate(params[:page], limit: params[:limit])
respond_with(@post_appeals) do |format|
format.xml do
render :xml => @post_appeals.to_xml(:root => "post-appeals")
end
end
respond_with(@post_appeals)
end
def create

View File

@@ -10,11 +10,7 @@ class PostFlagsController < ApplicationController
def index
@post_flags = PostFlag.search(search_params).includes(:creator, post: [:flags, :uploader, :approver])
@post_flags = @post_flags.paginate(params[:page], limit: params[:limit])
respond_with(@post_flags) do |format|
format.xml do
render :xml => @post_flags.to_xml(:root => "post-flags")
end
end
respond_with(@post_flags)
end
def create

View File

@@ -5,11 +5,7 @@ class PostVersionsController < ApplicationController
def index
@post_versions = PostArchive.includes(:updater, post: [:versions]).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@post_versions) do |format|
format.xml do
render xml: @post_versions.to_xml(root: "post-versions")
end
end
respond_with(@post_versions)
end
def search

View File

@@ -13,9 +13,6 @@ class PostsController < ApplicationController
@posts = @post_set.posts
respond_with(@posts) do |format|
format.atom
format.xml do
render xml: @posts.to_xml(root: "posts")
end
end
end
end

View File

@@ -4,12 +4,7 @@ class SavedSearchesController < ApplicationController
def index
@saved_searches = saved_searches.search(search_params).paginate(params[:page], limit: params[:limit])
respond_with(@saved_searches) do |format|
format.xml do
render :xml => @saved_searches.to_xml(:root => "saved-searches")
end
end
respond_with(@saved_searches)
end
def labels

View File

@@ -23,11 +23,7 @@ class TagAliasesController < ApplicationController
def index
@tag_aliases = TagAlias.includes(:antecedent_tag, :consequent_tag, :approver).search(search_params).paginate(params[:page], :limit => params[:limit])
respond_with(@tag_aliases) do |format|
format.xml do
render :xml => @tag_aliases.to_xml(:root => "tag-aliases")
end
end
respond_with(@tag_aliases)
end
def destroy

View File

@@ -23,11 +23,7 @@ class TagImplicationsController < ApplicationController
def index
@tag_implications = TagImplication.includes(:antecedent_tag, :consequent_tag, :approver).search(search_params).paginate(params[:page], :limit => params[:limit])
respond_with(@tag_implications) do |format|
format.xml do
render :xml => @tag_implications.to_xml(:root => "tag-implications")
end
end
respond_with(@tag_implications)
end
def destroy

View File

@@ -10,12 +10,7 @@ class TagsController < ApplicationController
def index
@tags = Tag.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@tags) do |format|
format.xml do
render :xml => @tags.to_xml(:root => "tags")
end
end
respond_with(@tags)
end
def autocomplete
@@ -28,11 +23,7 @@ class TagsController < ApplicationController
expires_in params[:expiry].to_i.days if params[:expiry]
respond_with(@tags) do |format|
format.xml do
render :xml => @tags.to_xml(:root => "tags")
end
end
respond_with(@tags, root: "tags")
end
def show

View File

@@ -25,11 +25,7 @@ class UploadsController < ApplicationController
def index
@uploads = Upload.search(search_params).includes(:post, :uploader).paginate(params[:page], :limit => params[:limit])
respond_with(@uploads) do |format|
format.xml do
render :xml => @uploads.to_xml(:root => "uploads")
end
end
respond_with(@uploads)
end
def show

View File

@@ -22,11 +22,7 @@ class UserFeedbacksController < ApplicationController
def index
@search = UserFeedback.visible.search(search_params)
@user_feedbacks = @search.paginate(params[:page], :limit => params[:limit])
respond_with(@user_feedbacks) do |format|
format.xml do
render :xml => @user_feedbacks.to_xml(:root => "user-feedbacks")
end
end
respond_with(@user_feedbacks)
end
def create

View File

@@ -31,9 +31,6 @@ class UsersController < ApplicationController
else
@users = User.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@users) do |format|
format.xml do
render :xml => @users.to_xml(:root => "users")
end
format.json do
render json: @users.to_json
expires_in params[:expiry].to_i.days if params[:expiry]

View File

@@ -3,11 +3,7 @@ class WikiPageVersionsController < ApplicationController
def index
@wiki_page_versions = WikiPageVersion.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@wiki_page_versions) do |format|
format.xml do
render :xml => @wiki_page_versions.to_xml(:root => "wiki-page-versions")
end
end
respond_with(@wiki_page_versions)
end
def show

View File

@@ -26,9 +26,6 @@ class WikiPagesController < ApplicationController
end
end
end
format.xml do
render :xml => @wiki_pages.to_xml(:root => "wiki-pages")
end
format.json do
render json: @wiki_pages.to_json
expires_in params[:expiry].to_i.days if params[:expiry]

View File

@@ -0,0 +1,12 @@
# https://github.com/plataformatec/responders
# https://github.com/plataformatec/responders/blob/master/lib/action_controller/responder.rb
class ApplicationResponder < ActionController::Responder
# this is called by respond_with for non-html, non-js responses.
def to_format
if format == :xml
options[:root] ||= resource.table_name.dasherize if resource.respond_to?(:table_name)
end
super
end
end