Skip to content

Commit

Permalink
Move turbo stream logic to own concern
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Mar 22, 2024
1 parent 710dae1 commit 7baf887
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
38 changes: 38 additions & 0 deletions src/marten_turbo/handlers/concerns/turbo_streamable.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module MartenTurbo
module Handlers
# Provides the ability to generate turbo stream responses with the content of rendered templates.
module TurboStreamable
macro included
# Returns the configured turbo stream template name.
class_getter turbo_stream_name : String?

extend MartenTurbo::Handlers::TurboStreamable::ClassMethods
end

module ClassMethods
# Allows to configure the turbo stream template that should be rendered by the handler.
def turbo_stream_name(turbo_stream_name : String?)
@@turbo_stream_name = turbo_stream_name
end
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

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
20 changes: 4 additions & 16 deletions src/marten_turbo/handlers/record_create.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "./concerns/turbo_streamable"

module MartenTurbo
module Handlers
# Handler for creating model records, with optional Turbo Stream support.
Expand All @@ -20,7 +22,8 @@ module MartenTurbo
# end
# ```
class RecordCreate < Marten::Handlers::RecordCreate
class_getter turbo_stream_name : String?
include TurboStreamable

class_getter record_context_name : String = "record"

def self.record_context_name(name : String | Symbol)
Expand All @@ -38,21 +41,6 @@ module MartenTurbo
Marten::HTTP::Response::Found.new success_url
end
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

def self.turbo_stream_name(turbo_stream_name : String?)
@@turbo_stream_name = turbo_stream_name
end

def turbo_stream_name : String | Nil
self.class.turbo_stream_name
end
end
end
end
19 changes: 3 additions & 16 deletions src/marten_turbo/handlers/record_delete.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "./concerns/turbo_streamable"

module MartenTurbo
module Handlers
# Handler for deleting model records, with optional Turbo Stream support.
Expand All @@ -18,7 +20,7 @@ module MartenTurbo
# end
# ```
class RecordDelete < Marten::Handlers::RecordDelete
class_getter turbo_stream_name : String?
include TurboStreamable

def post
perform_deletion
Expand All @@ -29,21 +31,6 @@ module MartenTurbo
Marten::HTTP::Response::Found.new(success_url)
end
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

def self.turbo_stream_name(turbo_stream_name : String?)
@@turbo_stream_name = turbo_stream_name
end

def turbo_stream_name : String | Nil
self.class.turbo_stream_name
end
end
end
end
19 changes: 3 additions & 16 deletions src/marten_turbo/handlers/record_update.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "./concerns/turbo_streamable"

module MartenTurbo
module Handlers
# Handler for updating model records with optional Turbo Stream support.
Expand All @@ -19,7 +21,7 @@ module MartenTurbo
# end
# ```
class RecordUpdate < Marten::Handlers::RecordUpdate
class_getter turbo_stream_name : String?
include TurboStreamable

def process_valid_schema
record.update!(schema.validated_data.select(model.fields.map(&.id)))
Expand All @@ -30,21 +32,6 @@ module MartenTurbo
Marten::HTTP::Response::Found.new success_url
end
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

def self.turbo_stream_name(turbo_stream_name : String?)
@@turbo_stream_name = turbo_stream_name
end

def turbo_stream_name : String | Nil
self.class.turbo_stream_name
end
end
end
end

0 comments on commit 7baf887

Please sign in to comment.