Skip to content

Commit

Permalink
Introduce TurboPower::Broadcastable module
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Mar 10, 2023
1 parent 4991a27 commit 72c0b47
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/turbo_power.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative "turbo_power/attribute_transformations"
require_relative "turbo_power/stream_helper"
require_relative "turbo_power/broadcasts"
require_relative "turbo_power/broadcastable"

module TurboPower
end
30 changes: 30 additions & 0 deletions lib/turbo_power/broadcastable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module TurboPower
module Broadcastable
ACTIONS = TurboPower::StreamHelper.instance_methods.sort

ACTIONS.each do |action|
action_name = "broadcast_#{action}"
action_name_to = "broadcast_#{action}_to"
action_name_later = "broadcast_#{action}_later"
action_name_later_to = "broadcast_#{action}_later_to"

define_method(action_name) do |**attributes|
Turbo::StreamsChannel.send(action_name_to, self, action: action, **attributes)
end

define_method(action_name_to) do |*streamables, **attributes|
Turbo::StreamsChannel.send(action_name_to, *streamables, action: action, **attributes)
end

define_method(action_name_later) do |**attributes|
Turbo::StreamsChannel.send(action_name_later_to, self, action: action, **attributes)
end

define_method(action_name_later_to) do |*streamables, **attributes|
Turbo::StreamsChannel.send(action_name_later_to, *streamables, action: action, **attributes)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/turbo_power/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ class Engine < Rails::Engine
initializer "turbo_power.broadcasts", after: :initialize do
Turbo::Streams::Broadcasts.include(TurboPower::Broadcasts)
end

initializer "turbo_power.broadcastable", after: :initialize do
Turbo::Broadcastable.include(TurboPower::Broadcastable)
end
end
end

0 comments on commit 72c0b47

Please sign in to comment.