From a2814364eefe57844fbe4b3af057d7e6704ecec4 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 10 May 2020 19:18:18 -0500 Subject: [PATCH] presenters: merge PostSetPresenter into PostSet. Reduce indirection. PostSet is basically a collection of helper methods for rendering the post index page. PostSetPresenter was a set of helper methods for rendering the tag list on the post index page. These don't need to be separated. --- app/logical/post_sets/post.rb | 76 ++++++++++++++++++- app/presenters/post_set_presenters/base.rb | 29 ------- app/presenters/post_set_presenters/post.rb | 62 --------------- app/views/posts/index.html.erb | 2 +- .../posts/partials/index/_posts.html.erb | 2 +- 5 files changed, 74 insertions(+), 97 deletions(-) delete mode 100644 app/presenters/post_set_presenters/base.rb delete mode 100644 app/presenters/post_set_presenters/post.rb diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index eff41c0cd..c30126d79 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -1,6 +1,8 @@ module PostSets class Post MAX_PER_PAGE = 200 + MAX_SIDEBAR_TAGS = 25 + attr_reader :page, :random, :post_count, :format, :tag_string, :query def initialize(tags, page = 1, per_page = nil, random: false, format: "html") @@ -118,10 +120,6 @@ module PostSets [page.to_i, 1].max end - def presenter - @presenter ||= ::PostSetPresenters::Post.new(self) - end - def best_post # be smarter about this in the future posts.reject(&:is_deleted).select(&:visible?).max_by(&:fav_count) @@ -131,5 +129,75 @@ module PostSets return BulkUpdateRequest.none unless tag.present? @pending_bulk_update_requests ||= BulkUpdateRequest.pending.where_array_includes_any(:tags, tag.name) end + + def post_previews_html(template, show_cropped: true, **options) + html = "" + if none_shown(options) + return template.render("post_sets/blank") + end + + posts.each do |post| + html << PostPresenter.preview(post, options.merge(:tags => tag_string)) + html << "\n" + end + + html.html_safe + end + + def not_shown(post, options) + !options[:show_deleted] && post.is_deleted? && tag_string !~ /status:(?:all|any|deleted|banned)/ + end + + def none_shown(options) + posts.reject {|post| not_shown(post, options) }.empty? + end + + concerning :TagListMethods do + def related_tags + if query.is_wildcard_search? + wildcard_tags + elsif query.is_metatag?(:search) + saved_search_tags + elsif query.is_empty_search? || query.is_metatag?(:order, :rank) + popular_tags + elsif query.is_single_term? + similar_tags + else + frequent_tags + end + end + + def popular_tags + if PopularSearchService.enabled? + PopularSearchService.new(Date.today).tags + else + frequent_tags + end + end + + def similar_tags + RelatedTagCalculator.cached_similar_tags_for_search(query, MAX_SIDEBAR_TAGS) + end + + def frequent_tags + RelatedTagCalculator.frequent_tags_for_post_array(posts).take(MAX_SIDEBAR_TAGS) + end + + def wildcard_tags + Tag.wildcard_matches(tag_string) + end + + def saved_search_tags + ["search:all"] + SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"} + end + + def tag_set_presenter + @tag_set_presenter ||= TagSetPresenter.new(related_tags.take(MAX_SIDEBAR_TAGS)) + end + + def tag_list_html(**options) + tag_set_presenter.tag_list_html(name_only: query.is_metatag?(:search), **options) + end + end end end diff --git a/app/presenters/post_set_presenters/base.rb b/app/presenters/post_set_presenters/base.rb deleted file mode 100644 index c99c8cbbb..000000000 --- a/app/presenters/post_set_presenters/base.rb +++ /dev/null @@ -1,29 +0,0 @@ -module PostSetPresenters - class Base - def posts - raise NotImplementedError - end - - def post_previews_html(template, options = {}) - html = "" - if none_shown(options) - return template.render("post_sets/blank") - end - - posts.each do |post| - html << PostPresenter.preview(post, options.merge(:tags => @post_set.tag_string)) - html << "\n" - end - - html.html_safe - end - - def not_shown(post, options) - !options[:show_deleted] && post.is_deleted? && @post_set.tag_string !~ /status:(?:all|any|deleted|banned)/ - end - - def none_shown(options) - posts.reject {|post| not_shown(post, options) }.empty? - end - end -end diff --git a/app/presenters/post_set_presenters/post.rb b/app/presenters/post_set_presenters/post.rb deleted file mode 100644 index 3e7f25fc8..000000000 --- a/app/presenters/post_set_presenters/post.rb +++ /dev/null @@ -1,62 +0,0 @@ -module PostSetPresenters - class Post < Base - MAX_TAGS = 25 - - attr_accessor :post_set - delegate :posts, :to => :post_set - - def initialize(post_set) - @post_set = post_set - end - - def tag_set_presenter - @tag_set_presenter ||= TagSetPresenter.new(related_tags.take(MAX_TAGS)) - end - - def post_previews_html(template, options = {}) - super(template, options.merge(show_cropped: true)) - end - - def related_tags - if post_set.query.is_wildcard_search? - wildcard_tags - elsif post_set.query.is_metatag?(:search) - saved_search_tags - elsif post_set.query.is_empty_search? || post_set.query.is_metatag?(:order, :rank) - popular_tags - elsif post_set.query.is_single_term? - similar_tags - else - frequent_tags - end - end - - def popular_tags - if PopularSearchService.enabled? - PopularSearchService.new(Date.today).tags - else - frequent_tags - end - end - - def similar_tags - RelatedTagCalculator.cached_similar_tags_for_search(post_set.query, MAX_TAGS) - end - - def frequent_tags - RelatedTagCalculator.frequent_tags_for_post_array(post_set.posts).take(MAX_TAGS) - end - - def wildcard_tags - Tag.wildcard_matches(post_set.tag_string) - end - - def saved_search_tags - ["search:all"] + SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"} - end - - def tag_list_html(**options) - tag_set_presenter.tag_list_html(name_only: post_set.query.is_metatag?(:search), **options) - end - end -end diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 0fecc10af..eb137a53c 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -7,7 +7,7 @@

Tags

- <%= @post_set.presenter.tag_list_html(current_query: params[:tags], show_extra_links: policy(Post).show_extra_links?) %> + <%= @post_set.tag_list_html(current_query: params[:tags], show_extra_links: policy(Post).show_extra_links?) %>
<%= render "posts/partials/index/options" %> diff --git a/app/views/posts/partials/index/_posts.html.erb b/app/views/posts/partials/index/_posts.html.erb index 672d42e8e..74f39f4c9 100644 --- a/app/views/posts/partials/index/_posts.html.erb +++ b/app/views/posts/partials/index/_posts.html.erb @@ -1,6 +1,6 @@
- <%= post_set.presenter.post_previews_html(self) %> + <%= post_set.post_previews_html(self) %>
<% if post_set.hidden_posts.present? %>