From 12a845de92f2cc5e9d95164262ed97eb201e2a9c Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 12 Jan 2020 22:57:40 -0600 Subject: [PATCH] explore/posts/popular: refactor post previews. --- app/controllers/explore/posts_controller.rb | 19 ++- app/controllers/static_controller.rb | 3 +- app/helpers/popular_posts_helper.rb | 23 ++++ app/logical/post_sets/popular.rb | 49 ------- app/presenters/post_set_presenters/popular.rb | 125 ------------------ app/views/explore/posts/_nav_links.html.erb | 17 +++ app/views/explore/posts/popular.html.erb | 22 ++- 7 files changed, 76 insertions(+), 182 deletions(-) create mode 100644 app/helpers/popular_posts_helper.rb delete mode 100644 app/logical/post_sets/popular.rb delete mode 100644 app/presenters/post_set_presenters/popular.rb create mode 100644 app/views/explore/posts/_nav_links.html.erb diff --git a/app/controllers/explore/posts_controller.rb b/app/controllers/explore/posts_controller.rb index 1d838c4cf..17e9e092e 100644 --- a/app/controllers/explore/posts_controller.rb +++ b/app/controllers/explore/posts_controller.rb @@ -4,8 +4,12 @@ module Explore before_action :set_date, only: [:searches, :viewed] def popular - @post_set = PostSets::Popular.new(params[:date], params[:scale]) - @posts = @post_set.posts + @date = params[:date] ? Date.parse(params[:date]) : Date.today + @scale = params[:scale] + @scale = "day" unless @scale.in?(["day", "week", "month"]) + + limit = params.fetch(:limit, CurrentUser.user.per_page) + @posts = popular_posts(@date, @scale).paginate(params[:page], limit: limit) respond_with(@posts) end @@ -32,5 +36,16 @@ module Explore def set_date @date = params[:date] ? Date.parse(params[:date]) : Date.today end + + def popular_posts(date, scale) + if scale == "day" + Post.tag_match("date:#{date} order:score") + else + min_date = date.send("beginning_of_#{scale}").to_date.to_s + max_date = date.send("end_of_#{scale}").to_date.to_s + search = "date:#{min_date}..#{max_date} order:score" + Post.tag_match(search) + end + end end end diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index f93848024..63d7f13ce 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -18,8 +18,7 @@ class StaticController < ApplicationController def sitemap @popular_search_service = PopularSearchService.new(Date.yesterday) - @post_set = PostSets::Popular.new(Date.yesterday.to_s, "week", limit: 200) - @posts = @post_set.posts + @posts = Post.where("created_at > ?", 1.week.ago).order(score: :desc).limit(200) render layout: false end end diff --git a/app/helpers/popular_posts_helper.rb b/app/helpers/popular_posts_helper.rb new file mode 100644 index 000000000..2863ded03 --- /dev/null +++ b/app/helpers/popular_posts_helper.rb @@ -0,0 +1,23 @@ +module PopularPostsHelper + def next_date_for_scale(date, scale) + case scale + when "day" + date + 1.day + when "week" + date + 1.day + when "month" + 1.month.since(date) + end + end + + def prev_date_for_scale(date, scale) + case scale + when "day" + date - 1.day + when "week" + date - 7.days + when "month" + 1.month.ago(date) + end + end +end diff --git a/app/logical/post_sets/popular.rb b/app/logical/post_sets/popular.rb deleted file mode 100644 index 6e7a0c427..000000000 --- a/app/logical/post_sets/popular.rb +++ /dev/null @@ -1,49 +0,0 @@ -module PostSets - class Popular < PostSets::Base - attr_reader :date, :scale, :limit - - def initialize(date, scale, limit: nil) - @date = date.blank? ? Time.zone.now : Time.zone.parse(date) - @scale = scale - @limit = limit || CurrentUser.per_page - end - - def posts - @posts ||= begin - query = ::Post.where("created_at between ? and ?", min_date.beginning_of_day, max_date.end_of_day).order("score desc").limit(limit) - query.each # hack to force rails to eager load - query - end - end - - def min_date - case scale - when "week" - date.beginning_of_week - - when "month" - date.beginning_of_month - - else - date - end - end - - def max_date - case scale - when "week" - date.end_of_week - - when "month" - date.end_of_month - - else - date - end - end - - def presenter - ::PostSetPresenters::Popular.new(self) - end - end -end diff --git a/app/presenters/post_set_presenters/popular.rb b/app/presenters/post_set_presenters/popular.rb deleted file mode 100644 index 4be7909e7..000000000 --- a/app/presenters/post_set_presenters/popular.rb +++ /dev/null @@ -1,125 +0,0 @@ -module PostSetPresenters - class Popular < Base - attr_accessor :post_set, :tag_set_presenter - delegate :posts, :date, :min_date, :max_date, :to => :post_set - - def initialize(post_set) - @post_set = post_set - end - - def prev_day - date - 1.day - end - - def next_day - date + 1.day - end - - def prev_week - date - 7.days - end - - def next_week - date + 7.days - end - - def prev_month - 1.month.ago(date) - end - - def next_month - 1.month.since(date) - end - - def link_rel_for_scale?(template, scale) - (template.params[:scale].blank? && scale == "day") || template.params[:scale].to_s.include?(scale) - end - - def next_date_for_scale(scale) - case scale - when "Day" - next_day - - when "Week" - next_week - - when "Month" - next_month - - else - nil - end - end - - def prev_date_for_scale(scale) - case scale - when "Day" - prev_day - - when "Week" - prev_week - - when "Month" - prev_month - - else - nil - end - end - - def nav_links_for_scale(template, scale) - html = [] - html << '' - html << template.link_to( - "«prev".html_safe, - template.popular_explore_posts_path( - :date => prev_date_for_scale(scale), - :scale => scale.downcase - ), - :id => (link_rel_for_scale?(template, scale.downcase) ? "paginator-prev" : nil), - :rel => (link_rel_for_scale?(template, scale.downcase) ? "prev" : nil), - :"data-shortcut" => (link_rel_for_scale?(template, scale.downcase) ? "a left" : nil) - ) - html << template.link_to( - scale, - template.popular_explore_posts_path( - :date => date, - :scale => scale.downcase - ), - :class => "desc" - ) - html << template.link_to( - "next»".html_safe, - template.popular_explore_posts_path( - :date => next_date_for_scale(scale), - :scale => scale.downcase - ), - :id => (link_rel_for_scale?(template, scale.downcase) ? "paginator-next" : nil), - :rel => (link_rel_for_scale?(template, scale.downcase) ? "next" : nil), - :"data-shortcut" => (link_rel_for_scale?(template, scale.downcase) ? "d right" : nil) - ) - html << '' - html.join("\n").html_safe - end - - def nav_links(template) - html = [] - html << '' - html.join("\n").html_safe - end - - def range_text - if min_date == max_date - date.strftime("%B %d, %Y") - elsif max_date - min_date < 10.days - min_date.strftime("Week of %B %d, %Y") - else - date.strftime("%B %Y") - end - end - end -end diff --git a/app/views/explore/posts/_nav_links.html.erb b/app/views/explore/posts/_nav_links.html.erb new file mode 100644 index 000000000..c2f19b938 --- /dev/null +++ b/app/views/explore/posts/_nav_links.html.erb @@ -0,0 +1,17 @@ +<%# date, selected_scale, scale %> + + + <% if selected_scale == scale %> + <%= link_to "« prev", popular_explore_posts_path(date: prev_date_for_scale(date, scale), scale: scale), id: "paginator-prev", rel: "prev", "data-shortcut": "a left" %> + <% else %> + <%= link_to "« prev", popular_explore_posts_path(date: prev_date_for_scale(date, scale), scale: scale) %> + <% end %> + + <%= link_to scale.capitalize, popular_explore_posts_path(date: date, scale: scale), class: "desc" %> + + <% if selected_scale == scale %> + <%= link_to "next »", popular_explore_posts_path(date: next_date_for_scale(date, scale), scale: scale), id: "paginator-next", rel: "next", "data-shortcut": "d right" %> + <% else %> + <%= link_to "next »", popular_explore_posts_path(date: next_date_for_scale(date, scale), scale: scale) %> + <% end %> + diff --git a/app/views/explore/posts/popular.html.erb b/app/views/explore/posts/popular.html.erb index 61b858860..bc6c455f2 100644 --- a/app/views/explore/posts/popular.html.erb +++ b/app/views/explore/posts/popular.html.erb @@ -1,12 +1,26 @@
@@ -17,5 +31,5 @@ <% end %> <% content_for(:html_header) do %> - + <% end %>