tags: eliminate Tag.category_for method.

Tag.category_for looked up a tag's category in the Redis cache. This was
only used in a few places (in related tags, and on the popular/missed
search pages). Get rid of this method so we can work towards getting rid
of caching tag categories in Redis.
This commit is contained in:
evazion
2020-12-27 21:03:26 -06:00
parent 1047b1f8af
commit 7e8f859b24
6 changed files with 17 additions and 31 deletions

View File

@@ -985,6 +985,11 @@ class PostQueryBuilder
def is_wildcard_search?
is_single_tag? && tags.first.wildcard
end
def simple_tag
return nil if !is_simple_tag?
Tag.find_by_name(tags.first.name)
end
end
memoize :split_query, :normalized_query

View File

@@ -71,9 +71,12 @@ class RelatedTagQuery
end
def other_wiki_pages
if Tag.category_for(query) == Tag.categories.copyright
tag = post_query.simple_tag
return [] if tag.nil?
if tag.copyright?
copyright_other_wiki_pages
elsif Tag.category_for(query) == Tag.categories.general
elsif tag.general?
general_other_wiki_pages
else
[]

View File

@@ -127,18 +127,6 @@ class Tag < ApplicationRecord
Tag.where(name: tag_name).pick(:category).to_i
end
def category_for(tag_name, options = {})
return Tag.categories.general if tag_name.blank?
if options[:disable_caching]
select_category_for(tag_name)
else
Cache.get("tc:#{Cache.hash(tag_name)}") do
select_category_for(tag_name)
end
end
end
def categories_for(tag_names, options = {})
if options[:disable_caching]
Array(tag_names).inject({}) do |hash, tag_name|

View File

@@ -15,11 +15,11 @@
</tr>
</thead>
<tbody>
<% @missed_searches.each do |tags, count| %>
<tr class="tag-type-<%= Tag.category_for(tags) %>">
<td><%= link_to tags, posts_path(:tags => tags) %></td>
<% @missed_searches.each do |search, count| %>
<tr class="tag-type-<%= Tag.find_by_name(search)&.category.to_i %>">
<td><%= link_to search, posts_path(tags: search) %></td>
<td>
<% unless WikiPage.titled(tags).exists? %>
<% unless WikiPage.titled(search).exists? %>
N
<% end %>
</td>

View File

@@ -13,9 +13,9 @@
</tr>
</thead>
<tbody>
<% @searches.each do |tags, count| %>
<tr class="tag-type-<%= Tag.category_for(tags) %>">
<td><%= link_to tags, posts_path(:tags => tags) %></td>
<% @searches.each do |search, count| %>
<tr class="tag-type-<%= Tag.find_by_name(search)&.category.to_i %>">
<td><%= link_to search, posts_path(tags: search) %></td>
<td style="text-align: right;"><%= count.to_i %></td>
</tr>
<% end %>

View File

@@ -13,16 +13,6 @@ class TagTest < ActiveSupport::TestCase
end
context "A tag category fetcher" do
should "fetch for a single tag" do
FactoryBot.create(:artist_tag, :name => "test")
assert_equal(Tag.categories.artist, Tag.category_for("test"))
end
should "fetch for a single tag with strange markup" do
FactoryBot.create(:artist_tag, :name => "!@$%")
assert_equal(Tag.categories.artist, Tag.category_for("!@$%"))
end
should "fetch for multiple tags" do
FactoryBot.create(:artist_tag, :name => "aaa")
FactoryBot.create(:copyright_tag, :name => "bbb")