From 6fc4049f6c250ecf2e65606e129ac4960016f830 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 19 Feb 2023 15:51:19 +0100 Subject: [PATCH] Pass answer list method into `paginate_answers` --- app/controllers/answer_controller.rb | 8 ++++++-- app/controllers/concerns/paginates_answers.rb | 5 +++-- app/controllers/user_controller.rb | 3 +-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/answer_controller.rb b/app/controllers/answer_controller.rb index 3d520116..56577a41 100644 --- a/app/controllers/answer_controller.rb +++ b/app/controllers/answer_controller.rb @@ -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 diff --git a/app/controllers/concerns/paginates_answers.rb b/app/controllers/concerns/paginates_answers.rb index 83fd5293..bd49d4e2 100644 --- a/app/controllers/concerns/paginates_answers.rb +++ b/app/controllers/concerns/paginates_answers.rb @@ -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 diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 0fc30366..1fe36a4f 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -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: } }