Skip to content

Commit d9256f4

Browse files
authored
Merge pull request #1792 from codidact/trichoplax/simplify-post-followed-by-user-method
Simplify post followed by user method
2 parents 768a15e + 55760b9 commit d9256f4

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

app/controllers/comments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def post
276276

277277
def post_follow
278278
@post = Post.find(params[:post_id])
279-
if CommentThread.post_followed?(@post, current_user)
279+
if @post.followed_by?(current_user)
280280
ThreadFollower.where(post: @post, user: current_user).destroy_all
281281
else
282282
ThreadFollower.create(post: @post, user: current_user)

app/models/comment_thread.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ class CommentThread < ApplicationRecord
1616

1717
after_create :create_follower
1818

19-
# Are there any threads on a given post that a given user follows?
20-
# @param post [Post] post to check
21-
# @param user [User] user to check
22-
# @return [Boolean] check result
23-
def self.post_followed?(post, user)
24-
ThreadFollower.where(post: post, user: user).any?
25-
end
26-
2719
# Is the thread read-only (can't be edited)?
2820
# @return [Boolean] check result
2921
def read_only?

app/models/post.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ def reaction_list
234234
.to_h { |_k, v| [v.first.reaction_type, v] }
235235
end
236236

237+
# Are new threads on this post followed by a given user?
238+
# @param post [Post] post to check
239+
# @param user [User] user to check
240+
# @return [Boolean] check result
241+
def followed_by?(user)
242+
ThreadFollower.where(post: self, user: user).any?
243+
end
244+
237245
private
238246

239247
##

app/views/posts/_expanded.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@
532532
<%= pluralize(public_count, 'comment thread') %>
533533
</h4>
534534
<% if user_signed_in? %>
535-
<% if CommentThread.post_followed?(post, current_user) %>
535+
<% if post.followed_by?(current_user) %>
536536
<%= link_to follow_post_comments_path(post_id: post.id), method: :post,
537537
class: "button is-muted is-outlined is-small",
538538
title: 'Don\'t follow new comment threads on this post',

test/comments_test_helpers.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ def try_unfollow_thread(thread)
7171
post :thread_unrestrict, params: { id: thread.id, type: 'follow' }
7272
end
7373

74+
# Attempts to follow new threads on a given post
75+
# @param post [Post] post to follow
76+
def try_post_follow(test_post)
77+
post :post_follow, params: { post_id: test_post.id }
78+
end
79+
80+
# Attempts to unfollow new threads on a given post
81+
# @param post [Post] post to unfollow
82+
def try_post_unfollow(test_post)
83+
post :post_follow, params: { post_id: test_post.id }
84+
end
85+
7486
# Attempts to lock a given comment thread
7587
# @param thread [CommentThread] thread to lock
7688
# @param duration [Integer] lock duration, in days
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'test_helper'
2+
require 'comments_test_helpers'
3+
4+
class CommentsControllerTest < ActionController::TestCase
5+
include Devise::Test::ControllerHelpers
6+
include CommentsControllerTestHelpers
7+
8+
test 'post follower can unfollow post' do
9+
user = users(:standard_user)
10+
sign_in user
11+
question = posts(:question_one)
12+
13+
# Assert user follows post
14+
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
15+
16+
try_post_unfollow(question)
17+
assert_response(:found)
18+
19+
# Assert user does not follow post
20+
assert_equal 0, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
21+
end
22+
23+
test 'non-follower can follow post' do
24+
user = users(:basic_user)
25+
sign_in user
26+
question = posts(:question_one)
27+
28+
# Assert user does not follow post
29+
assert_equal 0, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
30+
31+
try_post_follow(question)
32+
assert_response(:found)
33+
34+
# Assert user follows post
35+
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
36+
end
37+
end

test/fixtures/thread_followers.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ deleter_normal:
55
standard_author_normal:
66
user: standard_user
77
comment_thread: normal
8+
9+
standard_author_question_one:
10+
user: standard_user
11+
post: question_one

0 commit comments

Comments
 (0)