Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def post

def post_follow
@post = Post.find(params[:post_id])
if CommentThread.post_followed?(@post, current_user)
if @post.followed_by?(current_user)
ThreadFollower.where(post: @post, user: current_user).destroy_all
else
ThreadFollower.create(post: @post, user: current_user)
Expand Down
8 changes: 0 additions & 8 deletions app/models/comment_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ class CommentThread < ApplicationRecord

after_create :create_follower

# Are there any threads on a given post that a given user follows?
# @param post [Post] post to check
# @param user [User] user to check
# @return [Boolean] check result
def self.post_followed?(post, user)
ThreadFollower.where(post: post, user: user).any?
end

# Is the thread read-only (can't be edited)?
# @return [Boolean] check result
def read_only?
Expand Down
8 changes: 8 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ def reaction_list
.to_h { |_k, v| [v.first.reaction_type, v] }
end

# Are new threads on this post followed by a given user?
# @param post [Post] post to check
# @param user [User] user to check
# @return [Boolean] check result
def followed_by?(user)
ThreadFollower.where(post: self, user: user).any?
end

private

##
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/_expanded.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@
<%= pluralize(public_count, 'comment thread') %>
</h4>
<% if user_signed_in? %>
<% if CommentThread.post_followed?(post, current_user) %>
<% if post.followed_by?(current_user) %>
<%= link_to follow_post_comments_path(post_id: post.id), method: :post,
class: "button is-muted is-outlined is-small",
title: 'Don\'t follow new comment threads on this post',
Expand Down
12 changes: 12 additions & 0 deletions test/comments_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ def try_unfollow_thread(thread)
post :thread_unrestrict, params: { id: thread.id, type: 'follow' }
end

# Attempts to follow new threads on a given post
# @param post [Post] post to follow
def try_post_follow(test_post)
post :post_follow, params: { post_id: test_post.id }
end

# Attempts to unfollow new threads on a given post
# @param post [Post] post to unfollow
def try_post_unfollow(test_post)
post :post_follow, params: { post_id: test_post.id }
end

# Attempts to lock a given comment thread
# @param thread [CommentThread] thread to lock
# @param duration [Integer] lock duration, in days
Expand Down
37 changes: 37 additions & 0 deletions test/controllers/comments/post_follow_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'test_helper'
require 'comments_test_helpers'

class CommentsControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
include CommentsControllerTestHelpers

test 'post follower can unfollow post' do
user = users(:standard_user)
sign_in user
question = posts(:question_one)

# Assert user follows post
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count

try_post_unfollow(question)
assert_response(:found)

# Assert user does not follow post
assert_equal 0, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
end

test 'non-follower can follow post' do
user = users(:basic_user)
sign_in user
question = posts(:question_one)

# Assert user does not follow post
assert_equal 0, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count

try_post_follow(question)
assert_response(:found)

# Assert user follows post
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
end
end
4 changes: 4 additions & 0 deletions test/fixtures/thread_followers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ deleter_normal:
standard_author_normal:
user: standard_user
comment_thread: normal

standard_author_question_one:
user: standard_user
post: question_one