Prefetch subscriptions

This commit is contained in:
Karina Kwiatek 2023-02-16 23:51:38 +01:00
parent 2ec52210f4
commit 2604e6b240
4 changed files with 10 additions and 4 deletions

View File

@ -4,8 +4,10 @@ class QuestionController < ApplicationController
def show
@question = Question.find(params[:id])
@answers = @question.cursored_answers(last_id: params[:last_id], current_user:)
@answers_last_id = @answers.map(&:id).min
answer_ids = @answers.map(&:id)
@answers_last_id = answer_ids.min
@more_data_available = !@question.cursored_answers(last_id: @answers_last_id, size: 1, current_user:).count.zero?
@subscribed = Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id) if user_signed_in?
respond_to do |format|
format.html

View File

@ -32,8 +32,10 @@ class TimelineController < ApplicationController
def paginate_timeline
@timeline = yield(last_id: params[:last_id])
@timeline_last_id = @timeline.map(&:id).min
timeline_ids = @timeline.map(&:id)
@timeline_last_id = timeline_ids.min
@more_data_available = !yield(last_id: @timeline_last_id, size: 1).count.zero?
@subscribed = Subscription.where(user: current_user, answer_id: timeline_ids).pluck(:answer_id) if user_signed_in?
respond_to do |format|
format.html { render "timeline/timeline" }

View File

@ -7,9 +7,11 @@ class UserController < ApplicationController
def show
@answers = @user.cursored_answers(last_id: params[:last_id])
answer_ids = @answers.map(&:id)
@pinned_answers = @user.answers.pinned.order(pinned_at: :desc).limit(10)
@answers_last_id = @answers.map(&:id).min
@answers_last_id = answer_ids.min
@more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero?
@subscribed = Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id) if user_signed_in?
respond_to do |format|
format.html

View File

@ -1,5 +1,5 @@
.dropdown-menu.dropdown-menu-end{ role: :menu }
- if Subscription.is_subscribed(current_user, answer)
- if @subscribed&.include?(answer.id)
-# fun joke should subscribe?
%a.dropdown-item{ href: "#", data: { a_id: answer.id, action: "ab-submarine", torpedo: "no" } }
%i.fa.fa-fw.fa-anchor