From 7e8f859b24d381f00058afd687c07cf31c653f6c Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 27 Dec 2020 21:03:26 -0600 Subject: [PATCH] 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. --- app/logical/post_query_builder.rb | 5 +++++ app/logical/related_tag_query.rb | 7 +++++-- app/models/tag.rb | 12 ------------ app/views/explore/posts/missed_searches.html.erb | 8 ++++---- app/views/explore/posts/searches.html.erb | 6 +++--- test/unit/tag_test.rb | 10 ---------- 6 files changed, 17 insertions(+), 31 deletions(-) diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 4046485f1..a694e7f75 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -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 diff --git a/app/logical/related_tag_query.rb b/app/logical/related_tag_query.rb index 6a86b10b0..fb6c71413 100644 --- a/app/logical/related_tag_query.rb +++ b/app/logical/related_tag_query.rb @@ -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 [] diff --git a/app/models/tag.rb b/app/models/tag.rb index 527f8a797..c83d4e9b1 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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| diff --git a/app/views/explore/posts/missed_searches.html.erb b/app/views/explore/posts/missed_searches.html.erb index 329b66c43..1bad6c802 100644 --- a/app/views/explore/posts/missed_searches.html.erb +++ b/app/views/explore/posts/missed_searches.html.erb @@ -15,11 +15,11 @@ - <% @missed_searches.each do |tags, count| %> - - <%= link_to tags, posts_path(:tags => tags) %> + <% @missed_searches.each do |search, count| %> + + <%= link_to search, posts_path(tags: search) %> - <% unless WikiPage.titled(tags).exists? %> + <% unless WikiPage.titled(search).exists? %> N <% end %> diff --git a/app/views/explore/posts/searches.html.erb b/app/views/explore/posts/searches.html.erb index 5256427eb..a00154e61 100644 --- a/app/views/explore/posts/searches.html.erb +++ b/app/views/explore/posts/searches.html.erb @@ -13,9 +13,9 @@ - <% @searches.each do |tags, count| %> - - <%= link_to tags, posts_path(:tags => tags) %> + <% @searches.each do |search, count| %> + + <%= link_to search, posts_path(tags: search) %> <%= count.to_i %> <% end %> diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index 0c4bd3dce..a638dc9a9 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -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")