Skip to content

Commit c1f6144

Browse files
committed
added CommentThread#accessible_to class method
1 parent d808607 commit c1f6144

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,9 @@ def thread_unrestrict
263263

264264
def post
265265
@post = Post.find(params[:post_id])
266-
@comment_threads = if current_user&.at_least_moderator? || current_user&.post_privilege?('flag_curate', @post)
267-
CommentThread
268-
else
269-
CommentThread.undeleted
270-
end.where(post: @post).order(deleted: :asc, archived: :asc, reply_count: :desc)
266+
@comment_threads = CommentThread.accessible_to(current_user, @post)
267+
.where(post: @post)
268+
.order(deleted: :asc, archived: :asc, reply_count: :desc)
271269
respond_to do |format|
272270
format.html { render layout: false }
273271
format.json { render json: @comment_threads }

app/models/comment_thread.rb

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

1717
after_create :create_follower
1818

19+
# Gets threads appropriately scoped for a given user & post
20+
# @param user [User, nil] user to check
21+
# @para post [Post] post to check
22+
# @return [ActiveRecord::Relation<CommentThread>]
23+
def self.accessible_to(user, post)
24+
if user&.at_least_moderator? || user&.post_privilege?('flag_curate', post)
25+
CommentThread
26+
else
27+
CommentThread.undeleted
28+
end
29+
end
30+
1931
# Is the thread read-only (can't be edited)?
2032
# @return [Boolean] check result
2133
def read_only?

0 commit comments

Comments
 (0)