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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -31,7 +31,7 @@
<% if post.has_active_pools? %> <% if post.has_active_pools? %>
<li><strong>Pools</strong>: <%= render "pools/inline_list", pools: post.pools.undeleted %></li> <li><strong>Pools</strong>: <%= render "pools/inline_list", pools: post.pools.undeleted %></li>
<% end %> <% 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> </ul>
</section> </section>
</div> </div>

View File

@@ -9,7 +9,7 @@
<section id="tag-box"> <section id="tag-box">
<h1>Tags</h1> <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> </section>
<%= render "posts/partials/index/options" %> <%= render "posts/partials/index/options" %>

View File

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

View File

@@ -6,7 +6,7 @@
<%= render "posts/partials/index/blacklist" %> <%= render "posts/partials/index/blacklist" %>
<section id="tag-list"> <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>
<section id="post-information"> <section id="post-information">

View File

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