Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ed186ace
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 搜索 >>
提交
ed186ace
编写于
11月 10, 2019
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #37658 from rails/fix-collection-association-callback
Fix collection callbacks not terminating when abort is thrown
上级
97fcbfe6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
35 addition
and
2 deletion
+35
-2
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+6
-2
activerecord/test/cases/associations/callbacks_test.rb
activerecord/test/cases/associations/callbacks_test.rb
+17
-0
activerecord/test/models/author.rb
activerecord/test/models/author.rb
+12
-0
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
ed186ace
...
...
@@ -378,7 +378,9 @@ def delete_or_destroy(records, method)
end
def
remove_records
(
existing_records
,
records
,
method
)
records
.
each
{
|
record
|
callback
(
:before_remove
,
record
)
}
catch
(
:abort
)
do
records
.
each
{
|
record
|
callback
(
:before_remove
,
record
)
}
end
||
return
delete_records
(
existing_records
,
method
)
if
existing_records
.
any?
@target
-=
records
...
...
@@ -434,7 +436,9 @@ def concat_records(records, raise = false)
end
def
replace_on_target
(
record
,
index
,
skip_callbacks
)
callback
(
:before_add
,
record
)
unless
skip_callbacks
catch
(
:abort
)
do
callback
(
:before_add
,
record
)
end
||
return
unless
skip_callbacks
set_inverse_instance
(
record
)
...
...
activerecord/test/cases/associations/callbacks_test.rb
浏览文件 @
ed186ace
...
...
@@ -62,6 +62,23 @@ def test_multiple_callbacks
"after_adding
#{
@thinking
.
id
}
"
,
"after_adding_proc
#{
@thinking
.
id
}
"
],
@david
.
post_log
end
def
test_has_many_callbacks_halt_execution_when_abort_is_trown_when_adding_to_association
author
=
Author
.
create!
(
name:
"Roger"
)
post
=
Post
.
create!
(
title:
"hello"
,
body:
"abc"
)
author
.
posts_with_thrown_callbacks
<<
post
assert_empty
(
author
.
posts_with_callbacks
)
end
def
test_has_many_callbacks_halt_execution_when_abort_is_trown_when_removing_from_association
author
=
Author
.
create!
(
name:
"Roger"
)
post
=
Post
.
create!
(
title:
"hello"
,
body:
"abc"
,
author:
author
)
assert_equal
(
1
,
author
.
posts_with_thrown_callbacks
.
size
)
author
.
posts_with_thrown_callbacks
.
destroy
(
post
.
id
)
assert_equal
(
1
,
author
.
posts_with_thrown_callbacks
.
size
)
end
def
test_has_many_callbacks_with_create
morten
=
Author
.
create
name:
"Morten"
post
=
morten
.
posts_with_proc_callbacks
.
create!
title:
"Hello"
,
body:
"How are you doing?"
...
...
activerecord/test/models/author.rb
浏览文件 @
ed186ace
...
...
@@ -71,6 +71,10 @@ def ratings
after_add: :log_after_adding
,
before_remove: :log_before_removing
,
after_remove: :log_after_removing
has_many
:posts_with_thrown_callbacks
,
class_name:
"Post"
,
before_add: :throw_abort
,
after_add: :ensure_not_called
,
before_remove: :throw_abort
,
after_remove: :ensure_not_called
has_many
:posts_with_proc_callbacks
,
class_name:
"Post"
,
before_add:
Proc
.
new
{
|
o
,
r
|
o
.
post_log
<<
"before_adding
#{
r
.
id
||
'<new>'
}
"
},
after_add:
Proc
.
new
{
|
o
,
r
|
o
.
post_log
<<
"after_adding
#{
r
.
id
||
'<new>'
}
"
},
...
...
@@ -185,6 +189,14 @@ def social
validates_presence_of
:name
private
def
throw_abort
(
_
)
throw
(
:abort
)
end
def
ensure_not_called
(
_
)
raise
end
def
log_before_adding
(
object
)
@post_log
<<
"before_adding
#{
object
.
id
||
'<new>'
}
"
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录