Pass answer list method into `paginate_answers`

This commit is contained in:
Karina Kwiatek 2023-02-19 15:51:19 +01:00
parent d77919ee01
commit 6fc4049f6c
3 changed files with 10 additions and 6 deletions

View File

@ -9,9 +9,13 @@ class AnswerController < ApplicationController
def show
@answer = Answer.includes(comments: %i[user smiles], question: [:user], smiles: [:user]).find(params[:id])
@subscribed = Subscription.where(user: current_user, answer: @answer).pluck(:id)
@display_all = true
mark_notifications_as_read if user_signed_in?
@subscribed = []
return unless user_signed_in?
@subscribed = Subscription.where(user: current_user, answer: @answer).pluck(:answer_id)
mark_notifications_as_read
end
def pin

View File

@ -2,10 +2,11 @@
module PaginatesAnswers
def paginate_answers
@answers = yield(last_id: params[:last_id])
answer_ids = @answers.map(&:id)
answer_ids += @pinned_answers.pluck(:id) if @pinned_answers.present?
@answers_last_id = answer_ids.min
@more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero?
Subscription.where(user: current_user, answer_id: answer_ids + @pinned_answers.pluck(:id)).pluck(:answer_id) if user_signed_in?
@more_data_available = !yield(last_id: @answers_last_id, size: 1).count.zero?
Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id) if user_signed_in?
end
end

View File

@ -8,9 +8,8 @@ class UserController < ApplicationController
after_action :mark_notification_as_read, only: %i[show]
def show
@answers = @user.cursored_answers(last_id: params[:last_id])
@pinned_answers = @user.answers.pinned.order(pinned_at: :desc).limit(10)
subscribed_answer_ids = paginate_answers
subscribed_answer_ids = paginate_answers { |args| @user.cursored_answers(**args) }
respond_to do |format|
format.html { render locals: { subscribed_answer_ids: } }