Skip to content

Commit

Permalink
Accept block for stream initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Aug 27, 2024
1 parent 271de1d commit 6ba3bf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/marten-turbo/turbo_stream_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ describe MartenTurbo::TurboStream do
stream = MartenTurbo::TurboStream.new
stream.to_s.should eq ""
end

it "accepts a block to initialize the stream" do
stream = MartenTurbo::TurboStream.new do |stream|
stream.append("messages", "<div>Message 1</div>")
end

stream.to_s.should contain "<turbo-stream action=\"append\" target=\"messages\">"
stream.to_s.should contain "<div>Message 1</div>"
stream.to_s.should contain "</turbo-stream>"
end
end

describe "#append" do
Expand Down
5 changes: 5 additions & 0 deletions src/marten_turbo/turbo_stream.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module MartenTurbo
@streams = [] of String
end

def initialize
@streams = [] of String
yield self
end

# Creates a new TurboStream instance and adds a single action.
#
# ```
Expand Down

0 comments on commit 6ba3bf1

Please sign in to comment.