Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1e45818a
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,发现更多精彩内容 >>
提交
1e45818a
编写于
12月 28, 2008
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow multiple conditions for callbacks [#1627 state:resolved]
上级
3b92b141
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
43 addition
and
8 deletion
+43
-8
activesupport/lib/active_support/callbacks.rb
activesupport/lib/active_support/callbacks.rb
+2
-7
activesupport/test/callbacks_test.rb
activesupport/test/callbacks_test.rb
+41
-1
未找到文件。
activesupport/lib/active_support/callbacks.rb
浏览文件 @
1e45818a
...
...
@@ -192,13 +192,8 @@ def evaluate_method(method, *args, &block)
end
def
should_run_callback?
(
*
args
)
if
options
[
:if
]
evaluate_method
(
options
[
:if
],
*
args
)
elsif
options
[
:unless
]
!
evaluate_method
(
options
[
:unless
],
*
args
)
else
true
end
[
options
[
:if
]].
flatten
.
compact
.
all?
{
|
a
|
evaluate_method
(
a
,
*
args
)
}
&&
!
[
options
[
:unless
]].
flatten
.
compact
.
any?
{
|
a
|
evaluate_method
(
a
,
*
args
)
}
end
end
...
...
activesupport/test/callbacks_test.rb
浏览文件 @
1e45818a
...
...
@@ -53,10 +53,41 @@ def save
end
class
ConditionalPerson
<
Record
# proc
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:proc
]
},
:if
=>
Proc
.
new
{
|
r
|
true
}
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:if
=>
Proc
.
new
{
|
r
|
false
}
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:proc
]
},
:unless
=>
Proc
.
new
{
|
r
|
false
}
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:unless
=>
Proc
.
new
{
|
r
|
true
}
# symbol
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:symbol
]
},
:if
=>
:yes
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:if
=>
:no
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:symbol
]
},
:unless
=>
:no
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:unless
=>
:yes
# string
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:string
]
},
:if
=>
'yes'
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:if
=>
'no'
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:string
]
},
:unless
=>
'no'
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:unless
=>
'yes'
# Array with conditions
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:symbol_array
]
},
:if
=>
[
:yes
,
:other_yes
]
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:if
=>
[
:yes
,
:no
]
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:symbol_array
]
},
:unless
=>
[
:no
,
:other_no
]
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:unless
=>
[
:yes
,
:no
]
# Combined if and unless
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:combined_symbol
]
},
:if
=>
:yes
,
:unless
=>
:no
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:if
=>
:yes
,
:unless
=>
:yes
# Array with different types of conditions
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:symbol_proc_string_array
]
},
:if
=>
[
:yes
,
Proc
.
new
{
|
r
|
true
},
'yes'
]
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:if
=>
[
:yes
,
Proc
.
new
{
|
r
|
true
},
'no'
]
# Array with different types of conditions comibned if and unless
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
[
:before_save
,
:combined_symbol_proc_string_array
]
},
:if
=>
[
:yes
,
Proc
.
new
{
|
r
|
true
},
'yes'
],
:unless
=>
[
:no
,
'no'
]
before_save
Proc
.
new
{
|
r
|
r
.
history
<<
"b00m"
},
:if
=>
[
:yes
,
Proc
.
new
{
|
r
|
true
},
'no'
],
:unless
=>
[
:no
,
'no'
]
def
yes
;
true
;
end
def
other_yes
;
true
;
end
def
no
;
false
;
end
def
other_no
;
false
;
end
def
save
run_callbacks
(
:before_save
)
...
...
@@ -90,7 +121,16 @@ def test_save_conditional_person
person
.
save
assert_equal
[
[
:before_save
,
:proc
],
[
:before_save
,
:proc
]
[
:before_save
,
:proc
],
[
:before_save
,
:symbol
],
[
:before_save
,
:symbol
],
[
:before_save
,
:string
],
[
:before_save
,
:string
],
[
:before_save
,
:symbol_array
],
[
:before_save
,
:symbol_array
],
[
:before_save
,
:combined_symbol
],
[
:before_save
,
:symbol_proc_string_array
],
[
:before_save
,
:combined_symbol_proc_string_array
]
],
person
.
history
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录