autocomplete: render html server-side.

Render the HTML for autocomplete results server-side instead of in
Javascript. This is cleaner than building HTML in Javascript, but it may
hurt caching because the HTTP responses are larger.

Fixes #4698: user autocomplete contains links to /posts

Also fixes a bug where tag counts in the autocomplete menu were different
from tag counts displayed elsewhere because of differences in rounding.
This commit is contained in:
evazion
2022-08-29 20:50:19 -05:00
parent 55266be2ef
commit cf13ab1540
7 changed files with 84 additions and 58 deletions

View File

@@ -2,10 +2,10 @@ require "test_helper"
class AutocompleteControllerTest < ActionDispatch::IntegrationTest
def autocomplete(query, type)
get autocomplete_index_path(search: { query: query, type: type }), as: :json
get autocomplete_index_path(search: { query: query, type: type })
assert_response :success
response.parsed_body.map { |result| result["value"] }
response.parsed_body.css("li").map { |html| html["data-autocomplete-value"] }
end
def assert_autocomplete_equals(expected_value, query, type)
@@ -35,6 +35,24 @@ class AutocompleteControllerTest < ActionDispatch::IntegrationTest
assert_autocomplete_equals(["rating:sensitive"], "-rating:s", "tag_query")
end
should "work for an aliased tag" do
create(:tag_alias, antecedent_name: "oc", consequent_name: "original")
assert_autocomplete_equals(["original"], "oc", "tag_query")
end
should "work for the user: metatag" do
create(:user, name: "foobar")
assert_autocomplete_equals(["user:foobar"], "user:foo", "tag_query")
end
should "work for the pool: metatag" do
as(create(:user)) { create(:pool, name: "foobar") }
assert_autocomplete_equals(["pool:foobar"], "pool:foo", "tag_query")
end
should "work for a missing type" do
get autocomplete_index_path(search: { query: "azur" }), as: :json
@@ -48,7 +66,7 @@ class AutocompleteControllerTest < ActionDispatch::IntegrationTest
end
should "not set session cookies when the response is publicly cached" do
get autocomplete_index_path(search: { query: "azur", type: "tag_query" }), as: :json
get autocomplete_index_path(search: { query: "azur", type: "tag_query" })
assert_response :success
assert_equal(true, response.cache_control[:public])