Skip to content

Commit

Permalink
Allow to use custom turbo_stream method instead of turbo_stream templ…
Browse files Browse the repository at this point in the history
…ate in generic handlers
  • Loading branch information
treagod committed Jul 12, 2024
1 parent c1c61d4 commit 22324a8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
37 changes: 37 additions & 0 deletions spec/marten-turbo/handlers/record_create_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ describe MartenTurbo::Handlers::RecordCreate do
response.content.strip.should contain "</turbo-stream>"
end

it (
"renders a turbo stream append action containing the newly created record if" \
"a turbo request is handled and a custom turbo render is defined"
) do
request = Marten::HTTP::Request.new(
::HTTP::Request.new(
method: "POST",
resource: "",
headers: HTTP::Headers{
"Host" => "example.com",
"Content-Type" => "application/x-www-form-urlencoded",
"Accept" => "text/vnd.turbo-stream.html",
},
body: "name=new-turbo-tag"
)
)
handler = MartenTurbo::Handlers::RecordCreateSpec::TestWithCustomRenderMethod.new(request)

response = handler.post

response.should_not be_a Marten::HTTP::Response::Found
response.content.strip.should contain "<turbo-stream action=\"append\" target=\"messages\">"
response.content.strip.should contain "<div>New Message</div>"
response.content.strip.should contain "</turbo-stream>"
end

it "renders a Turbo Stream append action containing no record when a wrong template variable is given" do
request = Marten::HTTP::Request.new(
::HTTP::Request.new(
Expand Down Expand Up @@ -139,4 +165,15 @@ module MartenTurbo::Handlers::RecordCreateSpec
success_route_name "dummy"
template_name "tags/create.html"
end

class TestWithCustomRenderMethod < MartenTurbo::Handlers::RecordCreate
model Tag
schema TagCreateSchema
success_route_name "dummy"
template_name "tags/create.html"

def turbo_stream
MartenTurbo::TurboStream.append("messages", "<div>New Message</div>")
end
end
end
14 changes: 13 additions & 1 deletion src/marten_turbo/handlers/concerns/turbo_streamable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ module MartenTurbo
context : Hash | NamedTuple | Nil | Marten::Template::Context = nil,
status : ::HTTP::Status | Int32 = 200
)
render(turbo_stream_name.not_nil!, context: context, status: status, content_type: TURBO_CONTENT_TYPE)
if stream = turbo_stream.try(&.to_s)
respond(stream, status: status, content_type: TURBO_CONTENT_TYPE)
else
render(turbo_stream_name.not_nil!, context: context, status: status, content_type: TURBO_CONTENT_TYPE)
end
# stream = turbo_stream || turbo_stream_name.not_nil!
end

def turbo_stream
end

def turbo_streamable?
!!(turbo_stream || turbo_stream_name)
end

def turbo_stream_name : String | Nil
Expand Down
2 changes: 1 addition & 1 deletion src/marten_turbo/handlers/record_create.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module MartenTurbo
self.record = model.new(schema.validated_data)
self.record.try(&.save!)

if request.turbo? && turbo_stream_name
if request.turbo? && turbo_streamable?
context[self.class.record_context_name] = record
render_turbo_stream context
else
Expand Down
2 changes: 1 addition & 1 deletion src/marten_turbo/handlers/record_delete.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module MartenTurbo
def post
perform_deletion

if request.turbo? && turbo_stream_name
if request.turbo? && turbo_streamable?
render_turbo_stream context
else
Marten::HTTP::Response::Found.new(success_url)
Expand Down
2 changes: 1 addition & 1 deletion src/marten_turbo/handlers/record_update.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module MartenTurbo
def process_valid_schema
record.update!(schema.validated_data.select(model.fields.map(&.id)))

if request.turbo? && turbo_stream_name
if request.turbo? && turbo_streamable?
render_turbo_stream context
else
Marten::HTTP::Response::Found.new success_url
Expand Down

0 comments on commit 22324a8

Please sign in to comment.