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
|
def show
|
||||||
@query = RelatedTagQuery.new(query: params[:query], category: params[:category], user: CurrentUser.user)
|
@query = RelatedTagQuery.new(query: params[:query], category: params[:category], user: CurrentUser.user)
|
||||||
respond_with(@query) do |format|
|
respond_with(@query)
|
||||||
format.json do
|
|
||||||
render :json => @query.to_json
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
class RelatedTagQuery
|
class RelatedTagQuery
|
||||||
|
include ActiveModel::Serializers::JSON
|
||||||
|
include ActiveModel::Serializers::Xml
|
||||||
|
|
||||||
attr_reader :query, :category, :user
|
attr_reader :query, :category, :user
|
||||||
|
|
||||||
def initialize(query: nil, category: nil, user: nil)
|
def initialize(query: nil, category: nil, user: nil)
|
||||||
@@ -46,7 +49,7 @@ class RelatedTagQuery
|
|||||||
results
|
results
|
||||||
end
|
end
|
||||||
|
|
||||||
def other_wiki_category_tags
|
def other_wiki_pages
|
||||||
return [] unless Tag.category_for(query) == Tag.categories.copyright
|
return [] unless Tag.category_for(query) == Tag.categories.copyright
|
||||||
|
|
||||||
other_wikis = wiki_page&.tags.to_a.grep(/^list_of_/i)
|
other_wikis = wiki_page&.tags.to_a.grep(/^list_of_/i)
|
||||||
@@ -56,16 +59,22 @@ class RelatedTagQuery
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tags_for_html
|
def tags_for_html
|
||||||
map_with_category_data(tags)
|
tags_with_categories(tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_json
|
def serializable_hash(**options)
|
||||||
{: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
|
{
|
||||||
|
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
|
end
|
||||||
|
|
||||||
protected
|
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
|
Tag.categories_for(list_of_tag_names).to_a
|
||||||
end
|
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.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)) %>
|
<%= 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)) %>
|
<%= 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 %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
|
|||||||
CurrentUser.ip_addr = "127.0.0.1"
|
CurrentUser.ip_addr = "127.0.0.1"
|
||||||
end
|
end
|
||||||
|
|
||||||
context "#other_wiki_category_tags" do
|
context "#other_wiki_pages" do
|
||||||
subject { RelatedTagQuery.new(query: "copyright") }
|
subject { RelatedTagQuery.new(query: "copyright") }
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
@@ -17,7 +17,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "return tags from the associated list wiki" do
|
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_not_nil(result[0])
|
||||||
assert_equal(%w(alpha beta), result[0].tags)
|
assert_equal(%w(alpha beta), result[0].tags)
|
||||||
end
|
end
|
||||||
@@ -40,7 +40,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "render the json" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user