Skip to content

Commit

Permalink
Simplify #action
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Aug 28, 2024
1 parent 6482d9f commit e896090
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/marten_turbo/turbo_stream.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module MartenTurbo
# stream = MartenTurbo::TurboStream.new
# stream.action("append", "messages", "<div>New Message</div>")
# ```
def action(action, target_id : String, content)
def action(action, target : String | Marten::Model, content)
target_id = target.is_a?(String) ? target : dom_id(target.as(Marten::Model))
@streams << <<-TURBO_STREAM_TAG
<turbo-stream action="#{action}" target="#{target_id}">
#{render_template_tag(content)}
Expand All @@ -42,17 +43,10 @@ module MartenTurbo
#
# ```
# stream = MartenTurbo::TurboStream.new
# stream.replace("append", Message.get(pk: 1), "<div>Updated Message</div>")
# stream.replace("append", Message.get!(pk: 1), "<div>Updated Message</div>")
# ```
def action(action, target : Marten::Model, content)
target_id = dom_id(target)
@streams << <<-TURBO_STREAM_TAG
<turbo-stream action="#{action}" target="#{target_id}">
#{render_template_tag(content)}
</turbo-stream>
TURBO_STREAM_TAG

self
action(action, dom_id(target), content)
end

{% for action in ACTIONS %}
Expand Down

0 comments on commit e896090

Please sign in to comment.