diff --git a/app/controllers/explore/posts_controller.rb b/app/controllers/explore/posts_controller.rb index f94004914..5a7ad1461 100644 --- a/app/controllers/explore/posts_controller.rb +++ b/app/controllers/explore/posts_controller.rb @@ -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 diff --git a/app/helpers/popular_posts_helper.rb b/app/helpers/popular_posts_helper.rb index 2863ded03..82a4721d4 100644 --- a/app/helpers/popular_posts_helper.rb +++ b/app/helpers/popular_posts_helper.rb @@ -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 diff --git a/app/views/explore/posts/popular.html.erb b/app/views/explore/posts/popular.html.erb index 47df3fc83..8ab8f4c5f 100644 --- a/app/views/explore/posts/popular.html.erb +++ b/app/views/explore/posts/popular.html.erb @@ -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 %>