Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b09bbdb8
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 搜索 >>
提交
b09bbdb8
编写于
6月 12, 2011
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace inline lambdas with named methods
上级
14e6bbb1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
42 addition
and
29 deletion
+42
-29
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+42
-29
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
b09bbdb8
...
...
@@ -114,21 +114,13 @@ def create!(attributes = {}, options = {}, &block)
# Add +records+ to this association. Returns +self+ so method calls may be chained.
# Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically.
def
concat
(
*
records
)
result
=
true
load_target
if
owner
.
new_record?
block
=
lambda
do
records
.
flatten
.
each
do
|
record
|
raise_on_type_mismatch
(
record
)
add_to_target
(
record
)
do
|
r
|
result
&&=
insert_record
(
record
)
unless
owner
.
new_record?
end
end
if
owner
.
new_record?
concat_records
(
records
)
else
transaction
{
concat_records
(
records
)
}
end
owner
.
new_record?
?
block
.
call
:
transaction
(
&
block
)
result
&&
records
end
# Starts a transaction in the association class's database connection.
...
...
@@ -297,17 +289,11 @@ def replace(other_array)
other_array
.
each
{
|
val
|
raise_on_type_mismatch
(
val
)
}
original_target
=
load_target
.
dup
block
=
lambda
do
delete
(
target
-
other_array
)
unless
concat
(
other_array
-
target
)
@target
=
original_target
raise
RecordNotSaved
,
"Failed to replace
#{
reflection
.
name
}
because one or more of the "
\
"new records could not be saved."
end
if
owner
.
new_record?
replace_records
(
other_array
,
original_target
)
else
transaction
{
replace_records
(
other_array
,
original_target
)
}
end
owner
.
new_record?
?
block
.
call
:
transaction
(
&
block
)
end
def
include?
(
record
)
...
...
@@ -448,16 +434,20 @@ def delete_or_destroy(records, method)
records
.
each
{
|
record
|
raise_on_type_mismatch
(
record
)
}
existing_records
=
records
.
reject
{
|
r
|
r
.
new_record?
}
block
=
lambda
do
records
.
each
{
|
record
|
callback
(
:before_remove
,
record
)
}
if
existing_records
.
empty?
remove_records
(
existing_records
,
records
,
method
)
else
transaction
{
remove_records
(
existing_records
,
records
,
method
)
}
end
end
delete_records
(
existing_records
,
method
)
if
existing_records
.
any?
records
.
each
{
|
record
|
target
.
delete
(
record
)
}
def
remove_records
(
existing_records
,
records
,
method
)
records
.
each
{
|
record
|
callback
(
:before_remove
,
record
)
}
records
.
each
{
|
record
|
callback
(
:after_remove
,
record
)
}
end
delete_records
(
existing_records
,
method
)
if
existing_records
.
any?
records
.
each
{
|
record
|
target
.
delete
(
record
)
}
existing_records
.
any?
?
transaction
(
&
block
)
:
block
.
call
records
.
each
{
|
record
|
callback
(
:after_remove
,
record
)
}
end
# Delete the given records from the association, using one of the methods :destroy,
...
...
@@ -466,6 +456,29 @@ def delete_records(records, method)
raise
NotImplementedError
end
def
replace_records
(
new_target
,
original_target
)
delete
(
target
-
new_target
)
unless
concat
(
new_target
-
target
)
@target
=
original_target
raise
RecordNotSaved
,
"Failed to replace
#{
reflection
.
name
}
because one or more of the "
\
"new records could not be saved."
end
end
def
concat_records
(
records
)
result
=
true
records
.
flatten
.
each
do
|
record
|
raise_on_type_mismatch
(
record
)
add_to_target
(
record
)
do
|
r
|
result
&&=
insert_record
(
record
)
unless
owner
.
new_record?
end
end
result
&&
records
end
def
callback
(
method
,
record
)
callbacks_for
(
method
).
each
do
|
callback
|
case
callback
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录