retrospring/app/controllers/subscriptions_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

42 lines
1.2 KiB
Ruby
Raw Normal View History

2023-10-25 02:59:27 +02:00
# frozen_string_literal: true
2023-10-25 02:50:10 +02:00
class SubscriptionsController < ApplicationController
include TurboStreamable
before_action :authenticate_user!
turbo_stream_actions :create, :destroy
def create
answer = Answer.find(params[:answer])
result = Subscription.subscribe(current_user, answer)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("subscription-#{answer.id}", partial: "subscriptions/destroy", locals: { answer: }),
render_toast(t(result.present? ? ".success" : ".error"), result.present?)
]
end
format.html { redirect_to answer_path(username: answer.user.screen_name, id: answer.id) }
end
end
def destroy
answer = Answer.find(params[:answer])
result = Subscription.unsubscribe(current_user, answer)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("subscription-#{answer.id}", partial: "subscriptions/create", locals: { answer: }),
render_toast(t(result.present? ? ".success" : ".error"), result.present?)
]
end
format.html { redirect_to answer_path(username: answer.user.screen_name, id: answer.id) }
end
end
end