From 416f817f6d10d8bca9fa6bd446a4a69d9cb21bf7 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 4 Sep 2019 23:56:12 -0500 Subject: [PATCH] Post.fast_count: fix incorrect counts for aliased tags. Revert optimization from a6163258b. Turns out that we have to resolve aliases in fast_count, otherwise for aliased tags we'll return an empty count. Fixes #4156. --- app/models/post.rb | 2 +- test/unit/post_test.rb | 6 ++++++ test/unit/related_tag_calculator_test.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index e53ff2f99..bac56349b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1115,7 +1115,7 @@ class Post < ApplicationRecord tags = tags.to_s tags += " rating:s" if CurrentUser.safe_mode? tags += " -status:deleted" if CurrentUser.hide_deleted_posts? && !Tag.has_metatag?(tags, "status", "-status") - tags = Tag.normalize_query(tags, normalize_aliases: false) + tags = Tag.normalize_query(tags) # optimize some cases. these are just estimates but at these # quantities being off by a few hundred doesn't matter much diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 061ab978d..b118e7de2 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -2550,6 +2550,12 @@ class PostTest < ActiveSupport::TestCase end end + context "an aliased tag" do + should "return the count of the consequent tag" do + assert_equal(Post.fast_count("aaa"), Post.fast_count("alias")) + end + end + context "a single metatag" do should "return the correct cached count" do FactoryBot.build(:tag, name: "score:42", post_count: -100).save(validate: false) diff --git a/test/unit/related_tag_calculator_test.rb b/test/unit/related_tag_calculator_test.rb index 58f3071dc..7ebc05f72 100644 --- a/test/unit/related_tag_calculator_test.rb +++ b/test/unit/related_tag_calculator_test.rb @@ -61,6 +61,14 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase assert_equal(%w[1girl 1boy solo], RelatedTagCalculator.similar_tags_for_search("rating:q").pluck(:name)) assert_equal(%w[solo 1girl], RelatedTagCalculator.similar_tags_for_search("solo").pluck(:name)) end + + should "calculate the similar tags for an aliased tag" do + create(:tag_alias, antecedent_name: "rabbit", consequent_name: "bunny") + create(:post, tag_string: "bunny dog") + create(:post, tag_string: "bunny cat") + + assert_equal(%w[bunny cat dog], RelatedTagCalculator.similar_tags_for_search("rabbit").pluck(:name)) + end end end end