move replies filters to url path; remove 1 + n on unread tracking

This commit is contained in:
Peter Bhat Harkins 2018-01-31 08:21:11 -06:00
parent 3f17043e2e
commit 19a6e330d1
6 changed files with 64 additions and 55 deletions

View File

@ -1,54 +1,57 @@
class RepliesController < ApplicationController
REPLIES_PER_PAGE = 25
before_action :require_logged_in_user_or_400
after_action :update_read_ribbons
before_action :require_logged_in_user_or_400, :set_page
after_action :update_read_ribbons, only: [ :unread ]
def show
def all
@heading = @title = "All Your Replies"
@replies = ReplyingComment
.for_user(@user.id)
.offset((@page - 1) * REPLIES_PER_PAGE)
.limit(REPLIES_PER_PAGE)
render :show
end
def comments
@heading = @title = "Your Comment Replies"
@replies = ReplyingComment
.comment_replies_for(@user.id)
.offset((@page - 1) * REPLIES_PER_PAGE)
.limit(REPLIES_PER_PAGE)
render :show
end
def stories
@heading = @title = "Your Story Replies"
@replies = ReplyingComment
.story_replies_for(@user.id)
.offset((@page - 1) * REPLIES_PER_PAGE)
.limit(REPLIES_PER_PAGE)
render :show
end
def unread
@heading = @title = "Your Unread Replies"
@replies = ReplyingComment.unread_replies_for(@user.id)
render :show
end
private
def set_page
@page = params[:page].to_i
if @page == 0
@page = 1
elsif @page < 0 || @page > (2 ** 32)
raise ActionController::RoutingError.new("page out of bounds")
end
@filter = params[:filter] || 'unread'
case @filter
when 'comments'
@heading = @title = "Your Comment Replies"
@replies = ReplyingComment
.comment_replies_for(@user.id)
.offset((@page - 1) * REPLIES_PER_PAGE)
.limit(REPLIES_PER_PAGE)
when 'stories'
@heading = @title = "Your Story Replies"
@replies = ReplyingComment
.story_replies_for(@user.id)
.offset((@page - 1) * REPLIES_PER_PAGE)
.limit(REPLIES_PER_PAGE)
when 'all'
@heading = @title = "All Your Replies"
@replies = ReplyingComment
.for_user(@user.id)
.offset((@page - 1) * REPLIES_PER_PAGE)
.limit(REPLIES_PER_PAGE)
else
@heading = @title = "Your Unread Replies"
@replies = ReplyingComment.unread_replies_for(@user.id)
end
end
private
def update_read_ribbons
return unless @filter == 'unread'
stories = @replies.pluck(:story_id).uniq
stories.each do |story|
ribbon = ReadRibbon.find_by(user_id: @user.id, story_id: story)
ribbon.updated_at = Time.now
ribbon.save!
end
story_ids = @replies.pluck(:story_id).uniq
ReadRibbon
.where(user_id: @user.id, story_id: story_ids)
.update_all(updated_at: Time.now)
end
end

View File

@ -410,7 +410,7 @@ private
def track_story_reads
@story = Story.where(short_id: params[:id]).first!
@ribbon = ReadRibbon.where(user: @user, story: story).first_or_create
@ribbon = ReadRibbon.where(user: @user, story: @story).first_or_create
yield
@ribbon.touch
end

View File

@ -1,11 +1,9 @@
module RepliesHelper
def link_to_filter(name)
title = name.titleize
if @filter != name
link_to(title, replies_path(filter: name))
def link_to_different_page(text, path)
if current_page? path
text
else
title
link_to(text, path)
end
end
end

View File

@ -2,6 +2,9 @@ class ReadRibbon < ApplicationRecord
belongs_to :user
belongs_to :story
# careful with callbacks on this model; for performance the read tracking in
# StoriesController uses .touch and RepliesController uses update_all
def self.hide_replies_for(story_id, user_id)
ribbon = find_by(user_id: user_id, story_id: story_id)
ribbon.is_following = false

View File

@ -1,10 +1,9 @@
<div class="box wide">
<div class="legend right">
<%= link_to_filter('unread') %> |
<%= link_to_filter('all') %> |
<%= link_to_filter('comments') %> |
<%= link_to_filter('stories') %>
<%= link_to_different_page('Unread', replies_unread_path) %> |
<%= link_to_different_page('All', replies_path) %> |
<%= link_to_different_page('Comments', replies_comments_path) %> |
<%= link_to_different_page('Stories', replies_stories_path) %>
</div>
<div class="legend">
@ -16,9 +15,9 @@
<% @replies.each do |reply| %>
<li class="comments_subtree">
<%= render "comments/comment",
comment: reply.comment,
comment: reply.comment,
show_story: true,
is_unread: reply.is_unread,
is_unread: reply.is_unread,
show_tree_lines: false %>
<ol class="comments"></ol>
</li>

View File

@ -33,8 +33,14 @@ Lobsters::Application.routes.draw do
get "/threads" => "comments#threads"
get "/threads/:user" => "comments#threads"
get "/replies" => "replies#show"
get "/replies/page/:page" => "replies#show"
get "/replies" => "replies#all"
get "/replies/page/:page" => "replies#all"
get "/replies/comments" => "replies#comments"
get "/replies/comments/page/:page" => "replies#comments"
get "/replies/stories" => "replies#stories"
get "/replies/stories/page/:page" => "replies#stories"
get "/replies/unread" => "replies#unread"
get "/replies/unread/page/:page" => "replies#unread"
get "/login" => "login#index"
post "/login" => "login#login"