From d600a09762ed5ddcb43727083adf607affa34a13 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Fri, 29 Sep 2023 19:08:01 +0200 Subject: [PATCH] More verbose but "lazier" code --- app/models/common_tagging.rb | 8 ++++++-- app/models/tag.rb | 27 ++++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/models/common_tagging.rb b/app/models/common_tagging.rb index 58c2dd33e05..2d065582163 100644 --- a/app/models/common_tagging.rb +++ b/app/models/common_tagging.rb @@ -32,11 +32,15 @@ def update_wrangler end def add_to_autocomplete - common_tag.add_to_fandom_autocomplete(filterable) + if filterable.is_a?(Fandom) && common_tag.eligible_for_fandom_autocomplete? + common_tag.add_to_fandom_autocomplete(filterable) + end end def remove_from_autocomplete - common_tag&.remove_from_fandom_autocomplete(filterable) + if filterable.is_a?(Fandom) && common_tag&.was_eligible_for_fandom_autocomplete? + common_tag.remove_from_fandom_autocomplete(filterable) + end end # A relationship should inherit its characters' fandoms diff --git a/app/models/tag.rb b/app/models/tag.rb index 6b5c6cda0e5..14a92257a80 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -471,35 +471,40 @@ def autocomplete_prefixes end def add_to_autocomplete(score = nil) - parents.each do |parent| - add_to_fandom_autocomplete(parent, score) + if eligible_for_fandom_autocomplete? + parents.each do |parent| + add_to_fandom_autocomplete(parent, score) if parent.is_a?(Fandom) + end end super end def add_to_fandom_autocomplete(fandom, score = nil) - return unless canonical - return unless fandom.is_a?(Fandom) - return unless self.is_a?(Character) || self.is_a?(Relationship) - score ||= autocomplete_score REDIS_AUTOCOMPLETE.zadd(self.transliterate("autocomplete_fandom_#{fandom.name.downcase}_#{type.downcase}"), score, autocomplete_value) end def remove_from_autocomplete super - parents.each do |parent| - remove_from_fandom_autocomplete(parent) + if was_eligible_for_fandom_autocomplete? + parents.each do |parent| + remove_from_fandom_autocomplete(parent) if parent.is_a?(Fandom) + end end end def remove_from_fandom_autocomplete(fandom) - return unless fandom.is_a?(Fandom) - return unless self.is_a?(Character) || self.is_a?(Relationship) - REDIS_AUTOCOMPLETE.zrem(self.transliterate("autocomplete_fandom_#{fandom.name.downcase}_#{type.downcase}"), autocomplete_value) end + def eligible_for_fandom_autocomplete? + return (self.is_a?(Character) || self.is_a?(Relationship)) && canonical + end + + def was_eligible_for_fandom_autocomplete? + return (self.is_a?(Character) || self.is_a?(Relationship)) + end + def remove_stale_from_autocomplete super if self.is_a?(Character) || self.is_a?(Relationship)