tags: refactor tag lists to use ViewComponent.

This commit is contained in:
evazion
2021-01-30 14:01:07 -06:00
parent 9d60046f1d
commit 1f637867a4
25 changed files with 256 additions and 131 deletions

View File

@@ -0,0 +1,51 @@
# frozen_string_literal: true
class TagListComponent < ApplicationComponent
attr_reader :tags, :current_query, :show_extra_links
def initialize(tags: [], current_query: nil, show_extra_links: false)
@tags = tags
@current_query = current_query
@show_extra_links = show_extra_links
end
def self.tags_from_names(tag_names)
names_to_tags = Tag.where(name: tag_names).map { |tag| [tag.name, tag] }.to_h
tag_names.map do |name|
names_to_tags.fetch(name) { Tag.new(name: name).freeze }
end
end
def categorized_tags(categories, &block)
return to_enum(:categorized_tags, categories) unless block_given?
categories.each do |category|
tags = tags_for_category(category)
yield category, tags if tags.present?
end
end
def tags_for_category(category_name)
category = TagCategory.mapping[category_name.downcase]
tags_by_category[category] || []
end
def tags_by_category
@tags_by_category ||= tags.sort_by(&:name).group_by(&:category)
end
def is_underused_tag?(tag)
tag.post_count <= 1 && tag.general?
end
def humanized_post_count(tag)
if tag.post_count >= 10_000
"#{tag.post_count / 1_000}k"
elsif tag.post_count >= 1_000
"%.1fk" % (tag.post_count / 1_000.0)
else
tag.post_count.to_s
end
end
end

View File

@@ -0,0 +1,30 @@
<div class="tag-list categorized-tag-list">
<% categorized_tags(TagCategory.split_header_list).each do |category_name, tags| %>
<h3 class="<%= category_name %>-tag-list">
<%= category_name.capitalize.pluralize(tags) %>
</h3>
<ul class="<%= category_name %>-tag-list">
<% tags.each do |t| %>
<li class="tag-type-<%= t.category %>" data-tag-name="<%= t.name %>">
<% if t.artist? %>
<%= link_to "?", show_or_new_artists_path(t.name), class: "wiki-link" %>
<% elsif t.name =~ /\A\d+\z/ %>
<%= link_to "?", wiki_page_path("~#{t.name}"), class: "wiki-link" %>
<% else %>
<%= link_to "?", wiki_page_path(t.name), class: "wiki-link" %>
<% end %>
<% if show_extra_links && current_query.present? %>
<%= link_to "+", posts_path(tags: "#{current_query} #{t.name}"), class: "search-inc-tag" %>
<%= link_to "-", posts_path(tags: "#{current_query} -#{t.name}"), class: "search-exl-tag" %>
<% end %>
<%= link_to t.pretty_name, posts_path(tags: t.name), class: "search-tag" %>
<%= tag.span humanized_post_count(t), class: ["post-count", ("low-post-count" if is_underused_tag?(t))], title: t.post_count %>
</li>
<% end %>
</ul>
<% end %>
</div>

View File

@@ -0,0 +1,7 @@
<div class="tag-list inline-tag-list">
<% categorized_tags(TagCategory.categorized_list).each do |category_name, tags| %>
<% tags.each do |t| %>
<%= link_to t.name, posts_path(tags: t.name), class: "search-tag tag-type-#{t.category}", "data-tag-name": t.name %>
<% end %>
<% end %>
</div>

View File

@@ -0,0 +1,22 @@
<ul class="tag-list search-tag-list">
<% tags.each do |t| %>
<li class="tag-type-<%= t.category %>" data-tag-name="<%= t.name %>">
<%# ignore search:foo metatags %>
<% if t.artist? %>
<%= link_to "?", show_or_new_artists_path(t.name), class: "wiki-link" %>
<% elsif t.name =~ /\A\d+\z/ %>
<%= link_to "?", wiki_page_path("~#{t.name}"), class: "wiki-link" %>
<% else %>
<%= link_to "?", wiki_page_path(t.name), class: "wiki-link" %>
<% end %>
<% if show_extra_links && current_query.present? %>
<%= link_to "+", posts_path(tags: "#{current_query} #{t.name}"), class: "search-inc-tag" %>
<%= link_to "-", posts_path(tags: "#{current_query} -#{t.name}"), class: "search-exl-tag" %>
<% end %>
<%= link_to t.pretty_name, posts_path(tags: t.name), class: "search-tag" %>
<%= tag.span humanized_post_count(t), class: "post-count", title: t.post_count %>
</li>
<% end %>
</ul>

View File

@@ -0,0 +1,5 @@
<div class="tag-list simple-tag-list">
<% tags.each do |t| %>
<%= link_to t.pretty_name, posts_path(tags: t.name), class: "search-tag tag-type-#{t.category}", "data-tag-name": t.name %>
<% end %>
</div>

View File

@@ -0,0 +1,25 @@
.tag-list {
a.search-tag {
overflow-wrap: normal;
}
&.inline-tag-list {
display: inline;
a {
margin-right: 0.5em;
}
}
&.categorized-tag-list {
ul {
margin-bottom: 1em;
}
}
&.simple-tag-list {
a {
display: block;
}
}
}