-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#43) * Introduce `TurboPower::RenderHelper` to reuse `StreamHelpers` in model broadcasts * Update dummy app
- Loading branch information
Showing
18 changed files
with
168 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module TurboPower | ||
class RenderHelper | ||
extend ActionView::Helpers::TagHelper | ||
extend TurboPower::StreamHelper | ||
|
||
def self.turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes) | ||
template = action.to_sym.in?(%i[ remove refresh ]) ? "" : tag.template(template.to_s.html_safe) | ||
|
||
if target = convert_to_turbo_stream_dom_id(target) | ||
tag.turbo_stream(template, **attributes, action: action, target: target) | ||
elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true) | ||
tag.turbo_stream(template, **attributes, action: action, targets: targets) | ||
else | ||
tag.turbo_stream(template, **attributes, action: action) | ||
end | ||
end | ||
|
||
private | ||
|
||
def self.convert_to_turbo_stream_dom_id(target, include_selector: false) | ||
if Array(target).any? { |value| value.respond_to?(:to_key) } | ||
"#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(*target)}" | ||
else | ||
target | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.log | ||
*.sqlite3 | ||
*.sqlite3-shm | ||
*.sqlite3-wal | ||
tmp/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
# Add your own tasks in files placed in lib/tasks ending in .rake, | ||
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. | ||
|
||
require_relative "config/application" | ||
|
||
Rails.application.load_tasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module ApplicationCable | ||
class Channel < ActionCable::Channel::Base | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module ApplicationCable | ||
class Connection < ActionCable::Connection::Base | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class Message < ActiveRecord::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
development: | ||
adapter: async | ||
|
||
test: | ||
adapter: inline | ||
|
||
production: | ||
adapter: redis | ||
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> | ||
channel_prefix: dummy_production |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
default: &default | ||
adapter: sqlite3 | ||
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> | ||
timeout: 5000 | ||
|
||
test: | ||
adapter: sqlite3 | ||
database: db/combustion_test.sqlite | ||
<<: *default | ||
database: db/test.sqlite3 | ||
|
||
development: | ||
<<: *default | ||
database: db/development.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateMessages < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :messages do |t| | ||
t.text :content | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
# frozen_string_literal: true | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# This file is the source Rails uses to define your schema when running `bin/rails | ||
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to | ||
# be faster and is potentially less error prone than running all of your | ||
# migrations from scratch. Old migrations may fail to apply correctly if those | ||
# migrations use external dependencies or application code. | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema[7.1].define(version: 2024_06_06_204215) do | ||
create_table "messages", force: :cascade do |t| | ||
t.text "content" | ||
t.datetime "created_at", null: false | ||
t.datetime "updated_at", null: false | ||
end | ||
|
||
ActiveRecord::Schema.define do | ||
# Set up any tables you need to exist for your test suite that don't belong | ||
# in migrations. | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module TurboPower | ||
class BroadcastableTest < ActionCable::Channel::TestCase | ||
include ::Turbo::Broadcastable::TestHelper | ||
|
||
test "broadcast_dispatch_event" do | ||
message = Message.new(id: 1) | ||
|
||
detail = { id: message.id, action: "save" } | ||
message.broadcast_dispatch_event(target: dom_id(message), name: "saved", detail: detail) | ||
|
||
streams = capture_turbo_stream_broadcasts(message) | ||
|
||
assert_equal 1, streams.length | ||
assert_equal "message_1", streams.first["target"] | ||
assert_equal "dispatch_event", streams.first["action"] | ||
|
||
template_content = detail.to_json | ||
assert_equal template_content, streams.first.at_css("template").content | ||
end | ||
|
||
test "broadcast_dispatch_event_to" do | ||
message = Message.new(id: 1) | ||
|
||
detail = { id: message.id, action: "save" } | ||
message.broadcast_dispatch_event_to("messages", target: dom_id(message), name: "saved", detail: detail) | ||
|
||
streams = capture_turbo_stream_broadcasts("messages") | ||
|
||
assert_equal 1, streams.length | ||
assert_equal "message_1", streams.first["target"] | ||
assert_equal "dispatch_event", streams.first["action"] | ||
|
||
template_content = detail.to_json | ||
assert_equal template_content, streams.first.at_css("template").content | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module TurboStreamHelpers | ||
class ViewContext | ||
private | ||
|
||
def view_methods | ||
ActionView::Base.instance_methods - Object.instance_methods | ||
end | ||
|
||
def method_missing(name, *params, &block) | ||
view_methods.include?(name) ? nil : super | ||
end | ||
|
||
def respond_to_missing?(name, *_params, &_block) | ||
view_methods.include?(name) | ||
end | ||
end | ||
|
||
def view_context | ||
ViewContext.new | ||
end | ||
|
||
def turbo_stream | ||
Turbo::Streams::TagBuilder.new(view_context) | ||
TurboPower::RenderHelper | ||
end | ||
end |