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
|
||||
class PostsController < ApplicationController
|
||||
respond_to :html, :xml, :json
|
||||
before_action :set_date, only: [:searches, :viewed]
|
||||
|
||||
def popular
|
||||
@date = params[:date] ? Date.parse(params[:date]) : Date.today
|
||||
@scale = params[:scale]
|
||||
@scale = "day" unless @scale.in?(["day", "week", "month"])
|
||||
@date, @scale, @min_date, @max_date = parse_date(params)
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
def viewed
|
||||
@date, @scale, @min_date, @max_date = parse_date(params)
|
||||
@posts = PostViewCountService.new.popular_posts(@date)
|
||||
respond_with(@posts)
|
||||
end
|
||||
|
||||
def searches
|
||||
@date, @scale, @min_date, @max_date = parse_date(params)
|
||||
@search_service = PopularSearchService.new(@date)
|
||||
end
|
||||
|
||||
@@ -28,19 +28,17 @@ module Explore
|
||||
|
||||
private
|
||||
|
||||
def set_date
|
||||
@date = params[:date] ? Date.parse(params[:date]) : Date.today
|
||||
def parse_date(params)
|
||||
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
|
||||
|
||||
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
|
||||
def popular_posts(min_date, max_date)
|
||||
Post.where(created_at: min_date..max_date).tag_match("order:score")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ module PopularPostsHelper
|
||||
when "day"
|
||||
date + 1.day
|
||||
when "week"
|
||||
date + 1.day
|
||||
date + 1.week
|
||||
when "month"
|
||||
1.month.since(date)
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<% if @scale == "day" %>
|
||||
<%= @date.strftime("%B %d, %Y") %>
|
||||
<% 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" %>
|
||||
<%= @date.strftime("%B %Y") %>
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user