Move subscribed answer IDs to an ivar

This commit is contained in:
Karina Kwiatek 2023-03-19 16:00:15 +01:00
parent 9c4b2e452a
commit bbc0afe292
13 changed files with 20 additions and 20 deletions

View file

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

View file

@ -7,6 +7,6 @@ module PaginatesAnswers
answer_ids += @pinned_answers.pluck(:id) if @pinned_answers.present?
@answers_last_id = answer_ids.min
@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?
@subscribed_answer_ids = Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id) if user_signed_in?
end
end

View file

@ -12,8 +12,8 @@ class QuestionController < ApplicationController
@subscribed = Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id) if user_signed_in?
respond_to do |format|
format.html { render locals: { subscribed_answer_ids: } }
format.turbo_stream { render layout: false, status: :see_other, locals: { subscribed_answer_ids: } }
format.html
format.turbo_stream { render layout: false, status: :see_other }
end
end
end

View file

@ -35,11 +35,11 @@ class TimelineController < ApplicationController
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_answer_ids = Subscription.where(user: current_user, answer_id: timeline_ids).pluck(:answer_id)
@subscribed_answer_ids = Subscription.where(user: current_user, answer_id: timeline_ids).pluck(:answer_id)
respond_to do |format|
format.html { render "timeline/timeline", locals: { subscribed_answer_ids: } }
format.turbo_stream { render "timeline/timeline", layout: false, status: :see_other, locals: { subscribed_answer_ids: } }
format.html { render "timeline/timeline" }
format.turbo_stream { render "timeline/timeline", layout: false, status: :see_other }
end
end
end

View file

@ -9,11 +9,11 @@ class UserController < ApplicationController
def show
@pinned_answers = @user.answers.pinned.order(pinned_at: :desc).limit(10)
subscribed_answer_ids = paginate_answers { |args| @user.cursored_answers(**args) }
paginate_answers { |args| @user.cursored_answers(**args) }
respond_to do |format|
format.html { render locals: { subscribed_answer_ids: } }
format.turbo_stream { render layout: false, locals: { subscribed_answer_ids: } }
format.html
format.turbo_stream { render layout: false }
end
end

View file

@ -1,4 +1,4 @@
- provide(:title, answer_title(@answer))
- provide(:og, answer_opengraph(@answer))
.container-lg.container--main
= render "answerbox", a: @answer, display_all: @display_all, subscribed_answer_ids: @subscribed
= render "answerbox", a: @answer, display_all: @display_all, subscribed_answer_ids: @subscribed_answer_ids

View file

@ -21,7 +21,7 @@
.answerbox__answer-date
= link_to(raw(t("time.distance_ago", time: time_tooltip(a))), answer_path(a.user.screen_name, a.id), data: { selection_hotkey: "l" })
.col-md-6.d-flex.d-md-block.answerbox__actions
= render "answerbox/actions", a:, display_all:, subscribed_answer_ids:
= render "answerbox/actions", a:, display_all:
- else
.row
.col-md-6.text-start.text-muted

View file

@ -6,7 +6,7 @@
%button.d-none{ data: { hotkey: "j", action: "navigation#down" } }
%button.d-none{ data: { hotkey: "k", action: "navigation#up" } }
- @answers.each do |a|
= render "answerbox", a:, show_question: false, subscribed_answer_ids:
= render "answerbox", a:, show_question: false, subscribed_answer_ids: @subscribed_answer_ids
- if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start#paginator

View file

@ -1,6 +1,6 @@
= turbo_stream.append "answers" do
- @answers.each do |a|
= render "answerbox", a:, show_question: false, subscribed_answer_ids:
= render "answerbox", a:, show_question: false, subscribed_answer_ids: @subscribed_answer_ids
= turbo_stream.update "paginator" do
- if @more_data_available

View file

@ -2,7 +2,7 @@
%button.d-none{ data: { hotkey: "j", action: "navigation#down" } }
%button.d-none{ data: { hotkey: "k", action: "navigation#up" } }
- @timeline.each do |answer|
= render "answerbox", a: answer, subscribed_answer_ids:
= render "answerbox", a: answer, subscribed_answer_ids: @subscribed_answer_ids
- if @more_data_available
.d-flex.justify-content-center#paginator

View file

@ -1,6 +1,6 @@
= turbo_stream.append "timeline" do
- @timeline.each do |answer|
= render "answerbox", a: answer, subscribed_answer_ids:
= render "answerbox", a: answer, subscribed_answer_ids: @subscribed_answer_ids
= turbo_stream.update "paginator" do
- if @more_data_available

View file

@ -4,11 +4,11 @@
%button.d-none{ data: { hotkey: "k", action: "navigation#up" } }
#pinned-answers
- @pinned_answers.each do |a|
= render "answerbox", a:, subscribed_answer_ids:
= render "answerbox", a:, subscribed_answer_ids: @subscribed_answer_ids
#answers
- @answers.each do |a|
= render "answerbox", a:, subscribed_answer_ids:
= render "answerbox", a:, subscribed_answer_ids: @subscribed_answer_ids
- if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start#paginator

View file

@ -1,6 +1,6 @@
= turbo_stream.append "answers" do
- @answers.each do |a|
= render "answerbox", a:, subscribed_answer_ids:
= render "answerbox", a:
= turbo_stream.update "paginator" do
- if @more_data_available