diff --git a/spec/marten-turbo/handlers/record_delete_spec.cr b/spec/marten-turbo/handlers/record_delete_spec.cr new file mode 100644 index 0000000..87dc147 --- /dev/null +++ b/spec/marten-turbo/handlers/record_delete_spec.cr @@ -0,0 +1,8 @@ +require "./spec_helper" + +describe MartenTurbo::Handlers::RecordCreate do + describe "#post" do + it "" do + end + end +end diff --git a/src/marten_turbo/handlers/record_delete.cr b/src/marten_turbo/handlers/record_delete.cr new file mode 100644 index 0000000..fb2df23 --- /dev/null +++ b/src/marten_turbo/handlers/record_delete.cr @@ -0,0 +1,49 @@ +module MartenTurbo + module Handlers + # Handler for deleting model records, with optional Turbo Stream support. + # + # This handler extends `Marten::Handlers::RecordDelete`, enabling seamless Turbo Stream + # functionality. For Turbo Stream requests, if `turbo_stream_name` is set, the handler + # renders that template. This allows dynamic deletion confirmations and partial page updates. + # + # If no Turbo Stream request is present or no `turbo_stream_name` is defined, the + # handler behaves identically to its parent class, `Marten::Handlers::RecordDelete`. + # + # ``` + # class MyTurboDeleteHandler < MartenTurbo::Handlers::RecordDelete + # model MyModel + # template_name "my_delete.html" + # turbo_stream_name "my_delete.turbo_stream.html" + # success_route_name "my_delete_success" + # end + # ``` + class RecordDelete < Marten::Handlers::RecordDelete + class_getter turbo_stream_name : String? + + def post + perform_deletion + + if request.turbo? && turbo_stream_name + render_turbo_stream context + else + 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