views: replace .category-N css classes with .tag-type-N

* Replace the .category-N CSS classes on tags with .tag-type-N. Before
  we were inconsistent about whether tag colors were indicated with
  .category-N or .tag-type-N. Now it's always .tag-type-N.

* Fix various places to not use Tag.category_for. Tag.category_for does
  one Redis call per tag lookup, which leads to N Redis calls on many
  pages. This was inefficient because usually we either already had the
  tags from the database, or we could fetch them easily.
This commit is contained in:
evazion
2020-02-07 18:08:01 -06:00
parent 998eece95d
commit 7e67d3dd9c
21 changed files with 38 additions and 37 deletions

View File

@@ -3,7 +3,7 @@ class DtextLinksController < ApplicationController
def index
@dtext_links = DtextLink.paginated_search(params)
@dtext_links = @dtext_links.includes(:model) if request.format.html?
@dtext_links = @dtext_links.includes(linked_wiki: :tag, model: :tag) if request.format.html?
respond_with(@dtext_links)
end

View File

@@ -0,0 +1,6 @@
module TagsHelper
def tag_class(tag)
return nil if tag.blank?
"tag-type-#{tag.category}"
end
end

View File

@@ -1,7 +1,7 @@
<% TagCategory.css_mapping.each do |category,cssmap| %>
.category-<%= category %> a,
.tag-type-<%= category %> a,
a.tag-type-<%= category %>,
.ui-widget-content .category-<%= category %> a,
.ui-widget-content .tag-type-<%= category %> a,
.ui-widget-content a.tag-type-<%= category %> {
color: <%= cssmap["color"] %>;

View File

@@ -354,10 +354,6 @@ class Artist < ApplicationRecord
end
module TagMethods
def category_name
Tag.category_for(name)
end
def validate_tag_category
return unless is_active? && name_changed?

View File

@@ -1,5 +1,7 @@
class DtextLink < ApplicationRecord
belongs_to :model, polymorphic: true
belongs_to :linked_wiki, primary_key: :title, foreign_key: :link_target, class_name: "WikiPage", optional: true
enum link_type: [:wiki_link, :external_link]
before_validation :normalize_link_target

View File

@@ -38,10 +38,6 @@ class WikiPage < ApplicationRecord
where("is_deleted = false")
end
def recent
order("updated_at DESC").limit(25)
end
def other_names_include(name)
name = normalize_other_name(name)
subquery = WikiPage.from("unnest(other_names) AS other_name").where_iequals("other_name", name)
@@ -177,7 +173,7 @@ class WikiPage < ApplicationRecord
end
def category_name
Tag.category_for(title)
tag&.category
end
def pretty_title

View File

@@ -3,6 +3,7 @@ class WikiPageVersion < ApplicationRecord
belongs_to :wiki_page
belongs_to_updater
belongs_to :artist, optional: true
belongs_to :tag, primary_key: :name, foreign_key: :title, optional: true
module SearchMethods
def search(params)
@@ -51,10 +52,6 @@ class WikiPageVersion < ApplicationRecord
!is_deleted && previous.is_deleted
end
def category_name
Tag.category_for(title)
end
def self.available_includes
[:updater, :wiki_page, :artist]
end

View File

@@ -107,7 +107,7 @@ class TagSetPresenter < Presenter
count = tag.post_count
category = tag.category
html = %{<li class="category-#{tag.category}">}
html = %{<li class="tag-type-#{tag.category}">}
unless name_only
if category == Tag.categories.artist

View File

@@ -3,7 +3,7 @@
<div id="c-artists">
<div id="a-show">
<h1>Artist: <%= link_to @artist.pretty_name, posts_path(:tags => @artist.name), :class => "tag-type-#{@artist.category_name}" %></h1>
<h1>Artist: <%= link_to @artist.pretty_name, posts_path(tags: @artist.name), class: tag_class(@artist.tag) %></h1>
<% if @artist.notes.present? %>
<div class="prose">

View File

@@ -12,7 +12,7 @@
<%= table_for @dtext_links, class: "striped autofit" do |t| %>
<% t.column "Page" do |dtext_link| %>
<% if dtext_link.model_type == "WikiPage" %>
<span class="category-<%= Tag.category_for(dtext_link.model.title) %>">
<span class="<%= tag_class(dtext_link.model.tag) %>">
<%= link_to(dtext_link.model.title, dtext_link.model) %>
<%= link_to("»", dtext_links_path(search: { model_type: "WikiPage", model: { title: dtext_link.model.title }})) %>
</span>
@@ -25,7 +25,7 @@
<% if dtext_link.external_link? %>
<%= external_link_to(dtext_link.link_target) %>
<% elsif dtext_link.wiki_link? %>
<%= link_to_wiki dtext_link.link_target, class: "tag-type-#{Tag.category_for(dtext_link.link_target)}" %>
<%= link_to_wiki dtext_link.link_target, class: tag_class(dtext_link.linked_wiki&.tag) %>
<% end %>
<%= link_to("»", dtext_links_path(search: { link_target: dtext_link.link_target })) %>

View File

@@ -16,7 +16,7 @@
</thead>
<tbody>
<% @search_service.each_search do |tags, count| %>
<tr class="category-<%= Tag.category_for(tags) %>">
<tr class="tag-type-<%= Tag.category_for(tags) %>">
<td><%= link_to tags, posts_path(:tags => tags) %></td>
<td>
<% unless WikiPage.titled(tags).exists? %>

View File

@@ -14,7 +14,7 @@
</thead>
<tbody>
<% @search_service.each_search do |tags, count| %>
<tr class="category-<%= Tag.category_for(tags) %>">
<tr class="tag-type-<%= Tag.category_for(tags) %>">
<td><%= link_to tags, posts_path(:tags => tags) %></td>
<td style="text-align: right;"><%= count.to_i %></td>
</tr>

View File

@@ -14,8 +14,8 @@
<% if params.dig(:search, :query).present? %>
<%= table_for @query.tags do |t| %>
<% t.column "Name" do |tag| %>
<%= link_to_wiki "?", tag.name, class: "tag-type-#{tag.category}" %>
<%= link_to tag.name, posts_path(tags: tag.name), class: "tag-type-#{tag.category}" %>
<%= link_to_wiki "?", tag.name, class: tag_class(tag) %>
<%= link_to tag.name, posts_path(tags: tag.name), class: tag_class(tag) %>
<% end %>
<% end %>
<% end %>

View File

@@ -19,7 +19,7 @@
<% else %>
(<ul id="source-info-translated-artists">
<% @source.artists.each do |artist| %>
<li><%= link_to artist.name, artist_path(artist), class: "tag-type-#{artist.category_name}" %></li>
<li><%= link_to artist.name, artist_path(artist), class: tag_class(artist.tag) %></li>
<% end %>
</ul>)
<% end %>

View File

@@ -1,9 +1,11 @@
<%= table_for tag_aliases, width: "100%" do |t| %>
<% t.column "From", width: "25%" do |tag_alias| %>
<span class="category-<%= tag_alias.antecedent_tag.try(:category) %>"><%= link_to tag_alias.antecedent_name, posts_path(:tags => tag_alias.antecedent_name) %> <span class="count"><%= tag_alias.antecedent_tag.post_count rescue 0 %></span></span>
<%= link_to tag_alias.antecedent_name, posts_path(tags: tag_alias.antecedent_name), class: tag_class(tag_alias.antecedent_tag) %>
<span class="count"><%= tag_alias.antecedent_tag&.post_count.to_i %></span>
<% end %>
<% t.column "To", width: "25%" do |tag_alias| %>
<span class="category-<%= tag_alias.consequent_tag.try(:category) %>"><%= link_to tag_alias.consequent_name, posts_path(:tags => tag_alias.consequent_name) %> <span class="count"><%= tag_alias.consequent_tag.post_count rescue 0 %></span></span>
<%= link_to tag_alias.consequent_name, posts_path(tags: tag_alias.consequent_name), class: tag_class(tag_alias.consequent_tag) %>
<span class="count"><%= tag_alias.consequent_tag&.post_count.to_i %></span>
<% end %>
<% t.column "Reference", width: "10%" do |tag_alias| %>
<% if tag_alias.forum_topic_id %>

View File

@@ -1,9 +1,11 @@
<%= table_for tag_implications, width: "100%" do |t| %>
<% t.column "From", width: "25%" do |tag_implication| %>
<span class="category-<%= tag_implication.antecedent_tag.try(:category) %>"><%= link_to tag_implication.antecedent_name, posts_path(:tags => tag_implication.antecedent_name) %> <span class="count"><%= tag_implication.antecedent_tag.post_count rescue 0 %></span></span>
<%= link_to tag_implication.antecedent_name, posts_path(tags: tag_implication.antecedent_name), class: tag_class(tag_implication.antecedent_tag) %>
<span class="count"><%= tag_implication.antecedent_tag&.post_count.to_i %></span>
<% end %>
<% t.column "To", width: "25%" do |tag_implication| %>
<span class="category-<%= tag_implication.consequent_tag.try(:category) %>"><%= link_to tag_implication.consequent_name, posts_path(:tags => tag_implication.consequent_name) %> <span class="count"><%= tag_implication.consequent_tag.post_count rescue 0 %></span></span>
<%= link_to tag_implication.consequent_name, posts_path(tags: tag_implication.consequent_name), class: tag_class(tag_implication.consequent_tag) %>
<span class="count"><%= tag_implication.consequent_tag&.post_count.to_i %></span>
<% end %>
<% t.column "Reference", width: "10%" do |tag_implication| %>
<% if tag_implication.forum_topic_id %>

View File

@@ -7,8 +7,8 @@
<%= table_for @tags, {class: "striped autofit"} do |t| %>
<% t.column :post_count, name: "Count" %>
<% t.column "Name", td: {class: "col-expand"} do |tag| %>
<%= link_to_wiki "?", tag.name, class: "tag-type-#{tag.category}" %>
<%= link_to tag.name, posts_path(tags: tag.name), class: "tag-type-#{tag.category}" %>
<%= link_to_wiki "?", tag.name, class: tag_class(tag) %>
<%= link_to tag.name, posts_path(tags: tag.name), class: tag_class(tag) %>
<% end %>
<% t.column column: "control" do |tag| %>
<%= link_to_if tag.editable_by?(CurrentUser.user), "Edit", edit_tag_path(tag) %> |

View File

@@ -1,6 +1,6 @@
<div id="p-<%= listing_type(:wiki_page_id, member_check: false, types: [:page, :global]) %>-listing">
<%= form_tag(diff_wiki_page_versions_path, :method => :get) do %>
<%= table_for @wiki_page_versions, width: "100%" do |t| %>
<%= table_for @wiki_page_versions.includes(:updater, :tag), width: "100%" do |t| %>
<% t.column column: "diff", width: "3%" do |wiki_page_version, i| %>
<%= link_to_if wiki_page_version.previous.present?, "diff", diff_wiki_page_versions_path(otherpage: wiki_page_version.previous.try(:id), thispage: wiki_page_version.id) %>
<% end %>
@@ -15,7 +15,7 @@
<% end %>
<% t.column "Title" do |wiki_page_version| %>
<span class="category-<%= wiki_page_version.category_name %>">
<span class="<%= tag_class(wiki_page_version.tag) %>">
<%= link_to "?", wiki_page_path(wiki_page_version.wiki_page_id) %>
<%= link_to wiki_page_version.title, wiki_page_version %>
<%= link_to "»", wiki_page_versions_path(search: { wiki_page_id: wiki_page_version.wiki_page_id }) %>

View File

@@ -1,8 +1,8 @@
<section>
<h1>Recent Changes (<%= link_to "all", wiki_page_versions_path %>)</h1>
<ul>
<% WikiPage.recent.each do |page| %>
<li class="category-<%= page.category_name %>"><%= link_to page.pretty_title, wiki_page_path(page) %></li>
<% WikiPage.order(updated_at: :desc).includes(:tag).limit(25).each do |page| %>
<li><%= link_to page.pretty_title, wiki_page_path(page), class: tag_class(page.tag) %></li>
<% end %>
</ul>
</section>

View File

@@ -5,7 +5,7 @@
<%= table_for @wiki_pages, width: "100%" do |t| %>
<% t.column "Title" do |wiki_page| %>
<span class="category-<%= wiki_page.category_name %>"><%= link_to_wiki wiki_page.title %></span>
<span class="<%= tag_class(wiki_page.tag) %>"><%= link_to_wiki wiki_page.title %></span>
<% end %>
<% t.column "Last edited" do |wiki_page| %>
<%= time_ago_in_words_tagged(wiki_page.updated_at) %>

View File

@@ -6,7 +6,7 @@
<% content_for(:content) do %>
<h1 id="wiki-page-title">
<%= link_to @wiki_page.pretty_title, posts_path(:tags => @wiki_page.title), :class => "tag-type-#{@wiki_page.category_name}" %>
<%= link_to @wiki_page.pretty_title, posts_path(tags: @wiki_page.title), class: tag_class(@wiki_page.tag) %>
<% if @wiki_page.is_locked? %>
(locked)