From 422143925912efea43cf33277d19a8702e1c35ea Mon Sep 17 00:00:00 2001 From: Marvin Ahlgrimm Date: Mon, 8 Apr 2024 14:31:27 +0200 Subject: [PATCH] Fix `turbo_stream` tag and use correct `Content-Type` --- .../template/tag/turbo_stream_spec.cr | 12 +++++----- .../handlers/concerns/turbo_streamable.cr | 9 +------ src/marten_turbo/template/tag.cr | 1 + src/marten_turbo/template/tag/turbo_stream.cr | 24 +++++++------------ 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/spec/marten-turbo/template/tag/turbo_stream_spec.cr b/spec/marten-turbo/template/tag/turbo_stream_spec.cr index fd13eac..a67b70b 100644 --- a/spec/marten-turbo/template/tag/turbo_stream_spec.cr +++ b/spec/marten-turbo/template/tag/turbo_stream_spec.cr @@ -14,13 +14,13 @@ describe MartenTurbo::Template::Tag::TurboStream do end it "raises if turbo_stream does not define a target_id" do - parser = Marten::Template::Parser.new("{% turbo_stream.append %}") + parser = Marten::Template::Parser.new("{% turbo_stream 'append' %}") expect_raises( Marten::Template::Errors::InvalidSyntax, - "Malformed turbo_stream tag: you must define a target id" + "Malformed turbo_stream tag: you must define an action and a target id" ) do - MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream.append") + MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream 'append'") end end @@ -35,7 +35,7 @@ describe MartenTurbo::Template::Tag::TurboStream do Marten::Template::Errors::InvalidSyntax, "Unclosed tags, expected: end_turbostream" ) do - MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream.append 'tags' do") + MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream 'append' 'tags' do") end end end @@ -43,7 +43,7 @@ describe MartenTurbo::Template::Tag::TurboStream do describe "#render" do it "properly renders a turbo-stream tag with the correct action and target" do parser = Marten::Template::Parser.new("") - tag = MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream.remove 'my-id'") + tag = MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream 'remove' 'my-id'") content = tag.render(Marten::Template::Context.new) content.should contain "" @@ -54,7 +54,7 @@ describe MartenTurbo::Template::Tag::TurboStream do tag_model = Tag.create!(name: "Marten Turbo") parser = Marten::Template::Parser.new("") - tag = MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream.remove tag") + tag = MartenTurbo::Template::Tag::TurboStream.new(parser, "turbo_stream 'remove' tag") context = Marten::Template::Context{"tag" => tag_model} diff --git a/src/marten_turbo/handlers/concerns/turbo_streamable.cr b/src/marten_turbo/handlers/concerns/turbo_streamable.cr index fc98b32..5512b4e 100644 --- a/src/marten_turbo/handlers/concerns/turbo_streamable.cr +++ b/src/marten_turbo/handlers/concerns/turbo_streamable.cr @@ -20,19 +20,12 @@ 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) + render(turbo_stream_name.not_nil!, context: context, status: status, content_type: "text/vnd.turbo-stream.html") 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/template/tag.cr b/src/marten_turbo/template/tag.cr index 5b65521..83c5a2b 100644 --- a/src/marten_turbo/template/tag.cr +++ b/src/marten_turbo/template/tag.cr @@ -4,6 +4,7 @@ module MartenTurbo module Template module Tag Marten::Template::Tag.register "dom_id", DomId + Marten::Template::Tag.register "turbo_stream", TurboStream end end end diff --git a/src/marten_turbo/template/tag/turbo_stream.cr b/src/marten_turbo/template/tag/turbo_stream.cr index e58a394..1104804 100644 --- a/src/marten_turbo/template/tag/turbo_stream.cr +++ b/src/marten_turbo/template/tag/turbo_stream.cr @@ -9,29 +9,22 @@ module MartenTurbo include Identifiable @turbo_stream_nodes : Marten::Template::NodeSet? - @action : String + @action_filter : Marten::Template::FilterExpression @target_id : Marten::Template::Variable def initialize(parser : Marten::Template::Parser, source : String) parts = split_smartly(source) - tag_name_parts = parts[0].split(".") - if tag_name_parts.size != 2 + if parts.size < 3 raise Marten::Template::Errors::InvalidSyntax.new( - "Malformed turbo_stream tag: you must define an action" + "Malformed turbo_stream tag: you must define an action and a target id" ) end - @action = tag_name_parts[1] + @action_filter = Marten::Template::FilterExpression.new(parts[1]) - if parts.size < 2 - raise Marten::Template::Errors::InvalidSyntax.new( - "Malformed turbo_stream tag: you must define a target id" - ) - end - - @target_id = Marten::Template::Variable.new(parts[1]) + @target_id = Marten::Template::Variable.new(parts[2]) if parts[-1] == "do" @turbo_stream_nodes = parser.parse(up_to: {"end_turbostream"}) @@ -57,15 +50,16 @@ module MartenTurbo TEMPLATE_TAG end - private def render_turbo_stream_tag(target_id, content) + private def render_turbo_stream_tag(action, target_id, content) <<-TURBO_STREAM_TAG - + #{render_template_tag(content)} TURBO_STREAM_TAG end def render(context : Marten::Template::Context) : String + action = @action_filter.resolve(context).to_s content = if turbo_stream_nodes = @turbo_stream_nodes turbo_stream_nodes.render(context) else @@ -98,7 +92,7 @@ module MartenTurbo content = template.render(context) end - render_turbo_stream_tag(create_dom_id(@target_id.resolve(context)), content) + render_turbo_stream_tag(action, create_dom_id(@target_id.resolve(context)), content) end end end