From c0376ebb51898736400732735d06e94bb503a0ca Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 3 Mar 2020 03:34:34 -0600 Subject: [PATCH] tags/index: don't show aliases when search matches primary tag. When searching e.g. "*mask*", don't show "surgical_mask <- medical_mask" because "*mask*" already matches the primary tag. --- app/helpers/tags_helper.rb | 8 ++++++++ app/views/tags/index.html.erb | 4 +--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb index d6e8a9728..53f0e01e7 100644 --- a/app/helpers/tags_helper.rb +++ b/app/helpers/tags_helper.rb @@ -3,4 +3,12 @@ module TagsHelper return nil if tag.blank? "tag-type-#{tag.category}" end + + def tag_alias_for_pattern(tag, pattern) + return nil if pattern.blank? + + tag.consequent_aliases.find do |tag_alias| + !tag.name.ilike?(pattern) && tag_alias.antecedent_name.ilike?(pattern) + end + end end diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index 86b138ee0..ad0c67e75 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -10,9 +10,7 @@ <%= link_to_wiki "?", tag.name, class: tag_class(tag) %> <%= link_to tag.name, posts_path(tags: tag.name), class: tag_class(tag) %> - <% pattern = params.dig(:search, :name_or_alias_matches).to_s %> - <% tag_alias = tag.consequent_aliases.find { |tag_alias| tag_alias.antecedent_name.ilike?(pattern) } %> - + <% tag_alias = tag_alias_for_pattern(tag, params[:search][:name_or_alias_matches]) %> <% if tag_alias.present? %> ← <%= link_to tag_alias.antecedent_name, tag_alias, class: "fineprint" %> <% end %>