Skip to content

Commit 67cd84c

Browse files
authored
Introduce RenderHelper to reuse StreamHelpers in model broadcasts (#43)
* Introduce `TurboPower::RenderHelper` to reuse `StreamHelpers` in model broadcasts * Update dummy app
1 parent 86a65ee commit 67cd84c

File tree

18 files changed

+168
-36
lines changed

18 files changed

+168
-36
lines changed

.github/workflows/tests.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ on:
44
push:
55
pull_request:
66

7+
env:
8+
RAILS_ENV: test
9+
710
jobs:
811
build:
912
runs-on: ubuntu-latest
@@ -26,5 +29,9 @@ jobs:
2629
ruby-version: ${{ matrix.ruby }}
2730
bundler-cache: true
2831

32+
- name: Setup database
33+
run: bundle exec rake db:create db:migrate
34+
working-directory: test/dummy
35+
2936
- name: Run tests
3037
run: bundle exec rake test

Gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ gem "rake", "~> 13.0"
88
gem "rubocop", "~> 1.21"
99

1010
group :test do
11-
gem "sprockets-rails"
12-
gem "sqlite3"
11+
gem "rails"
1312
gem "simplecov"
13+
gem "sprockets-rails"
14+
gem "sqlite3", "~> 1.3"
1415
end

lib/turbo_power.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative "turbo_power/engine"
77
require_relative "turbo_power/attribute_transformations"
88
require_relative "turbo_power/stream_helper"
9+
require_relative "turbo_power/render_helper"
910
require_relative "turbo_power/broadcasts"
1011
require_relative "turbo_power/broadcastable"
1112

lib/turbo_power/broadcasts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Broadcasts
1717
def custom_broadcast_action_to(*streamables, action:, **attributes)
1818
broadcast_stream_to(
1919
*streamables,
20-
content: turbo_stream_action_tag(action, **attributes)
20+
content: RenderHelper.public_send(action, **attributes)
2121
)
2222
end
2323

lib/turbo_power/render_helper.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module TurboPower
2+
class RenderHelper
3+
extend ActionView::Helpers::TagHelper
4+
extend TurboPower::StreamHelper
5+
6+
def self.turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes)
7+
template = action.to_sym.in?(%i[ remove refresh ]) ? "" : tag.template(template.to_s.html_safe)
8+
9+
if target = convert_to_turbo_stream_dom_id(target)
10+
tag.turbo_stream(template, **attributes, action: action, target: target)
11+
elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
12+
tag.turbo_stream(template, **attributes, action: action, targets: targets)
13+
else
14+
tag.turbo_stream(template, **attributes, action: action)
15+
end
16+
end
17+
18+
private
19+
20+
def self.convert_to_turbo_stream_dom_id(target, include_selector: false)
21+
if Array(target).any? { |value| value.respond_to?(:to_key) }
22+
"#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(*target)}"
23+
else
24+
target
25+
end
26+
end
27+
end
28+
end

test/dummy/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.log
2+
*.sqlite3
3+
*.sqlite3-shm
4+
*.sqlite3-wal
5+
tmp/*

test/dummy/Rakefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# Add your own tasks in files placed in lib/tasks ending in .rake,
4+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
5+
6+
require_relative "config/application"
7+
8+
Rails.application.load_tasks
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module ApplicationCable
4+
class Channel < ActionCable::Channel::Base
5+
end
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module ApplicationCable
4+
class Connection < ActionCable::Connection::Base
5+
end
6+
end

test/dummy/app/models/message.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class Message < ActiveRecord::Base
4+
end

0 commit comments

Comments
 (0)