/related_tag: convert to table builder.

This commit is contained in:
evazion
2020-01-07 00:57:58 -06:00
parent ef89e7321f
commit ab4af3c410
3 changed files with 17 additions and 34 deletions

View File

@@ -18,11 +18,11 @@ class RelatedTagQuery
if query =~ /\*/ if query =~ /\*/
pattern_matching_tags pattern_matching_tags
elsif category.present? elsif category.present?
RelatedTagCalculator.frequent_tags_for_search(query, category: Tag.categories.value_for(category)).take(25).pluck(:name) RelatedTagCalculator.frequent_tags_for_search(query, category: Tag.categories.value_for(category)).take(25)
elsif query.present? elsif query.present?
RelatedTagCalculator.similar_tags_for_search(query).take(25).map(&:name) RelatedTagCalculator.similar_tags_for_search(query).take(25)
else else
[] Tag.none
end end
end end
@@ -54,15 +54,11 @@ class RelatedTagQuery
other_wikis other_wikis
end end
def tags_for_html
tags_with_categories(tags)
end
def serializable_hash(**options) def serializable_hash(**options)
{ {
query: query, query: query,
category: category, category: category,
tags: tags_with_categories(tags), tags: tags_with_categories(tags.map(&:name)),
wiki_page_tags: tags_with_categories(wiki_page_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 other_wikis: other_wiki_pages.map { |wiki| [wiki.title, tags_with_categories(wiki.tags)] }.to_h
} }
@@ -75,7 +71,7 @@ class RelatedTagQuery
end end
def pattern_matching_tags def pattern_matching_tags
Tag.name_matches(query).where("post_count > 0").order("post_count desc").limit(50).sort_by {|x| x.name}.map(&:name) Tag.nonempty.name_matches(query).order("post_count desc, name asc").limit(50)
end end
def wiki_page def wiki_page

View File

@@ -9,26 +9,13 @@
<% end %> <% end %>
<% if params.dig(:search, :query).present? %> <% if params.dig(:search, :query).present? %>
<section> <%= table_for @query.tags do |t| %>
<table class="striped"> <% t.column "Name" do |tag| %>
<thead> <%= link_to_wiki "?", tag.name, class: "tag-type-#{tag.category}" %>
<tr> <%= link_to tag.name, posts_path(tags: tag.name), class: "tag-type-#{tag.category}" %>
<th>Name</th> <% end %>
</tr> <% end %>
</thead> <% end %>
<tbody>
<% @query.tags_for_html.each do |tag, category| %>
<tr>
<td class="category-<%= category %>">
<%= link_to_wiki "?", tag %>
<%= link_to(tag, posts_path(:tags => tag)) %>
</td>
</tr>
<% end %>
</tbody>
</table>
</section>
<% end %>
</div> </div>
</div> </div>

View File

@@ -37,7 +37,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
end end
should "work" do should "work" do
assert_equal(["aaa", "bbb", "ccc"], @query.tags) assert_equal(["aaa", "bbb", "ccc"], @query.tags.map(&:name))
end end
should "render the json" do should "render the json" do
@@ -51,7 +51,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
end end
should "work" do should "work" do
assert_equal([], @query.tags) assert_equal(true, @query.tags.empty?)
end end
end end
@@ -68,7 +68,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
end end
should "take related tags from the consequent tag" do should "take related tags from the consequent tag" do
assert_equal(%w[aaa bbb ccc], @query.tags) assert_equal(%w[aaa bbb ccc], @query.tags.map(&:name))
end end
end end
@@ -78,7 +78,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
end end
should "work" do should "work" do
assert_equal(["aaa"], @query.tags) assert_equal(["aaa"], @query.tags.map(&:name))
end end
end end
@@ -118,7 +118,7 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
end end
should "find the related tags" do should "find the related tags" do
assert_equal(%w(ccc), @query.tags) assert_equal(["ccc"], @query.tags.map(&:name))
end end
end end
end end