5
3
mirror of https://github.com/tildeverse/lobsters synced 2024-06-20 15:37:04 +00:00

Comment: refactor hidden story filtering into scope

This commit is contained in:
Peter Bhat Harkins 2020-06-08 21:18:06 -05:00
parent b32bac90b4
commit 4aa92ecffb
2 changed files with 8 additions and 4 deletions

View File

@ -238,6 +238,7 @@ class CommentsController < ApplicationController
end
@comments = Comment.for_user(@user)
.not_on_story_hidden_by(@user)
.order("id DESC")
.includes(:user, :hat, :story => :user)
.joins(:story).where.not(stories: { is_expired: true })
@ -245,10 +246,6 @@ class CommentsController < ApplicationController
.offset((@page - 1) * COMMENTS_PER_PAGE)
if @user
@comments = @comments.where("NOT EXISTS (SELECT 1 FROM " <<
"hidden_stories WHERE user_id = ? AND " <<
"hidden_stories.story_id = comments.story_id)", @user.id)
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id, @comments.map(&:id))
@comments.each do |c|

View File

@ -34,6 +34,13 @@ class Comment < ApplicationRecord
scope :not_moderated, -> { where(is_moderated: false) }
scope :active, -> { not_deleted.not_moderated }
scope :for_user, ->(user) { user && user.is_moderator? ? all : active }
scope :not_on_story_hidden_by, ->(user) {
user ? where.not(
HiddenStory.select('TRUE')
.where(Arel.sql('hidden_stories.story_id = stories.id'))
.by(user).arel.exists
) : where('true')
}
DOWNVOTABLE_DAYS = 7
DELETEABLE_DAYS = DOWNVOTABLE_DAYS * 2