fix exception on anon viewing reply form

This commit is contained in:
Peter Bhat Harkins 2023-11-22 10:13:58 -06:00
parent 88c7a4a972
commit bc27b2c929
2 changed files with 13 additions and 20 deletions

View File

@ -6,7 +6,7 @@ class CommentsController < ApplicationController
caches_page :index, :threads, if: CACHE_PAGE
before_action :require_logged_in_user_or_400,
only: [:create, :preview, :upvote, :flag, :unvote]
only: [:create, :preview, :reply, :upvote, :flag, :unvote, :update]
before_action :require_logged_in_user, only: [:upvoted]
before_action :flag_warning, only: [:user_threads]
before_action :show_title_h1
@ -136,7 +136,7 @@ class CommentsController < ApplicationController
else
parents = comment.parents.with_thread_attributes.for_presentation
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id, parents.map(&:id))
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user&.id, parents.map(&:id))
parents.each { |c| c.current_vote = @votes[c.id] }
render "_commentbox", locals: {
comment: comment,
@ -272,11 +272,8 @@ class CommentsController < ApplicationController
.limit(COMMENTS_PER_PAGE)
.offset((@page - 1) * COMMENTS_PER_PAGE)
if @user
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id, @comments.map(&:id))
@comments.each { |c| c.current_vote = @votes[c.id] }
end
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user&.id, @comments.map(&:id))
@comments.each { |c| c.current_vote = @votes[c.id] } unless @votes.empty?
respond_to do |format|
format.html { render action: "index" }
@ -318,9 +315,7 @@ class CommentsController < ApplicationController
# TODO: respect hidden stories
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id, @comments.map(&:id))
@comments.each do |c|
c.current_vote = @votes[c.id]
end
@comments.each { |c| c.current_vote = @votes[c.id] }
respond_to do |format|
format.html { render action: :index }

View File

@ -79,16 +79,14 @@ class Vote < ApplicationRecord
end
def self.comment_votes_by_user_for_comment_ids_hash(user_id, comment_ids)
if comment_ids.empty?
{}
else
votes = where(
user_id: user_id,
comment_id: comment_ids
)
votes.each_with_object({}) do |v, memo|
memo[v.comment_id] = {vote: v.vote, reason: v.reason}
end
return {} if user_id.nil? || comment_ids.empty?
votes = where(
user_id: user_id,
comment_id: comment_ids
).select(:comment_id, :vote, :reason)
votes.each_with_object({}) do |v, memo|
memo[v.comment_id] = {vote: v.vote, reason: v.reason}
end
end