Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream helpers powered by turbo-ruby #18

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/turbo_power.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "turbo-rails"
require "phlex-rails"
require "turbo/ruby"
require "turbo_ready"

require_relative "turbo_power/version"
Expand Down
37 changes: 37 additions & 0 deletions lib/turbo_power/stream_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
# frozen_string_literal: true

module TurboPower

class StreamHelperClass
def initialize(buffer = nil)
@buffer = buffer || String.new
end

def to_s
@buffer.to_s
end

def morph(...)
@buffer << self.class.morph(...)
end

def console_log(...)
@buffer << self.class.console_log(...)
end

def self.morph(target:, content: nil, html: nil, **attributes, &block)
Turbo::Elements::TurboStream.new(action: "morph", targets: target, content: content || html, **attributes, &block).to_html
end

def self.console_log(message:, level: :log, **attributes)
Turbo::Elements::TurboStream.new(action: "console_log", attributes: attributes.merge(message: message, level: level)).to_html
end
end

class StreamsBuilder
def self.render(&block)
turbo_stream = StreamHelperClass.new

yield turbo_stream if block_given?

turbo_stream.to_s
end
end

module StreamHelper
# Custom Action Helpers

Expand Down
34 changes: 34 additions & 0 deletions test/turbo_power/stream_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,44 @@ class StreamHelperTest < ActionView::TestCase
assert_dom_equal stream, turbo_stream.dispatch_event("#element", "custom-event", detail: { count: 1, type: "custom", enabled: true, ids: [1, 2, 3] })
end

test "morph" do
stream = %(<turbo-stream action="morph" targets="#element"><template><h1>Hello</h1></template></turbo-stream>)

assert_dom_equal stream, TurboPower::StreamHelperClass.morph(target: "#element", content: "<h1>Hello</h1>")
end


test "morph block" do
stream = %(<turbo-stream action="morph" targets="#element"><template><h1>Hello</h1></template></turbo-stream>)

actual = TurboPower::StreamHelperClass.morph(target: "#element") do
"<h1>Hello</h1>"
end

assert_dom_equal stream, actual
end

test "console_log" do
stream = %(<turbo-stream action="console_log" level="log" message="Message"></turbo-stream>)

assert_dom_equal stream, TurboPower::StreamHelperClass.console_log(message: "Message")
end

test "reset_form" do
stream = %(<turbo-stream action="reset_form" targets="#form"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.reset_form("#form")
end

test "render" do
stream = %(<turbo-stream action="console_log" message="hello" level="log"></turbo-stream><turbo-stream action="morph" targets="#hello"><template>hello world</template></turbo-stream>)

rendered = TurboPower::StreamsBuilder.render do |turbo_stream|
turbo_stream.console_log(message: "hello")
turbo_stream.morph(target: "#hello", content: "hello world")
end

assert_dom_equal stream, rendered
end
end
end
2 changes: 2 additions & 0 deletions turbo_power.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Gem::Specification.new do |spec|
"[A-Z]*"
]

spec.add_dependency "phlex-rails", "~> 0.4"
spec.add_dependency "turbo-rails", "~> 1.3.0"
spec.add_dependency "turbo_ready"
spec.add_dependency "turbo-ruby", "~> 0.1"
end