From 5a192a3598d125e5760d4622b25cf481f784b16c Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 11 Feb 2023 14:41:36 +0100 Subject: [PATCH] Turn `turbo_stream_actions` into a class method Co-Authored-By: Georg Gadinger --- app/controllers/anonymous_block_controller.rb | 6 +++--- app/controllers/concerns/turbo_streamable.rb | 10 ++++------ app/controllers/settings/mutes_controller.rb | 6 +++--- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/controllers/anonymous_block_controller.rb b/app/controllers/anonymous_block_controller.rb index c71ade6c..a8c9a32f 100644 --- a/app/controllers/anonymous_block_controller.rb +++ b/app/controllers/anonymous_block_controller.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true class AnonymousBlockController < ApplicationController + include TurboStreamable + before_action :authenticate_user! - def turbo_stream_actions = %i[create destroy] - - include TurboStreamable + turbo_stream_actions :create, :destroy def create params.require :question diff --git a/app/controllers/concerns/turbo_streamable.rb b/app/controllers/concerns/turbo_streamable.rb index 694b4a7c..fd1d8f86 100644 --- a/app/controllers/concerns/turbo_streamable.rb +++ b/app/controllers/concerns/turbo_streamable.rb @@ -3,18 +3,16 @@ module TurboStreamable extend ActiveSupport::Concern - included do |controller| - around_action :handle_error, only: controller.respond_to?(:turbo_stream_actions) ? controller.turbo_stream_actions : [] + class_methods do + def turbo_stream_actions(*actions) + around_action :handle_error, only: actions + end end def render_toast(message, success = true) turbo_stream.append("toasts", partial: "shared/toast", locals: { message:, success: }) end - class_methods do - def render_toast = render_toast - end - private def handle_error diff --git a/app/controllers/settings/mutes_controller.rb b/app/controllers/settings/mutes_controller.rb index 8ad0ab72..1000b862 100644 --- a/app/controllers/settings/mutes_controller.rb +++ b/app/controllers/settings/mutes_controller.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true class Settings::MutesController < ApplicationController + include TurboStreamable + before_action :authenticate_user! - def turbo_stream_actions = %i[create destroy] - - include TurboStreamable + turbo_stream_actions :create, :destroy def index @users = current_user.muted_users