diff --git a/app/controllers/counts_controller.rb b/app/controllers/counts_controller.rb index 35694b3ee..4baf35f2d 100644 --- a/app/controllers/counts_controller.rb +++ b/app/controllers/counts_controller.rb @@ -1,9 +1,15 @@ class CountsController < ApplicationController - respond_to :xml, :json + respond_to :html, :xml, :json def posts estimate_count = params.fetch(:estimate_count, "true").truthy? skip_cache = params.fetch(:skip_cache, "false").truthy? @count = PostQueryBuilder.new(params[:tags], CurrentUser.user).normalized_query.fast_count(timeout: CurrentUser.statement_timeout, estimate_count: estimate_count, skip_cache: skip_cache) + + if request.format.xml? + respond_with({ posts: @count }, root: "counts") + else + respond_with({ counts: { posts: @count }}) + end end end diff --git a/app/views/counts/posts.json.erb b/app/views/counts/posts.json.erb deleted file mode 100644 index 7c68802cb..000000000 --- a/app/views/counts/posts.json.erb +++ /dev/null @@ -1 +0,0 @@ -{"counts": {"posts": <%= @count %>}} diff --git a/app/views/counts/posts.xml.erb b/app/views/counts/posts.xml.erb deleted file mode 100644 index 9ae07f67d..000000000 --- a/app/views/counts/posts.xml.erb +++ /dev/null @@ -1,6 +0,0 @@ - - - - <%= @count %> - - diff --git a/test/functional/counts_controller_test.rb b/test/functional/counts_controller_test.rb index 24c6dad03..b400521de 100644 --- a/test/functional/counts_controller_test.rb +++ b/test/functional/counts_controller_test.rb @@ -7,6 +7,16 @@ class CountsControllerTest < ActionDispatch::IntegrationTest get posts_counts_path assert_response :success end + + should "render for json" do + get posts_counts_path(format: :json) + assert_response :success + end + + should "render for xml" do + get posts_counts_path(format: :xml) + assert_response :success + end end end end