Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1afa9fa5
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,发现更多精彩内容 >>
提交
1afa9fa5
编写于
12月 30, 2009
作者:
E
Eloy Duran
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactored nested attributes a bit around :reject_if => :all_blank.
上级
a323b83a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
8 addition
and
6 deletion
+8
-6
activerecord/lib/active_record/nested_attributes.rb
activerecord/lib/active_record/nested_attributes.rb
+4
-5
activerecord/test/cases/nested_attributes_test.rb
activerecord/test/cases/nested_attributes_test.rb
+4
-1
未找到文件。
activerecord/lib/active_record/nested_attributes.rb
浏览文件 @
1afa9fa5
...
@@ -188,6 +188,8 @@ class TooManyRecords < ActiveRecordError
...
@@ -188,6 +188,8 @@ class TooManyRecords < ActiveRecordError
# the parent model is saved. This happens inside the transaction initiated
# the parent model is saved. This happens inside the transaction initiated
# by the parents save method. See ActiveRecord::AutosaveAssociation.
# by the parents save method. See ActiveRecord::AutosaveAssociation.
module
ClassMethods
module
ClassMethods
REJECT_ALL_BLANK_PROC
=
proc
{
|
attributes
|
attributes
.
all?
{
|
_
,
value
|
value
.
blank?
}
}
# Defines an attributes writer for the specified association(s). If you
# Defines an attributes writer for the specified association(s). If you
# are using <tt>attr_protected</tt> or <tt>attr_accessible</tt>, then you
# are using <tt>attr_protected</tt> or <tt>attr_accessible</tt>, then you
# will need to add the attribute writer to the allowed list.
# will need to add the attribute writer to the allowed list.
...
@@ -229,6 +231,7 @@ def accepts_nested_attributes_for(*attr_names)
...
@@ -229,6 +231,7 @@ def accepts_nested_attributes_for(*attr_names)
options
=
{
:allow_destroy
=>
false
,
:update_only
=>
false
}
options
=
{
:allow_destroy
=>
false
,
:update_only
=>
false
}
options
.
update
(
attr_names
.
extract_options!
)
options
.
update
(
attr_names
.
extract_options!
)
options
.
assert_valid_keys
(
:allow_destroy
,
:reject_if
,
:limit
,
:update_only
)
options
.
assert_valid_keys
(
:allow_destroy
,
:reject_if
,
:limit
,
:update_only
)
options
[
:reject_if
]
=
REJECT_ALL_BLANK_PROC
if
options
[
:reject_if
]
==
:all_blank
attr_names
.
each
do
|
association_name
|
attr_names
.
each
do
|
association_name
|
if
reflection
=
reflect_on_association
(
association_name
)
if
reflection
=
reflect_on_association
(
association_name
)
...
@@ -241,11 +244,7 @@ def accepts_nested_attributes_for(*attr_names)
...
@@ -241,11 +244,7 @@ def accepts_nested_attributes_for(*attr_names)
reflection
.
options
[
:autosave
]
=
true
reflection
.
options
[
:autosave
]
=
true
add_autosave_association_callbacks
(
reflection
)
add_autosave_association_callbacks
(
reflection
)
self
.
nested_attributes_options
[
association_name
.
to_sym
]
=
options
nested_attributes_options
[
association_name
.
to_sym
]
=
options
if
options
[
:reject_if
]
==
:all_blank
self
.
nested_attributes_options
[
association_name
.
to_sym
][
:reject_if
]
=
proc
{
|
attributes
|
attributes
.
all?
{
|
k
,
v
|
v
.
blank?
}
}
end
# def pirate_attributes=(attributes)
# def pirate_attributes=(attributes)
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
...
...
activerecord/test/cases/nested_attributes_test.rb
浏览文件 @
1afa9fa5
...
@@ -34,7 +34,10 @@ def test_base_should_have_an_empty_nested_attributes_options
...
@@ -34,7 +34,10 @@ def test_base_should_have_an_empty_nested_attributes_options
end
end
def
test_should_add_a_proc_to_nested_attributes_options
def
test_should_add_a_proc_to_nested_attributes_options
[
:parrots
,
:birds
,
:birds_with_reject_all_blank
].
each
do
|
name
|
assert_equal
ActiveRecord
::
NestedAttributes
::
ClassMethods
::
REJECT_ALL_BLANK_PROC
,
Pirate
.
nested_attributes_options
[
:birds_with_reject_all_blank
][
:reject_if
]
[
:parrots
,
:birds
].
each
do
|
name
|
assert_instance_of
Proc
,
Pirate
.
nested_attributes_options
[
name
][
:reject_if
]
assert_instance_of
Proc
,
Pirate
.
nested_attributes_options
[
name
][
:reject_if
]
end
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录