Skip to content

Commit

Permalink
Allow turbo stream instances to be created from Marten::Model
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Jul 17, 2024
1 parent 589767e commit adc2404
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 38 deletions.
19 changes: 19 additions & 0 deletions spec/marten-turbo/turbo_stream_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ describe MartenTurbo::TurboStream do
stream.to_s.should_not contain "<template>"
stream.to_s.should contain "</turbo-stream>"
end

it "adds a remove action to the streams using a record" do
tag = Tag.create!(name: "Tag 1")
stream = MartenTurbo::TurboStream.new
stream.remove(tag)

stream.to_s.should contain "<turbo-stream action=\"remove\" target=\"tag_#{tag.pk!}\">"
stream.to_s.should_not contain "<template>"
stream.to_s.should contain "</turbo-stream>"
end
end

describe "Class methods" do
Expand All @@ -67,5 +77,14 @@ describe MartenTurbo::TurboStream do
stream.to_s.should_not contain "<template>"
stream.to_s.should contain "</turbo-stream>"
end

it "::remove accepts a model returns a new TurboStream with the remove action" do
tag = Tag.create!(name: "Tag 1")
stream = MartenTurbo::TurboStream.remove(tag)

stream.to_s.should contain "<turbo-stream action=\"remove\" target=\"tag_#{tag.pk!}\">"
stream.to_s.should_not contain "<template>"
stream.to_s.should contain "</turbo-stream>"
end
end
end
1 change: 1 addition & 0 deletions src/marten_turbo/app.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "./concerncs/**"
require "./turbo_stream"
require "./ext/**"
require "./handlers/**"
Expand Down
31 changes: 31 additions & 0 deletions src/marten_turbo/concerncs/dom_identifier.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module MartenTurbo
module Identifiable
def create_dom_id(value : Marten::Template::Value, prefix : Marten::Template::Value? = nil)
if value.raw.is_a? Marten::Model
create_dom_id(value.raw.as(Marten::Model), prefix)
else
dom_id = value.to_s
prefix ? "#{prefix}_#{dom_id}" : dom_id
end
end

def create_dom_id(model : Marten::Model, prefix : Marten::Template::Value? = nil)
generate_id_for_model(model, prefix)
end

private def formatted_prefix(prefix)
prefix ? "#{prefix}_" : ""
end

private def generate_id_for_model(model, prefix)
identifier = model.class.name.downcase.gsub(RE_NAMESPACE_IDENTIFIER, '_')
if model.new_record?
"#{formatted_prefix(prefix)}new_#{identifier}"
else
"#{formatted_prefix(prefix)}#{identifier}_#{model.pk}"
end
end

RE_NAMESPACE_IDENTIFIER = /(?:\:\:|\.)/
end
end
31 changes: 0 additions & 31 deletions src/marten_turbo/template/tag/concerncs/dom_identifier.cr

This file was deleted.

2 changes: 0 additions & 2 deletions src/marten_turbo/template/tag/dom_id.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "./concerncs/dom_identifier"

module MartenTurbo
module Template
module Tag
Expand Down
2 changes: 0 additions & 2 deletions src/marten_turbo/template/tag/turbo_stream.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "./concerncs/dom_identifier"

module MartenTurbo
module Template
module Tag
Expand Down
25 changes: 22 additions & 3 deletions src/marten_turbo/turbo_stream.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module MartenTurbo
class TurboStream
include Identifiable

ACTIONS = %w[append prepend replace update remove before after]

def initialize
Expand All @@ -21,7 +23,24 @@ module MartenTurbo
# stream = MartenTurbo::TurboStream.new
# stream.action("append", "messages", "<div>New Message</div>")
# ```
def action(action, target_id, content)
def action(action, target_id : String, content)
@streams << <<-TURBO_STREAM_TAG
<turbo-stream action="#{action}" target="#{target_id}">
#{render_template_tag(content)}
</turbo-stream>
TURBO_STREAM_TAG

self
end

# Creates a new TurboStream instance and adds a single action.
#
# ```
# stream = MartenTurbo::TurboStream.new
# stream.replace("append", Message.get(pk: 1), "<div>Updated Message</div>")
# ```
def action(action, target : Marten::Model, content)
target_id = create_dom_id(target)
@streams << <<-TURBO_STREAM_TAG
<turbo-stream action="#{action}" target="#{target_id}">
#{render_template_tag(content)}
Expand All @@ -33,15 +52,15 @@ module MartenTurbo

{% for action in ACTIONS %}
# Adds a turbo stream {{ action.id }} action to the streams array.
def {{ action.id }}(target : String, content : String? = nil)
def {{ action.id }}(target, content : String? = nil)
action("{{ action.id }}", target, content)

self
end

# Creates a a turbo stream instance with a {{ action.id }} action
# already in its array.
def self.{{ action.id }}(target : String, content : String? = nil)
def self.{{ action.id }}(target, content : String? = nil)
self.new.action("{{ action.id }}", target, content)
end
{% end %}
Expand Down

0 comments on commit adc2404

Please sign in to comment.