Fix #4035: The Related Tag JSON endpoint is returning the wrong information
* Fix /related_tags.json to return a list of tags in the `other_wikis` field. * Add support for /related_tags.xml.
This commit is contained in:
@@ -4,11 +4,7 @@ class RelatedTagsController < ApplicationController
|
||||
|
||||
def show
|
||||
@query = RelatedTagQuery.new(query: params[:query], category: params[:category], user: CurrentUser.user)
|
||||
respond_with(@query) do |format|
|
||||
format.json do
|
||||
render :json => @query.to_json
|
||||
end
|
||||
end
|
||||
respond_with(@query)
|
||||
end
|
||||
|
||||
def update
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
class RelatedTagQuery
|
||||
include ActiveModel::Serializers::JSON
|
||||
include ActiveModel::Serializers::Xml
|
||||
|
||||
attr_reader :query, :category, :user
|
||||
|
||||
def initialize(query: nil, category: nil, user: nil)
|
||||
@@ -46,7 +49,7 @@ class RelatedTagQuery
|
||||
results
|
||||
end
|
||||
|
||||
def other_wiki_category_tags
|
||||
def other_wiki_pages
|
||||
return [] unless Tag.category_for(query) == Tag.categories.copyright
|
||||
|
||||
other_wikis = wiki_page&.tags.to_a.grep(/^list_of_/i)
|
||||
@@ -56,16 +59,22 @@ class RelatedTagQuery
|
||||
end
|
||||
|
||||
def tags_for_html
|
||||
map_with_category_data(tags)
|
||||
tags_with_categories(tags)
|
||||
end
|
||||
|
||||
def to_json
|
||||
{:query => query, :category => category, :tags => map_with_category_data(tags), :wiki_page_tags => map_with_category_data(wiki_page_tags), :other_wikis => other_wiki_category_tags}.to_json
|
||||
def serializable_hash(**options)
|
||||
{
|
||||
query: query,
|
||||
category: category,
|
||||
tags: tags_with_categories(tags),
|
||||
wiki_page_tags: tags_with_categories(wiki_page_tags),
|
||||
other_wikis: other_wiki_pages.map { |wiki| [wiki.title, tags_with_categories(wiki.tags)] }.to_h
|
||||
}
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def map_with_category_data(list_of_tag_names)
|
||||
def tags_with_categories(list_of_tag_names)
|
||||
Tag.categories_for(list_of_tag_names).to_a
|
||||
end
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<%= render "related_tags/tag_column", tags: related_tags.tags, class: "general-related-tags-column", title: related_tags.pretty_name %>
|
||||
<%= render "related_tags/tag_column", tags: related_tags.wiki_page_tags, class: "wiki-related-tags-column", title: link_to("wiki:#{related_tags.pretty_name}", show_or_new_wiki_pages_path(title: related_tags.query)) %>
|
||||
|
||||
<% related_tags.other_wiki_category_tags.each do |wiki| %>
|
||||
<% related_tags.other_wiki_pages.each do |wiki| %>
|
||||
<%= render "related_tags/tag_column", tags: wiki.tags, class: "other-wiki-related-tags-column", title: link_to("wiki:#{wiki.pretty_title}", show_or_new_wiki_pages_path(title: wiki.title)) %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
@@ -7,7 +7,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
end
|
||||
|
||||
context "#other_wiki_category_tags" do
|
||||
context "#other_wiki_pages" do
|
||||
subject { RelatedTagQuery.new(query: "copyright") }
|
||||
|
||||
setup do
|
||||
@@ -17,7 +17,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "return tags from the associated list wiki" do
|
||||
result = subject.other_wiki_category_tags
|
||||
result = subject.other_wiki_pages
|
||||
assert_not_nil(result[0])
|
||||
assert_equal(%w(alpha beta), result[0].tags)
|
||||
end
|
||||
@@ -40,7 +40,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "render the json" do
|
||||
assert_equal("{\"query\":\"aaa\",\"category\":null,\"tags\":[[\"aaa\",0],[\"bbb\",0],[\"ccc\",0]],\"wiki_page_tags\":[],\"other_wikis\":[]}", @query.to_json)
|
||||
assert_equal("{\"query\":\"aaa\",\"category\":null,\"tags\":[[\"aaa\",0],[\"bbb\",0],[\"ccc\",0]],\"wiki_page_tags\":[],\"other_wikis\":{}}", @query.to_json)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user