Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8159bc9c
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 搜索 >>
提交
8159bc9c
编写于
8月 26, 2012
作者:
M
Mikhail Dieterle
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
allow to pass Symbol or Proc into :limit option of #accepts_nested_attributes_for
上级
67153430
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
45 addition
and
11 deletion
+45
-11
activerecord/lib/active_record/nested_attributes.rb
activerecord/lib/active_record/nested_attributes.rb
+13
-3
activerecord/test/cases/nested_attributes_test.rb
activerecord/test/cases/nested_attributes_test.rb
+31
-7
activerecord/test/models/pirate.rb
activerecord/test/models/pirate.rb
+1
-1
未找到文件。
activerecord/lib/active_record/nested_attributes.rb
浏览文件 @
8159bc9c
...
@@ -245,7 +245,8 @@ module ClassMethods
...
@@ -245,7 +245,8 @@ module ClassMethods
# any value for _destroy.
# any value for _destroy.
# [:limit]
# [:limit]
# Allows you to specify the maximum number of the associated records that
# Allows you to specify the maximum number of the associated records that
# can be processed with the nested attributes. If the size of the
# can be processed with the nested attributes. Limit also can be specified as a
# Proc or a Symbol pointing to a method that should return number. If the size of the
# nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords
# nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords
# exception is raised. If omitted, any number associations can be processed.
# exception is raised. If omitted, any number associations can be processed.
# Note that the :limit option is only applicable to one-to-many associations.
# Note that the :limit option is only applicable to one-to-many associations.
...
@@ -388,8 +389,17 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
...
@@ -388,8 +389,17 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
raise
ArgumentError
,
"Hash or Array expected, got
#{
attributes_collection
.
class
.
name
}
(
#{
attributes_collection
.
inspect
}
)"
raise
ArgumentError
,
"Hash or Array expected, got
#{
attributes_collection
.
class
.
name
}
(
#{
attributes_collection
.
inspect
}
)"
end
end
if
options
[
:limit
]
&&
attributes_collection
.
size
>
options
[
:limit
]
limit
=
case
options
[
:limit
]
raise
TooManyRecords
,
"Maximum
#{
options
[
:limit
]
}
records are allowed. Got
#{
attributes_collection
.
size
}
records instead."
when
Symbol
send
(
options
[
:limit
])
when
Proc
options
[
:limit
].
call
else
options
[
:limit
]
end
if
limit
&&
attributes_collection
.
size
>
limit
raise
TooManyRecords
,
"Maximum
#{
limit
}
records are allowed. Got
#{
attributes_collection
.
size
}
records instead."
end
end
if
attributes_collection
.
is_a?
Hash
if
attributes_collection
.
is_a?
Hash
...
...
activerecord/test/cases/nested_attributes_test.rb
浏览文件 @
8159bc9c
...
@@ -846,13 +846,7 @@ def setup
...
@@ -846,13 +846,7 @@ def setup
include
NestedAttributesOnACollectionAssociationTests
include
NestedAttributesOnACollectionAssociationTests
end
end
class
TestNestedAttributesLimit
<
ActiveRecord
::
TestCase
module
NestedAttributesLimitTests
def
setup
Pirate
.
accepts_nested_attributes_for
:parrots
,
:limit
=>
2
@pirate
=
Pirate
.
create!
(
:catchphrase
=>
"Don' botharrr talkin' like one, savvy?"
)
end
def
teardown
def
teardown
Pirate
.
accepts_nested_attributes_for
:parrots
,
:allow_destroy
=>
true
,
:reject_if
=>
proc
{
|
attributes
|
attributes
.
empty?
}
Pirate
.
accepts_nested_attributes_for
:parrots
,
:allow_destroy
=>
true
,
:reject_if
=>
proc
{
|
attributes
|
attributes
.
empty?
}
end
end
...
@@ -876,6 +870,36 @@ def test_limit_with_exceeding_records
...
@@ -876,6 +870,36 @@ def test_limit_with_exceeding_records
end
end
end
end
class
TestNestedAttributesLimitNumeric
<
ActiveRecord
::
TestCase
def
setup
Pirate
.
accepts_nested_attributes_for
:parrots
,
:limit
=>
2
@pirate
=
Pirate
.
create!
(
:catchphrase
=>
"Don' botharrr talkin' like one, savvy?"
)
end
include
NestedAttributesLimitTests
end
class
TestNestedAttributesLimitSymbol
<
ActiveRecord
::
TestCase
def
setup
Pirate
.
accepts_nested_attributes_for
:parrots
,
:limit
=>
:parrots_limit
@pirate
=
Pirate
.
create!
(
:catchphrase
=>
"Don' botharrr talkin' like one, savvy?"
,
:parrots_limit
=>
2
)
end
include
NestedAttributesLimitTests
end
class
TestNestedAttributesLimitProc
<
ActiveRecord
::
TestCase
def
setup
Pirate
.
accepts_nested_attributes_for
:parrots
,
:limit
=>
proc
{
2
}
@pirate
=
Pirate
.
create!
(
:catchphrase
=>
"Don' botharrr talkin' like one, savvy?"
)
end
include
NestedAttributesLimitTests
end
class
TestNestedAttributesWithNonStandardPrimaryKeys
<
ActiveRecord
::
TestCase
class
TestNestedAttributesWithNonStandardPrimaryKeys
<
ActiveRecord
::
TestCase
fixtures
:owners
,
:pets
fixtures
:owners
,
:pets
...
...
activerecord/test/models/pirate.rb
浏览文件 @
8159bc9c
...
@@ -53,7 +53,7 @@ def reject_empty_ships_on_create(attributes)
...
@@ -53,7 +53,7 @@ def reject_empty_ships_on_create(attributes)
attributes
.
delete
(
'_reject_me_if_new'
).
present?
&&
!
persisted?
attributes
.
delete
(
'_reject_me_if_new'
).
present?
&&
!
persisted?
end
end
attr_accessor
:cancel_save_from_callback
attr_accessor
:cancel_save_from_callback
,
:parrots_limit
before_save
:cancel_save_callback_method
,
:if
=>
:cancel_save_from_callback
before_save
:cancel_save_callback_method
,
:if
=>
:cancel_save_from_callback
def
cancel_save_callback_method
def
cancel_save_callback_method
false
false
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录