Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5a3dc809
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,发现更多精彩内容 >>
提交
5a3dc809
编写于
12月 25, 2012
作者:
P
Pascal Friederich
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
validate :on option on after_commit and after_rollback callbacks
上级
227d4e9b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
29 addition
and
6 deletion
+29
-6
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+5
-0
activerecord/lib/active_record/transactions.rb
activerecord/lib/active_record/transactions.rb
+16
-6
activerecord/test/cases/transaction_callbacks_test.rb
activerecord/test/cases/transaction_callbacks_test.rb
+8
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
5a3dc809
...
...
@@ -22,6 +22,11 @@
*Rafael Mendonça França*
*
after_commit and after_rollback now validate the :on option and raise an ArgumentError if
it is not one of :create, :destroy or :update
*Pascal Friederich*
*
Keep index names when using
`alter_table`
with sqlite3.
Fix #3489.
...
...
activerecord/lib/active_record/transactions.rb
浏览文件 @
5a3dc809
...
...
@@ -4,6 +4,7 @@ module ActiveRecord
# See ActiveRecord::Transactions::ClassMethods for documentation.
module
Transactions
extend
ActiveSupport
::
Concern
ACTIONS
=
[
:create
,
:destroy
,
:update
]
class
TransactionError
<
ActiveRecordError
# :nodoc:
end
...
...
@@ -224,11 +225,7 @@ def transaction(options = {}, &block)
# Note that transactional fixtures do not play well with this feature. Please
# use the +test_after_commit+ gem to have these hooks fired in tests.
def
after_commit
(
*
args
,
&
block
)
options
=
args
.
last
if
options
.
is_a?
(
Hash
)
&&
options
[
:on
]
options
[
:if
]
=
Array
(
options
[
:if
])
options
[
:if
]
<<
"transaction_include_action?(:
#{
options
[
:on
]
}
)"
end
set_options_for_callbacks!
(
args
)
set_callback
(
:commit
,
:after
,
*
args
,
&
block
)
end
...
...
@@ -236,12 +233,25 @@ def after_commit(*args, &block)
#
# Please check the documentation of +after_commit+ for options.
def
after_rollback
(
*
args
,
&
block
)
set_options_for_callbacks!
(
args
)
set_callback
(
:rollback
,
:after
,
*
args
,
&
block
)
end
private
def
set_options_for_callbacks!
(
args
)
options
=
args
.
last
if
options
.
is_a?
(
Hash
)
&&
options
[
:on
]
assert_valid_transaction_action
(
options
[
:on
])
options
[
:if
]
=
Array
(
options
[
:if
])
options
[
:if
]
<<
"transaction_include_action?(:
#{
options
[
:on
]
}
)"
end
set_callback
(
:rollback
,
:after
,
*
args
,
&
block
)
end
def
assert_valid_transaction_action
(
action
)
unless
ACTIONS
.
include?
(
action
.
to_sym
)
raise
ArgumentError
,
":on conditions for after_commit and after_rollback callbacks have to be one of
#{
ACTIONS
.
join
(
","
)
}
"
end
end
end
...
...
activerecord/test/cases/transaction_callbacks_test.rb
浏览文件 @
5a3dc809
...
...
@@ -244,6 +244,14 @@ def @first.last_after_transaction_error; @last_transaction_error; end
assert_equal
:rollback
,
@first
.
last_after_transaction_error
assert_equal
[
:after_rollback
],
@second
.
history
end
def
test_after_rollback_callbacks_should_validate_on_condition
assert_raise
(
ArgumentError
)
{
Topic
.
send
(
:after_rollback
,
:on
=>
:save
)
}
end
def
test_after_commit_callbacks_should_validate_on_condition
assert_raise
(
ArgumentError
)
{
Topic
.
send
(
:after_commit
,
:on
=>
:save
)
}
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录