diff --git a/README.md b/README.md
index 6d85869..21b9312 100644
--- a/README.md
+++ b/README.md
@@ -162,7 +162,7 @@ import 'controllers'
### Notification Actions
-* `turbo_stream.notification(title, options, **attributes)`
+* `turbo_stream.notification(title, **options)`
### Turbo Actions
diff --git a/lib/turbo_power.rb b/lib/turbo_power.rb
index b4e45f1..a8ec1b2 100644
--- a/lib/turbo_power.rb
+++ b/lib/turbo_power.rb
@@ -4,6 +4,7 @@
require_relative "turbo_power/version"
require_relative "turbo_power/engine"
+require_relative "turbo_power/attribute_transformations"
require_relative "turbo_power/stream_helper"
module TurboPower
diff --git a/lib/turbo_power/attribute_transformations.rb b/lib/turbo_power/attribute_transformations.rb
new file mode 100644
index 0000000..5f5d8e5
--- /dev/null
+++ b/lib/turbo_power/attribute_transformations.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module TurboPower
+ module AttributeTransformations
+ private
+
+ def transform_attributes(attributes)
+ attributes
+ .transform_keys { |key| transform_key(key) }
+ .transform_values { |value| transform_value(value) }
+ end
+
+ def transform_key(key)
+ key.to_s.underscore.dasherize.to_sym
+ end
+
+ def transform_value(value)
+ case value
+ when String
+ value
+ when NilClass
+ ""
+ else
+ value.to_json
+ end
+ end
+ end
+end
diff --git a/lib/turbo_power/stream_helper.rb b/lib/turbo_power/stream_helper.rb
index d89eb7c..a202d56 100644
--- a/lib/turbo_power/stream_helper.rb
+++ b/lib/turbo_power/stream_helper.rb
@@ -2,15 +2,13 @@
module TurboPower
module StreamHelper
+ include ::TurboPower::AttributeTransformations
+
# Custom Action Helpers
## Also see:
## => https://github.com/hotwired/turbo-rails/pull/374
- private def transform_attributes(attributes)
- attributes.transform_keys { |key| key.to_s.underscore.dasherize.to_sym }
- end
-
def custom_action(name, target: nil, content: nil, attributes: {})
turbo_stream_action_tag name, target: target, template: content, **transform_attributes(attributes)
end
@@ -167,7 +165,6 @@ def set_title(title, **attributes)
custom_action :set_title, attributes: attributes.merge(title: title)
end
-
# Browser History Actions
def history_go(delta, **attributes)
@@ -194,8 +191,8 @@ def console_table(data, columns, **attributes)
# Notification Actions
- def notification(title, options, **attributes)
- custom_action :notification, attributes: attributes.merge(title: title, options: options)
+ def notification(title = nil, **attributes)
+ custom_action :notification, attributes: { title: title }.merge(attributes)
end
# Turbo Actions
diff --git a/test/turbo_power/stream_helper_test.rb b/test/turbo_power/stream_helper_test.rb
index aa5ff5f..5ac8fcf 100644
--- a/test/turbo_power/stream_helper_test.rb
+++ b/test/turbo_power/stream_helper_test.rb
@@ -32,7 +32,7 @@ class StreamHelperTest < ActionView::TestCase
end
test "turbo_progress_bar_set_value with nil" do
- stream = %()
+ stream = %()
assert_dom_equal stream, turbo_stream.turbo_progress_bar_set_value(nil)
end
@@ -85,6 +85,64 @@ class StreamHelperTest < ActionView::TestCase
assert_dom_equal stream, turbo_stream.turbo_clear_cache
end
+ test "notification with just title" do
+ stream = %()
+
+ assert_dom_equal stream, turbo_stream.notification("A title")
+ end
+
+ test "notification with title and option" do
+ stream = %()
+
+ assert_dom_equal stream, turbo_stream.notification("A title", body: "A body")
+ end
+
+ test "notification with title and all options" do
+ stream = %(
+
+ ).squish
+
+ options = {
+ dir: "ltr",
+ lang: "EN",
+ badge: "https://example.com/badge.png",
+ body: "This is displayed below the title.",
+ tag: "Demo",
+ icon: "https://example.com/icon.png",
+ image: "https://example.com/image.png",
+ data: { arbitrary: "data" },
+ vibrate: [200, 100, 200],
+ renotify: true,
+ requireInteraction: true,
+ actions: [{ action: "respond", title: "Please respond", icon: "https://example.com/icon.png" }],
+ silent: true
+ }
+
+ assert_dom_equal stream, turbo_stream.notification("A title", **options)
+ end
+
+ test "notification with title kwarg" do
+ stream = %()
+
+ assert_dom_equal stream, turbo_stream.notification(title: "A title")
+ end
+
test "attributes transformation" do
stream = %()