Skip to content

Commit

Permalink
Rename create_dom_id to dom_id and make it more general
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Aug 27, 2024
1 parent e8763f3 commit 271de1d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
24 changes: 15 additions & 9 deletions src/marten_turbo/concerncs/dom_identifier.cr
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
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
@[Deprecated("Use `#dom_id` instead")]
def create_dom_id(value, prefix : String | Symbol | Nil = nil)
dom_id(value, prefix)
end

def dom_id(value, prefix : String | Symbol | Nil = nil)
dom_id = value.to_s
prefix ? "#{prefix}_#{dom_id}" : dom_id
end

@[Deprecated("Use `#dom_id` instead")]
def create_dom_id(value : Marten::Model, prefix : String | Symbol | Nil = nil)
dom_id(value, prefix)
end

def create_dom_id(model : Marten::Model, prefix : Marten::Template::Value? = nil)
generate_id_for_model(model, prefix)
def dom_id(value : Marten::Model, prefix : String | Symbol | Nil = nil)
generate_id_for_model(value, prefix)
end

private def formatted_prefix(prefix)
Expand Down
2 changes: 1 addition & 1 deletion src/marten_turbo/template/tag/dom_id.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module MartenTurbo
end

def render(context : Marten::Template::Context) : String
create_dom_id @instance_name.resolve(context), @prefix.try(&.resolve(context))
dom_id @instance_name.resolve(context).raw, @prefix.try(&.resolve(context).try(&.to_s))
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/marten_turbo/template/tag/turbo_stream.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ module MartenTurbo
content = template.render(context)
end

MartenTurbo::TurboStream.action(action, create_dom_id(@target_id.resolve(context)), content).to_s
target = @target_id.resolve(context)

MartenTurbo::TurboStream.action(action, dom_id(target.raw), content).to_s
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/marten_turbo/turbo_stream.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module MartenTurbo
# stream.replace("append", Message.get(pk: 1), "<div>Updated Message</div>")
# ```
def action(action, target : Marten::Model, content)
target_id = create_dom_id(target)
target_id = dom_id(target)
@streams << <<-TURBO_STREAM_TAG
<turbo-stream action="#{action}" target="#{target_id}">
#{render_template_tag(content)}
Expand Down

0 comments on commit 271de1d

Please sign in to comment.