Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from lsylvester/add-broadcast_to-and-stream_fo…
…r-methods-to-channel add broadcast_to and stream_for methods as per #26
- Loading branch information
Showing
11 changed files
with
139 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module ActionCable | ||
module Channel | ||
module Broadcasting | ||
extend ActiveSupport::Concern | ||
|
||
delegate :broadcasting_for, to: :class | ||
|
||
class_methods do | ||
# Broadcast a hash to a unique broadcasting for this <tt>model</tt> in this channel. | ||
def broadcast_to(model, message) | ||
ActionCable.server.broadcast(broadcasting_for([ channel_name, model ]), message) | ||
end | ||
|
||
def broadcasting_for(model) #:nodoc: | ||
case | ||
when model.is_a?(Array) | ||
model.map { |m| broadcasting_for(m) }.join(':') | ||
when model.respond_to?(:to_gid_param) | ||
model.to_gid_param | ||
else | ||
model.to_param | ||
end | ||
end | ||
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,22 @@ | ||
module ActionCable | ||
module Channel | ||
module Naming | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
# Returns the name of the channel, underscored, without the <tt>Channel</tt> ending. | ||
# If the channel is in a namespace, then the namespaces are represented by single | ||
# colon separators in the channel name. | ||
# | ||
# ChatChannel.channel_name # => 'chat' | ||
# Chats::AppearancesChannel.channel_name # => 'chats:appearances' | ||
def channel_name | ||
@channel_name ||= name.sub(/Channel$/, '').gsub('::',':').underscore | ||
end | ||
end | ||
|
||
# Delegates to the class' <tt>channel_name</tt> | ||
delegate :channel_name, to: :class | ||
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
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,29 @@ | ||
require 'test_helper' | ||
require 'stubs/test_connection' | ||
require 'stubs/room' | ||
|
||
class ActionCable::Channel::BroadcastingTest < ActiveSupport::TestCase | ||
class ChatChannel < ActionCable::Channel::Base | ||
end | ||
|
||
setup do | ||
@connection = TestConnection.new | ||
end | ||
|
||
test "broadcasts_to" do | ||
ActionCable.stubs(:server).returns mock().tap { |m| m.expects(:broadcast).with('action_cable:channel:broadcasting_test:chat:Room#1-Campfire', "Hello World") } | ||
ChatChannel.broadcast_to(Room.new(1), "Hello World") | ||
end | ||
|
||
test "broadcasting_for with an object" do | ||
assert_equal "Room#1-Campfire", ChatChannel.broadcasting_for(Room.new(1)) | ||
end | ||
|
||
test "broadcasting_for with an array" do | ||
assert_equal "Room#1-Campfire:Room#2-Campfire", ChatChannel.broadcasting_for([ Room.new(1), Room.new(2) ]) | ||
end | ||
|
||
test "broadcasting_for with a string" do | ||
assert_equal "hello", ChatChannel.broadcasting_for("hello") | ||
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,10 @@ | ||
require 'test_helper' | ||
|
||
class ActionCable::Channel::NamingTest < ActiveSupport::TestCase | ||
class ChatChannel < ActionCable::Channel::Base | ||
end | ||
|
||
test "channel_name" do | ||
assert_equal "action_cable:channel:naming_test:chat", ChatChannel.channel_name | ||
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
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