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

@@ -1,6 +1,6 @@
class PostPresenter
attr_reader :pool, :next_post_in_pool
delegate :tag_list_html, :split_tag_list_html, :split_tag_list_text, :inline_tag_list_html, to: :tag_set_presenter
delegate :split_tag_list_text, to: :tag_set_presenter
def initialize(post)
@post = post

View File

@@ -14,48 +14,6 @@ class TagSetPresenter
@tag_names = tag_names
end
def tag_list_html(current_query: "", show_extra_links: false, name_only: false)
html = ""
if ordered_tags.present?
html << '<ul>'
ordered_tags.each do |tag|
html << build_list_item(tag, current_query: current_query, show_extra_links: show_extra_links, name_only: name_only)
end
html << "</ul>"
end
html.html_safe
end
def split_tag_list_html(headers: true, category_list: TagCategory.split_header_list, current_query: "", show_extra_links: false, name_only: false, humanize_tags: true)
html = ""
category_list.each do |category|
typetags = tags_for_category(category)
if typetags.any?
if headers
html << %{<h3 class="#{category}-tag-list">#{category.capitalize.pluralize(typetags.size)}</h3>}
end
html << %{<ul class="#{category}-tag-list">}
typetags.each do |tag|
html << build_list_item(tag, current_query: current_query, show_extra_links: show_extra_links, name_only: name_only, humanize_tags: humanize_tags)
end
html << "</ul>"
end
end
html.html_safe
end
# compact (horizontal) list, as seen in the /comments index.
def inline_tag_list_html(humanize_tags: false)
html = split_tag_list_html(category_list: TagCategory.categorized_list, headers: false, show_extra_links: false, name_only: true, humanize_tags: humanize_tags)
%{<span class="inline-tag-list">#{html}</span>}.html_safe
end
# the list of tags inside the tag box in the post edit form.
def split_tag_list_text(category_list: TagCategory.categorized_list)
category_list.map do |category|
@@ -104,57 +62,5 @@ class TagSetPresenter
end
end
def build_list_item(tag, name_only: false, humanize_tags: true, show_extra_links: false, current_query: "")
name = tag.name
count = tag.post_count
category = tag.category
html = %{<li class="tag-type-#{tag.category}" data-tag-name="#{h(name)}">}
unless name_only
if tag.artist?
html << %{<a class="wiki-link" href="/artists/show_or_new?name=#{u(name)}">?</a> }
elsif name =~ /\A\d+\z/
html << %{<a class="wiki-link" href="/wiki_pages/~#{u(name)}">?</a> }
else
html << %{<a class="wiki-link" href="/wiki_pages/#{u(name)}">?</a> }
end
if show_extra_links && current_query.present?
html << %{<a href="/posts?tags=#{u(current_query)}+#{u(name)}" class="search-inc-tag">+</a> }
html << %{<a href="/posts?tags=#{u(current_query)}+-#{u(name)}" class="search-exl-tag">&ndash;</a> }
end
end
humanized_tag = humanize_tags ? name.tr("_", " ") : name
html << %{<a class="search-tag" href="/posts?tags=#{u(name)}">#{h(humanized_tag)}</a> }
unless name_only || tag.new_record?
if count >= 10_000
post_count = "#{count / 1_000}k"
elsif count >= 1_000
post_count = format("%.1fk", (count / 1_000.0))
else
post_count = count
end
is_underused_tag = count <= 1 && tag.general?
klass = "post-count#{is_underused_tag ? " low-post-count" : ""}"
html << %{<span class="#{klass}" title="#{count}">#{post_count}</span>}
end
html << "</li>"
html
end
def h(s)
CGI.escapeHTML(s)
end
def u(s)
CGI.escape(s)
end
memoize :tags, :tags_by_category, :ordered_tags, :humanized_essential_tag_string
end