TagSetPresenter: refactor to pass options explicitly.

Refactor tag_list_html, split_tag_list_html, and inline_tag_list_html to
take the `show_extra_links` and `current_query` options explicitly,
rather than implicitly relying on CurrentUser or taking `params[:tags]`
from the template.
This commit is contained in:
evazion
2018-09-30 11:32:30 -05:00
parent 2cc4e35cc9
commit 99632d5e8a
12 changed files with 36 additions and 44 deletions

View File

@@ -1,7 +1,8 @@
module PostSetPresenters
class Favorite < Base
attr_accessor :post_set, :tag_set_presenter
delegate :favorites, :to => :post_set
delegate :favorites, :posts, :to => :post_set
delegate :tag_list_html, to: :tag_set_presenter
def initialize(post_set)
@post_set = post_set
@@ -11,13 +12,5 @@ module PostSetPresenters
).map {|x| x[0]}
)
end
def tag_list_html(template)
tag_set_presenter.tag_list_html(template)
end
def posts
@posts ||= post_set.posts
end
end
end

View File

@@ -12,10 +12,6 @@ module PostSetPresenters
)
end
def tag_list_html(template)
tag_set_presenter.tag_list_html(template)
end
def post_previews_html(template)
html = ""

View File

@@ -67,12 +67,8 @@ module PostSetPresenters
SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"}
end
def tag_list_html(template, options = {})
if post_set.is_saved_search?
options[:name_only] = true
end
tag_set_presenter.tag_list_html(template, options)
def tag_list_html(**options)
tag_set_presenter.tag_list_html(name_only: post_set.is_saved_search?, **options)
end
end
end

View File

@@ -5,16 +5,18 @@
=end
class TagSetPresenter < Presenter
def initialize(tags)
@tags = tags
attr_reader :tag_names, :tags
def initialize(tag_names)
@tag_names = tag_names
end
def tag_list_html(template, options = {})
def tag_list_html(current_query: "", show_extra_links: false, name_only: false)
html = ""
if @tags.present?
if tag_names.present?
html << '<ul itemscope itemtype="http://schema.org/ImageObject">'
@tags.each do |tag|
html << build_list_item(tag, template, options)
tag_names.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
@@ -22,7 +24,7 @@ class TagSetPresenter < Presenter
html.html_safe
end
def split_tag_list_html(template, category_list: TagCategory.split_header_list, headers: true, **options)
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|
@@ -31,7 +33,7 @@ class TagSetPresenter < Presenter
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, current_query: current_query, show_extra_links: show_extra_links, name_only: name_only, humanize_tags: humanize_tags)
end
html << "</ul>"
end
@@ -41,9 +43,9 @@ class TagSetPresenter < Presenter
end
# compact (horizontal) list, as seen in the /comments index.
def inline_tag_list_html(template, classes: "inline-tag-list", **options)
html = split_tag_list_html(template, category_list: TagCategory.categorized_list, headers: false, name_only: true, humanize_tags: true, **options)
template.tag.span(html, class: classes)
def inline_tag_list_html(humanize_tags: true)
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
private
@@ -51,27 +53,25 @@ class TagSetPresenter < Presenter
def typed_tags(name)
@typed_tags ||= {}
@typed_tags[name] ||= begin
@tags.select do |tag|
tag_names.select do |tag|
categories[tag] == TagCategory.mapping[name]
end
end
end
def categories
@categories ||= Tag.categories_for(@tags)
@categories ||= Tag.categories_for(tag_names)
end
def counts
@counts ||= Tag.counts_for(@tags).inject({}) do |hash, x|
@counts ||= Tag.counts_for(tag_names).inject({}) do |hash, x|
hash[x["name"]] = x["post_count"]
hash
end
end
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] || ""
def build_list_item(tag, name_only: false, humanize_tags: true, show_extra_links: false, current_query: "")
html = %{<li class="category-#{categories[tag]}">}
unless name_only
if categories[tag] == Tag.categories.artist

View File

@@ -1,5 +1,12 @@
class UploadPresenter < Presenter
attr_reader :upload
delegate :inline_tag_list_html, to: :tag_set_presenter
def initialize(upload)
@upload = upload
end
def tag_set_presenter
@tag_set_presenter ||= TagSetPresenter.new(upload.tag_string.split)
end
end

View File

@@ -26,7 +26,7 @@
</div>
<div class="row list-of-tags">
<strong>Tags</strong>
<%= post.presenter.inline_tag_list_html(self) %>
<%= post.presenter.inline_tag_list_html %>
</div>
</div>

View File

@@ -9,7 +9,7 @@
<section id="tag-box">
<h1>Tags</h1>
<%= @favorite_set.presenter.tag_list_html(self) %>
<%= @favorite_set.presenter.tag_list_html %>
</section>
<section id="related-box">

View File

@@ -31,7 +31,7 @@
<% if post.has_active_pools? %>
<li><strong>Pools</strong>: <%= render "pools/inline_list", pools: post.pools.undeleted %></li>
<% end %>
<li><strong>Tags</strong>: <%= post.presenter.inline_tag_list_html(self) %></li>
<li><strong>Tags</strong>: <%= post.presenter.inline_tag_list_html %></li>
</ul>
</section>
</div>

View File

@@ -9,7 +9,7 @@
<section id="tag-box">
<h1>Tags</h1>
<%= @post_set.presenter.tag_list_html(self) %>
<%= @post_set.presenter.tag_list_html(current_query: params[:tags], show_extra_links: CurrentUser.user.is_gold?) %>
</section>
<%= render "posts/partials/index/options" %>

View File

@@ -46,5 +46,5 @@
<% end %>
</div>
<%= @post.presenter.inline_tag_list_html(self, humanize_tags: false) %>
<%= @post.presenter.inline_tag_list_html(humanize_tags: false) %>
</div>

View File

@@ -6,7 +6,7 @@
<%= render "posts/partials/index/blacklist" %>
<section id="tag-list">
<%= @post.presenter.split_tag_list_html(self) %>
<%= @post.presenter.split_tag_list_html(current_query: params[:tags], show_extra_links: CurrentUser.user.is_gold?) %>
</section>
<section id="post-information">

View File

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