diff --git a/app/controllers/related_tags_controller.rb b/app/controllers/related_tags_controller.rb index 438c4f455..99669017e 100644 --- a/app/controllers/related_tags_controller.rb +++ b/app/controllers/related_tags_controller.rb @@ -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 diff --git a/app/logical/related_tag_query.rb b/app/logical/related_tag_query.rb index 81f58f87c..66065b61d 100644 --- a/app/logical/related_tag_query.rb +++ b/app/logical/related_tag_query.rb @@ -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 diff --git a/app/views/related_tags/_current_tags.html.erb b/app/views/related_tags/_current_tags.html.erb index b11d7e655..aa87e8754 100644 --- a/app/views/related_tags/_current_tags.html.erb +++ b/app/views/related_tags/_current_tags.html.erb @@ -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 %> diff --git a/test/unit/related_tag_query_test.rb b/test/unit/related_tag_query_test.rb index 97578a056..eac4524a5 100644 --- a/test/unit/related_tag_query_test.rb +++ b/test/unit/related_tag_query_test.rb @@ -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