From 7baf8877feace45a7f990ae49404d1ad787ee2b8 Mon Sep 17 00:00:00 2001 From: Marvin Ahlgrimm Date: Fri, 22 Mar 2024 14:13:35 +0100 Subject: [PATCH] Move turbo stream logic to own concern --- .../handlers/concerns/turbo_streamable.cr | 38 +++++++++++++++++++ src/marten_turbo/handlers/record_create.cr | 20 ++-------- src/marten_turbo/handlers/record_delete.cr | 19 ++-------- src/marten_turbo/handlers/record_update.cr | 19 ++-------- 4 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 src/marten_turbo/handlers/concerns/turbo_streamable.cr diff --git a/src/marten_turbo/handlers/concerns/turbo_streamable.cr b/src/marten_turbo/handlers/concerns/turbo_streamable.cr new file mode 100644 index 0000000..fc98b32 --- /dev/null +++ b/src/marten_turbo/handlers/concerns/turbo_streamable.cr @@ -0,0 +1,38 @@ +module MartenTurbo + module Handlers + # Provides the ability to generate turbo stream responses with the content of rendered templates. + module TurboStreamable + macro included + # Returns the configured turbo stream template name. + class_getter turbo_stream_name : String? + + extend MartenTurbo::Handlers::TurboStreamable::ClassMethods + end + + module ClassMethods + # Allows to configure the turbo stream template that should be rendered by the handler. + def turbo_stream_name(turbo_stream_name : String?) + @@turbo_stream_name = turbo_stream_name + end + end + + def render_turbo_stream( + context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, + status : ::HTTP::Status | Int32 = 200 + ) + render(turbo_stream_name.not_nil!, context: context, status: status) + end + + def turbo_stream_name : String | Nil + self.class.turbo_stream_name + end + + def render_turbo_stream( + context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, + status : ::HTTP::Status | Int32 = 200 + ) + render(turbo_stream_name.not_nil!, context: context, status: status) + end + end + end +end diff --git a/src/marten_turbo/handlers/record_create.cr b/src/marten_turbo/handlers/record_create.cr index bf87397..6402907 100644 --- a/src/marten_turbo/handlers/record_create.cr +++ b/src/marten_turbo/handlers/record_create.cr @@ -1,3 +1,5 @@ +require "./concerns/turbo_streamable" + module MartenTurbo module Handlers # Handler for creating model records, with optional Turbo Stream support. @@ -20,7 +22,8 @@ module MartenTurbo # end # ``` class RecordCreate < Marten::Handlers::RecordCreate - class_getter turbo_stream_name : String? + include TurboStreamable + class_getter record_context_name : String = "record" def self.record_context_name(name : String | Symbol) @@ -38,21 +41,6 @@ module MartenTurbo Marten::HTTP::Response::Found.new success_url end end - - def render_turbo_stream( - context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, - status : ::HTTP::Status | Int32 = 200 - ) - render(turbo_stream_name.not_nil!, context: context, status: status) - end - - def self.turbo_stream_name(turbo_stream_name : String?) - @@turbo_stream_name = turbo_stream_name - end - - def turbo_stream_name : String | Nil - self.class.turbo_stream_name - end end end end diff --git a/src/marten_turbo/handlers/record_delete.cr b/src/marten_turbo/handlers/record_delete.cr index c1abfd2..66f5854 100644 --- a/src/marten_turbo/handlers/record_delete.cr +++ b/src/marten_turbo/handlers/record_delete.cr @@ -1,3 +1,5 @@ +require "./concerns/turbo_streamable" + module MartenTurbo module Handlers # Handler for deleting model records, with optional Turbo Stream support. @@ -18,7 +20,7 @@ module MartenTurbo # end # ``` class RecordDelete < Marten::Handlers::RecordDelete - class_getter turbo_stream_name : String? + include TurboStreamable def post perform_deletion @@ -29,21 +31,6 @@ module MartenTurbo Marten::HTTP::Response::Found.new(success_url) end end - - def render_turbo_stream( - context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, - status : ::HTTP::Status | Int32 = 200 - ) - render(turbo_stream_name.not_nil!, context: context, status: status) - end - - def self.turbo_stream_name(turbo_stream_name : String?) - @@turbo_stream_name = turbo_stream_name - end - - def turbo_stream_name : String | Nil - self.class.turbo_stream_name - end end end end diff --git a/src/marten_turbo/handlers/record_update.cr b/src/marten_turbo/handlers/record_update.cr index ac2784e..5da8b80 100644 --- a/src/marten_turbo/handlers/record_update.cr +++ b/src/marten_turbo/handlers/record_update.cr @@ -1,3 +1,5 @@ +require "./concerns/turbo_streamable" + module MartenTurbo module Handlers # Handler for updating model records with optional Turbo Stream support. @@ -19,7 +21,7 @@ module MartenTurbo # end # ``` class RecordUpdate < Marten::Handlers::RecordUpdate - class_getter turbo_stream_name : String? + include TurboStreamable def process_valid_schema record.update!(schema.validated_data.select(model.fields.map(&.id))) @@ -30,21 +32,6 @@ module MartenTurbo Marten::HTTP::Response::Found.new success_url end end - - def render_turbo_stream( - context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, - status : ::HTTP::Status | Int32 = 200 - ) - render(turbo_stream_name.not_nil!, context: context, status: status) - end - - def self.turbo_stream_name(turbo_stream_name : String?) - @@turbo_stream_name = turbo_stream_name - end - - def turbo_stream_name : String | Nil - self.class.turbo_stream_name - end end end end