Files
danbooru/app/controllers/explore/posts_controller.rb
evazion e8590afa6d 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.
2020-02-23 17:26:08 -06:00

45 lines
1.2 KiB
Ruby

module Explore
class PostsController < ApplicationController
respond_to :html, :xml, :json
def popular
@date, @scale, @min_date, @max_date = parse_date(params)
limit = params.fetch(:limit, CurrentUser.user.per_page)
@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
def missed_searches
@search_service = MissedSearchService.new
end
private
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(min_date, max_date)
Post.where(created_at: min_date..max_date).tag_match("order:score")
end
end
end