forked from lobsters/lobsters
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show story and comment replies, tracking unread ones
- Loading branch information
Showing
22 changed files
with
264 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class RepliesController < ApplicationController | ||
REPLIES_PER_PAGE = 25 | ||
|
||
before_action :require_logged_in_user_or_400 | ||
after_action :update_read_ribbons | ||
|
||
def show | ||
@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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module RepliesHelper | ||
def link_to_filter(name) | ||
title = name.titleize | ||
|
||
if @filter != name | ||
link_to(title, replies_path(filter: name)) | ||
else | ||
title | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ApplicationRecord < ActiveRecord::Base | ||
self.abstract_class = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class ReadRibbon < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :story | ||
|
||
def self.hide_replies_for(story_id, user_id) | ||
ribbon = find_by(user_id: user_id, story_id: story_id) | ||
ribbon.is_following = false | ||
ribbon.save! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class ReplyingComment < ApplicationRecord | ||
attribute :is_unread, :boolean | ||
|
||
belongs_to :comment | ||
|
||
scope :for_user, ->(user_id) { where(user_id: user_id).order(comment_created_at: :desc) } | ||
scope :unread_replies_for, ->(user_id) { for_user(user_id).where(is_unread: true) } | ||
scope :comment_replies_for, ->(user_id) { for_user(user_id).where('parent_comment_id is not null') } | ||
scope :story_replies_for, ->(user_id) { for_user(user_id).where('parent_comment_id is null') } | ||
|
||
protected | ||
# This is a view, not a real table | ||
def readonly? | ||
true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<div class="box wide"> | ||
<div class="legend right"> | ||
<%= link_to_filter('unread') %> | | ||
<%= link_to_filter('all') %> | | ||
<%= link_to_filter('comments') %> | | ||
<%= link_to_filter('stories') %> | ||
|
||
</div> | ||
|
||
<div class="legend"> | ||
<%= @heading %> | ||
</div> | ||
</div> | ||
|
||
<ol class="comments comments1"> | ||
<% @replies.each do |reply| %> | ||
<li class="comments_subtree"> | ||
<%= render "comments/comment", | ||
comment: reply.comment, | ||
show_story: true, | ||
is_unread: reply.is_unread, | ||
show_tree_lines: false %> | ||
<ol class="comments"></ol> | ||
</li> | ||
<% end %> | ||
</ol> | ||
|
||
<% if @replies.count > RepliesController::REPLIES_PER_PAGE && @filter != 'unread'%> | ||
<div class="morelink"> | ||
<% if @page && @page > 1 %> | ||
<a href="/replies<%= @page == 2 ? "" : "/page/#{@page - 1}" %>"><< | ||
Page <%= @page - 1 %></a> | ||
<% end %> | ||
|
||
<% if @replies.any? %> | ||
<% if @page && @page > 1 %> | ||
| | ||
<% end %> | ||
|
||
<a href="/replies/page/<%= @page + 1 %>">Page | ||
<%= @page + 1 %> >></a> | ||
<% end %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Scenic.configure do |config| | ||
config.database = Scenic::Adapters::Mysql.new | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
db/migrate/20180130235553_add_read_notification_support.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class AddReadNotificationSupport < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :read_ribbons do |t| | ||
t.boolean :is_following, default: true | ||
t.timestamps | ||
end | ||
|
||
add_reference :read_ribbons, :user, index: true | ||
add_reference :read_ribbons, :story, index: true | ||
|
||
create_view :replying_comments | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.