explore/posts/popular: refactor post previews.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
23
app/helpers/popular_posts_helper.rb
Normal file
23
app/helpers/popular_posts_helper.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
@@ -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 << '<span class="period">'
|
||||
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 << '</span>'
|
||||
html.join("\n").html_safe
|
||||
end
|
||||
|
||||
def nav_links(template)
|
||||
html = []
|
||||
html << '<p id="popular-nav-links">'
|
||||
html << nav_links_for_scale(template, "Day")
|
||||
html << nav_links_for_scale(template, "Week")
|
||||
html << nav_links_for_scale(template, "Month")
|
||||
html << '</p>'
|
||||
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
|
||||
17
app/views/explore/posts/_nav_links.html.erb
Normal file
17
app/views/explore/posts/_nav_links.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<%# date, selected_scale, scale %>
|
||||
|
||||
<span class="period">
|
||||
<% 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 %>
|
||||
</span>
|
||||
@@ -1,12 +1,26 @@
|
||||
<div id="c-explore-posts">
|
||||
<div id="a-popular">
|
||||
<h1>Popular: <%= @post_set.presenter.range_text %></h1>
|
||||
<h1>
|
||||
Popular:
|
||||
|
||||
<%= @post_set.presenter.nav_links(self) %>
|
||||
<% if @scale == "day" %>
|
||||
<%= @date.strftime("%B %d, %Y") %>
|
||||
<% elsif @scale == "week" %>
|
||||
<%= @date.strftime("Week of %B %d, %Y") %>
|
||||
<% elsif @scale == "month" %>
|
||||
<%= @date.strftime("%B %Y") %>
|
||||
<% end %>
|
||||
</h1>
|
||||
|
||||
<p id="popular-nav-links">
|
||||
<%= render "explore/posts/nav_links", date: @date, selected_scale: @scale, scale: "day" %>
|
||||
<%= render "explore/posts/nav_links", date: @date, selected_scale: @scale, scale: "week" %>
|
||||
<%= render "explore/posts/nav_links", date: @date, selected_scale: @scale, scale: "month" %>
|
||||
</p>
|
||||
|
||||
<%= render "posts/partials/common/inline_blacklist" %>
|
||||
|
||||
<%= @post_set.presenter.post_previews_html(self) %>
|
||||
<%= post_previews_html(@posts) %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -17,5 +31,5 @@
|
||||
<% end %>
|
||||
|
||||
<% content_for(:html_header) do %>
|
||||
<meta name="description" content="The most popular posts per day">
|
||||
<meta name="description" content="The most popular posts per <%= @scale %>">
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user