Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3fb1a84f
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3fb1a84f
编写于
7月 23, 2015
作者:
K
Kasper Timm Hansen
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #36 from cristianbica/channel-action-dispatched
Improve channel actions dispatcher to allow inheritance/mixins
上级
bdbc91ab
a97a1fc7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
75 addition
and
4 deletion
+75
-4
lib/action_cable/channel/base.rb
lib/action_cable/channel/base.rb
+37
-2
test/channel/base_test.rb
test/channel/base_test.rb
+38
-2
未找到文件。
lib/action_cable/channel/base.rb
浏览文件 @
3fb1a84f
...
...
@@ -75,6 +75,42 @@ class Base
attr_reader
:params
,
:connection
delegate
:logger
,
to: :connection
class
<<
self
# A list of method names that should be considered actions. This
# includes all public instance methods on a channel, less
# any internal methods (defined on Base), adding back in
# any methods that are internal, but still exist on the class
# itself.
#
# ==== Returns
# * <tt>Set</tt> - A set of all methods that should be considered actions.
def
action_methods
@action_methods
||=
begin
# All public instance methods of this class, including ancestors
methods
=
(
public_instance_methods
(
true
)
-
# Except for public instance methods of Base and its ancestors
ActionCable
::
Channel
::
Base
.
public_instance_methods
(
true
)
+
# Be sure to include shadowed public instance methods of this class
public_instance_methods
(
false
)).
uniq
.
map
(
&
:to_s
)
methods
.
to_set
end
end
protected
# action_methods are cached and there is sometimes need to refresh
# them. ::clear_action_methods! allows you to do that, so next time
# you run action_methods, they will be recalculated
def
clear_action_methods!
@action_methods
=
nil
end
# Refresh the cached action_methods when a new action_method is added.
def
method_added
(
name
)
super
clear_action_methods!
end
end
def
initialize
(
connection
,
identifier
,
params
=
{})
@connection
=
connection
@identifier
=
identifier
...
...
@@ -147,7 +183,7 @@ def extract_action(data)
end
def
processable_action?
(
action
)
self
.
class
.
instance_methods
(
false
).
include?
(
action
)
self
.
class
.
action_methods
.
include?
(
action
.
to_s
)
end
def
dispatch_action
(
action
,
data
)
...
...
@@ -168,7 +204,6 @@ def action_signature(action, data)
end
end
def
run_subscribe_callbacks
self
.
class
.
on_subscribe_callbacks
.
each
{
|
callback
|
send
(
callback
)
}
end
...
...
test/channel/base_test.rb
浏览文件 @
3fb1a84f
...
...
@@ -3,7 +3,22 @@
require
'stubs/room'
class
ActionCable::Channel::BaseTest
<
ActiveSupport
::
TestCase
class
ChatChannel
<
ActionCable
::
Channel
::
Base
class
ActionCable::Channel::Base
def
kick
@last_action
=
[
:kick
]
end
def
topic
end
end
class
BasicChannel
<
ActionCable
::
Channel
::
Base
def
chatters
@last_action
=
[
:chatters
]
end
end
class
ChatChannel
<
BasicChannel
attr_reader
:room
,
:last_action
on_subscribe
:toggle_subscribed
on_unsubscribe
:toggle_subscribed
...
...
@@ -29,6 +44,10 @@ def speak(data)
@last_action
=
[
:speak
,
data
]
end
def
topic
(
data
)
@last_action
=
[
:topic
,
data
]
end
def
subscribed?
@subscribed
end
...
...
@@ -87,11 +106,28 @@ def rm_rf
assert_equal
[
:speak
,
data
],
@channel
.
last_action
end
test
"
try calling
a private method"
do
test
"
should not dispatch
a private method"
do
@channel
.
perform_action
'action'
=>
:rm_rf
assert_nil
@channel
.
last_action
end
test
"should not dispatch a public method defined on Base"
do
@channel
.
perform_action
'action'
=>
:kick
assert_nil
@channel
.
last_action
end
test
"should dispatch a public method defined on Base and redefined on channel"
do
data
=
{
'action'
=>
:topic
,
'content'
=>
"This is Sparta!"
}
@channel
.
perform_action
data
assert_equal
[
:topic
,
data
],
@channel
.
last_action
end
test
"should dispatch calling a public method defined in an ancestor"
do
@channel
.
perform_action
'action'
=>
:chatters
assert_equal
[
:chatters
],
@channel
.
last_action
end
test
"transmitting data"
do
@channel
.
perform_action
'action'
=>
:get_latest
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录