Add OpenSearch suggestion support.
Add autocomplete support when searching Danbooru from the Chrome address bar. If you type "danb<tab>" in the address bar then search for a tag, then autocomplete results from Danbooru will appear as search suggestions in Chrome. Note that the "Autocomplete searches and URLs" Chrome setting must be enabled for this to work. Ref: * http://dev.chromium.org/tab-to-search * https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Supporting_search_suggestions_in_search_plugins * https://github.com/dewitt/opensearch/blob/master/mediawiki/Specifications/OpenSearch/Extensions/Suggestions/1.1/Draft%201.wiki
This commit is contained in:
16
app/controllers/autocomplete_controller.rb
Normal file
16
app/controllers/autocomplete_controller.rb
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
class AutocompleteController < ApplicationController
|
||||||
|
respond_to :xml, :json
|
||||||
|
|
||||||
|
def index
|
||||||
|
@tags = Tag.names_matches_with_aliases(params[:query], params.fetch(:limit, 10).to_i)
|
||||||
|
|
||||||
|
if request.variant.opensearch?
|
||||||
|
expires_in 1.hour
|
||||||
|
results = [params[:query], @tags.map(&:pretty_name)]
|
||||||
|
respond_with(results)
|
||||||
|
else
|
||||||
|
# XXX
|
||||||
|
respond_with(@tags.map(&:attributes))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -4,4 +4,5 @@
|
|||||||
<Description><%= Danbooru.config.app_name %> search</Description>
|
<Description><%= Danbooru.config.app_name %> search</Description>
|
||||||
<Image height="16" width="16" type="image/x-icon"><%= root_url %>favicon.ico</Image>
|
<Image height="16" width="16" type="image/x-icon"><%= root_url %>favicon.ico</Image>
|
||||||
<Url type="text/html" template="<%= posts_url %>?tags={searchTerms}&utm_source=opensearch"/>
|
<Url type="text/html" template="<%= posts_url %>?tags={searchTerms}&utm_source=opensearch"/>
|
||||||
|
<Url type="application/x-suggestions+json" template="<%= autocomplete_index_url(format: :json) %>?query={searchTerms}&type=tags&variant=opensearch"/>
|
||||||
</OpenSearchDescription>
|
</OpenSearchDescription>
|
||||||
|
|||||||
17
test/functional/autocomplete_controller_test.rb
Normal file
17
test/functional/autocomplete_controller_test.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class AutocompleteControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
context "Autocomplete controller" do
|
||||||
|
context "index action" do
|
||||||
|
setup do
|
||||||
|
create(:tag, name: "azur_lane")
|
||||||
|
end
|
||||||
|
|
||||||
|
should "work for opensearch queries" do
|
||||||
|
get autocomplete_index_path(query: "azur", variant: "opensearch"), as: :json
|
||||||
|
assert_response :success
|
||||||
|
assert_equal(["azur", ["azur lane"]], response.parsed_body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user