popular posts: fix date range handling.
* Fix the next button for the weekly timescale to jump to the next week, not the next day. * Show the start and end dates for the weekly timescale. * Use `Time.zone.today` instead of `Date.today` to respect the user's timezone setting.
This commit is contained in:
@@ -1,24 +1,24 @@
|
|||||||
module Explore
|
module Explore
|
||||||
class PostsController < ApplicationController
|
class PostsController < ApplicationController
|
||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
before_action :set_date, only: [:searches, :viewed]
|
|
||||||
|
|
||||||
def popular
|
def popular
|
||||||
@date = params[:date] ? Date.parse(params[:date]) : Date.today
|
@date, @scale, @min_date, @max_date = parse_date(params)
|
||||||
@scale = params[:scale]
|
|
||||||
@scale = "day" unless @scale.in?(["day", "week", "month"])
|
|
||||||
|
|
||||||
limit = params.fetch(:limit, CurrentUser.user.per_page)
|
limit = params.fetch(:limit, CurrentUser.user.per_page)
|
||||||
@posts = popular_posts(@date, @scale).paginate(params[:page], limit: limit)
|
@posts = popular_posts(@min_date, @max_date).paginate(params[:page], limit: limit)
|
||||||
|
|
||||||
respond_with(@posts)
|
respond_with(@posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def viewed
|
def viewed
|
||||||
|
@date, @scale, @min_date, @max_date = parse_date(params)
|
||||||
@posts = PostViewCountService.new.popular_posts(@date)
|
@posts = PostViewCountService.new.popular_posts(@date)
|
||||||
respond_with(@posts)
|
respond_with(@posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def searches
|
def searches
|
||||||
|
@date, @scale, @min_date, @max_date = parse_date(params)
|
||||||
@search_service = PopularSearchService.new(@date)
|
@search_service = PopularSearchService.new(@date)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -28,19 +28,17 @@ module Explore
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_date
|
def parse_date(params)
|
||||||
@date = params[:date] ? Date.parse(params[:date]) : Date.today
|
date = params[:date] ? Date.parse(params[:date]) : Time.zone.today
|
||||||
|
scale = params[:scale].in?(["day", "week", "month"]) ? params[:scale] : "day"
|
||||||
|
min_date = date.send("beginning_of_#{scale}")
|
||||||
|
max_date = date.send("next_#{scale}").send("beginning_of_#{scale}")
|
||||||
|
|
||||||
|
[date, scale, min_date, max_date]
|
||||||
end
|
end
|
||||||
|
|
||||||
def popular_posts(date, scale)
|
def popular_posts(min_date, max_date)
|
||||||
if scale == "day"
|
Post.where(created_at: min_date..max_date).tag_match("order:score")
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ module PopularPostsHelper
|
|||||||
when "day"
|
when "day"
|
||||||
date + 1.day
|
date + 1.day
|
||||||
when "week"
|
when "week"
|
||||||
date + 1.day
|
date + 1.week
|
||||||
when "month"
|
when "month"
|
||||||
1.month.since(date)
|
1.month.since(date)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
<% if @scale == "day" %>
|
<% if @scale == "day" %>
|
||||||
<%= @date.strftime("%B %d, %Y") %>
|
<%= @date.strftime("%B %d, %Y") %>
|
||||||
<% elsif @scale == "week" %>
|
<% elsif @scale == "week" %>
|
||||||
<%= @date.strftime("Week of %B %d, %Y") %>
|
<%= @min_date.strftime("%B %d, %Y") %> - <%= @max_date.strftime("%B %d, %Y") %>
|
||||||
<% elsif @scale == "month" %>
|
<% elsif @scale == "month" %>
|
||||||
<%= @date.strftime("%B %Y") %>
|
<%= @date.strftime("%B %Y") %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
Reference in New Issue
Block a user