Skip to content

Commit

Permalink
Add RecordDelete class
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Mar 20, 2024
1 parent e7aba4f commit 5d3a971
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/marten-turbo/handlers/record_delete_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "./spec_helper"

describe MartenTurbo::Handlers::RecordCreate do
describe "#post" do
it "" do
end
end
end
49 changes: 49 additions & 0 deletions src/marten_turbo/handlers/record_delete.cr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5d3a971

Please sign in to comment.