Skip to content

Commit

Permalink
Add toggle_attribute Turbo Stream helper (#44)
Browse files Browse the repository at this point in the history
* Add `toggle_attribute` helper

* Fix implementation and tests
  • Loading branch information
marcoroth authored Dec 15, 2024
1 parent f852ce5 commit 960f1ca
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/turbo_power/stream_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def toggle_css_class(targets = nil, classes = "", **attributes)
custom_action_all :toggle_css_class, targets: targets, attributes: attributes.merge(classes: classes)
end

def toggle_attribute(targets = nil, attribute = "", force = "false", **attributes)
attribute = attributes[:attribute] || attribute
force = attributes[:force] || force

custom_action_all :toggle_attribute, targets: targets, attributes: attributes.merge(attribute: attribute, force: force)
end

def replace_css_class(targets = nil, from = "", to = "", **attributes)
from = attributes[:from] || from
to = attributes[:to] || to
Expand Down
81 changes: 81 additions & 0 deletions test/turbo_power/stream_helper/toggle_attribute_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "test_helper"

module TurboPower
module StreamHelper
class ToggleAttributeTest < StreamHelperTestCase
test "toggle_attribute" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", "disabled")
end

test "toggle_attribute with targets and attribute as kwargs" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute(targets: "#element", attribute: "disabled")
end

test "toggle_attribute with target and attribute as kwargs" do
stream = %(<turbo-stream target="element" action="toggle_attribute" attribute="disabled" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute(target: "element", attribute: "disabled")
end

test "toggle_attribute with attribute and targets as kwargs" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute(attribute: "disabled", targets: "#element")
end

test "toggle_attribute with targets as positional arg and attribute as kwarg" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", attribute: "disabled")
end

test "toggle_attribute with targets/attribute as positional arg and kwarg" do
stream = %(<turbo-stream targets="#better-input" action="toggle_attribute" attribute="other-disabled" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", "disabled", targets: "#better-input", attribute: "other-disabled")
end

test "toggle_attribute with additional arguments" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" something="else" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", attribute: "disabled", something: "else")
end

test "toggle_attribute with force as positional arg" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="true"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", "disabled", "true")
end

test "toggle_attribute with force as kwarg" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="true"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", attribute: "disabled", force: "true")
end

test "toggle_attribute with force as positional arg and kwarg" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="true"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", attribute: "disabled", force: "true")
end

test "toggle_attribute with force as boolean (true)" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="true"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", attribute: "disabled", force: true)
end

test "toggle_attribute with force as boolean (false)" do
stream = %(<turbo-stream targets="#element" action="toggle_attribute" attribute="disabled" force="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.toggle_attribute("#element", attribute: "disabled", force: false)
end
end
end
end

0 comments on commit 960f1ca

Please sign in to comment.