tag_set_presenter: refactor split_tag_list_html.

* Make category headers optional.
* Make category order configurable.
* Clean up code.
This commit is contained in:
evazion
2018-04-25 19:49:46 -05:00
parent fdd5ea3e78
commit 442964b0eb
3 changed files with 15 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
class PostPresenter < Presenter
attr_reader :pool, :next_post_in_pool
delegate :tag_list_html, :split_tag_list_html, :inline_tag_list_html, :split_inline_tag_list_html, to: :tag_set_presenter
def self.preview(post, options = {})
if post.nil?
@@ -162,10 +163,6 @@ class PostPresenter < Presenter
categorized_tag_groups.join(" \n")
end
def humanized_categorized_tag_string
categorized_tag_groups.flatten.slice(0, 25).join(", ").tr("_", " ")
end
def safe_mode_message(template)
html = ["This image is unavailable on safe mode (#{Danbooru.config.app_name}). Go to "]
html << template.link_to("Danbooru", "http://danbooru.donmai.us") # XXX don't hardcode.
@@ -193,18 +190,6 @@ class PostPresenter < Presenter
end
end
def tag_list_html(template, options = {})
tag_set_presenter.tag_list_html(template, options.merge(:show_extra_links => CurrentUser.user.is_gold?))
end
def split_tag_list_html(template, options = {})
tag_set_presenter.split_tag_list_html(template, options.merge(:show_extra_links => CurrentUser.user.is_gold?))
end
def inline_tag_list_html(template)
tag_set_presenter.inline_tag_list(template)
end
def has_nav_links?(template)
has_sequential_navigation?(template.params) || @post.pools.undeleted.any? || @post.favorite_groups(active_id=template.params[:favgroup_id]).any?
end

View File

@@ -22,16 +22,16 @@ class TagSetPresenter < Presenter
html.html_safe
end
def split_tag_list_html(template, options = {})
def split_tag_list_html(template, category_list: TagCategory.split_header_list, headers: true, **options)
html = ""
TagCategory.split_header_list.each do |category|
category_list.each do |category|
typetags = typed_tags(category)
if typetags.any?
html << TagCategory.header_mapping[category]
html << TagCategory.header_mapping[category] if headers
html << %{<ul class="#{category}-tag-list">}
typetags.each do |tag|
html << build_list_item(tag, template, options)
html << build_list_item(tag, template, **options)
end
html << "</ul>"
end
@@ -41,7 +41,7 @@ class TagSetPresenter < Presenter
end
# compact (horizontal) list, as seen in the /comments index.
def inline_tag_list(template)
def inline_tag_list_html(template)
@tags.map do |tag_name|
<<-EOS
<span class="category-#{categories[tag_name]}">
@@ -51,7 +51,8 @@ class TagSetPresenter < Presenter
end.join.html_safe
end
private
private
def typed_tags(name)
@typed_tags ||= {}
@typed_tags[name] ||= begin
@@ -72,34 +73,33 @@ private
end
end
def build_list_item(tag, template, options)
def build_list_item(tag, template, name_only: false, humanize_tags: true, show_extra_links: CurrentUser.is_gold?)
html = ""
html << %{<li class="category-#{categories[tag]}">}
current_query = template.params[:tags] || ""
unless options[:name_only]
unless name_only
if categories[tag] == Tag.categories.artist
html << %{<a class="wiki-link" href="/artists/show_or_new?name=#{u(tag)}">?</a> }
else
html << %{<a class="wiki-link" href="/wiki_pages/show_or_new?title=#{u(tag)}">?</a> }
end
if CurrentUser.user.is_gold? && current_query.present?
if show_extra_links && current_query.present?
html << %{<a rel="nofollow" href="/posts?tags=#{u(current_query)}+#{u(tag)}" class="search-inc-tag">+</a> }
html << %{<a rel="nofollow" href="/posts?tags=#{u(current_query)}+-#{u(tag)}" class="search-exl-tag">&ndash;</a> }
end
end
humanized_tag = tag.tr("_", " ")
path = options[:path_prefix] || "/posts"
humanized_tag = humanize_tags ? tag.tr("_", " ") : tag
if categories[tag] == Tag.categories.artist
itemprop = 'itemprop="author"'
else
itemprop = nil
end
html << %{<a class="search-tag" #{itemprop} href="#{path}?tags=#{u(tag)}">#{h(humanized_tag)}</a> }
html << %{<a class="search-tag" #{itemprop} href="/posts?tags=#{u(tag)}">#{h(humanized_tag)}</a> }
unless options[:name_only]
unless name_only
if counts[tag].to_i >= 10_000
post_count = "#{counts[tag].to_i / 1_000}k"
elsif counts[tag].to_i >= 1_000

View File

@@ -49,7 +49,7 @@
<span class="info">
<strong>Tags</strong>
<%= TagSetPresenter.new(upload.tag_string.split).inline_tag_list(self) %>
<%= TagSetPresenter.new(upload.tag_string.split).inline_tag_list_html(self) %>
</span>
</td>
<td>