Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a98736f8
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 搜索 >>
提交
a98736f8
编写于
10月 04, 2019
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #37295 from tekin/fix-belongs-to-validation-context-issue
Fix autosave validation context regression
上级
0355717e
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
35 addition
and
8 deletion
+35
-8
activerecord/lib/active_record/autosave_association.rb
activerecord/lib/active_record/autosave_association.rb
+7
-3
activerecord/test/cases/autosave_association_test.rb
activerecord/test/cases/autosave_association_test.rb
+21
-3
activerecord/test/models/account.rb
activerecord/test/models/account.rb
+5
-0
activerecord/test/models/pirate.rb
activerecord/test/models/pirate.rb
+1
-1
activerecord/test/models/ship.rb
activerecord/test/models/ship.rb
+1
-1
未找到文件。
activerecord/lib/active_record/autosave_association.rb
浏览文件 @
a98736f8
...
...
@@ -272,7 +272,7 @@ def changed_for_autosave?
# or saved. If +autosave+ is +false+ only new records will be returned,
# unless the parent is/was a new record itself.
def
associated_records_to_validate_or_save
(
association
,
new_record
,
autosave
)
if
new_record
if
new_record
||
custom_validation_context?
association
&&
association
.
target
elsif
autosave
association
.
target
.
find_all
(
&
:changed_for_autosave?
)
...
...
@@ -304,7 +304,7 @@ def nested_records_changed_for_autosave?
def
validate_single_association
(
reflection
)
association
=
association_instance_get
(
reflection
.
name
)
record
=
association
&&
association
.
reader
association_valid?
(
reflection
,
record
)
if
record
&&
record
.
changed_for_autosave?
association_valid?
(
reflection
,
record
)
if
record
&&
(
record
.
changed_for_autosave?
||
custom_validation_context?
)
end
# Validate the associated records if <tt>:validate</tt> or
...
...
@@ -324,7 +324,7 @@ def validate_collection_association(reflection)
def
association_valid?
(
reflection
,
record
,
index
=
nil
)
return
true
if
record
.
destroyed?
||
(
reflection
.
options
[
:autosave
]
&&
record
.
marked_for_destruction?
)
context
=
validation_context
unless
[
:create
,
:update
].
include?
(
validation_context
)
context
=
validation_context
if
custom_validation_context?
unless
valid
=
record
.
valid?
(
context
)
if
reflection
.
options
[
:autosave
]
...
...
@@ -499,6 +499,10 @@ def save_belongs_to_association(reflection)
end
end
def
custom_validation_context?
validation_context
&&
[
:create
,
:update
].
exclude?
(
validation_context
)
end
def
_ensure_no_duplicate_errors
errors
.
messages
.
each_key
do
|
attribute
|
errors
[
attribute
].
uniq!
...
...
activerecord/test/cases/autosave_association_test.rb
浏览文件 @
a98736f8
...
...
@@ -34,7 +34,7 @@
require
"models/reply"
class
TestAutosaveAssociationsInGeneral
<
ActiveRecord
::
TestCase
def
test_autosave_
validation
def
test_autosave_
does_not_pass_through_non_custom_validation_contexts
person
=
Class
.
new
(
ActiveRecord
::
Base
)
{
self
.
table_name
=
"people"
validate
:should_be_cool
,
on: :create
...
...
@@ -55,8 +55,11 @@ def self.name; "Reference"; end
}
u
=
person
.
create!
(
first_name:
"cool"
)
u
.
update_attributes!
(
first_name:
"nah"
)
# still valid because validation only applies on 'create'
assert_predicate
reference
.
create!
(
person:
u
),
:persisted?
u
.
first_name
=
"nah"
assert_predicate
u
,
:valid?
r
=
reference
.
new
(
person:
u
)
assert_predicate
r
,
:valid?
end
def
test_should_not_add_the_same_callbacks_multiple_times_for_has_one
...
...
@@ -1683,6 +1686,14 @@ def setup
assert_not_predicate
@pirate
,
:valid?
end
def
test_validations_still_fire_on_unchanged_association_with_custom_validation_context
pirate
=
FamousPirate
.
create!
(
catchphrase:
"Avast Ye!"
)
pirate
.
famous_ships
.
create!
assert
pirate
.
valid?
assert_not
pirate
.
valid?
(
:conference
)
end
end
class
TestAutosaveAssociationValidationsOnAHasOneAssociation
<
ActiveRecord
::
TestCase
...
...
@@ -1727,6 +1738,13 @@ def setup
@pirate
.
non_validated_parrot
=
Parrot
.
new
(
name:
""
)
assert_predicate
@pirate
,
:valid?
end
def
test_validations_still_fire_on_unchanged_association_with_custom_validation_context
firm_with_low_credit
=
Firm
.
create!
(
name:
"Something"
,
account:
Account
.
new
(
credit_limit:
50
))
assert
firm_with_low_credit
.
valid?
assert_not
firm_with_low_credit
.
valid?
(
:bank_loan
)
end
end
class
TestAutosaveAssociationValidationsOnAHABTMAssociation
<
ActiveRecord
::
TestCase
...
...
activerecord/test/models/account.rb
浏览文件 @
a98736f8
...
...
@@ -21,12 +21,17 @@ def self.destroyed_account_ids
end
validate
:check_empty_credit_limit
validate
:ensure_good_credit
,
on: :bank_loan
private
def
check_empty_credit_limit
errors
.
add
(
"credit_limit"
,
:blank
)
if
credit_limit
.
blank?
end
def
ensure_good_credit
errors
.
add
(
:credit_limit
,
"too low"
)
unless
credit_limit
>
10_000
end
def
private_method
"Sir, yes sir!"
end
...
...
activerecord/test/models/pirate.rb
浏览文件 @
a98736f8
...
...
@@ -89,6 +89,6 @@ class DestructivePirate < Pirate
class
FamousPirate
<
ActiveRecord
::
Base
self
.
table_name
=
"pirates"
has_many
:famous_ships
has_many
:famous_ships
,
inverse_of: :famous_pirate
,
foreign_key: :pirate_id
validates_presence_of
:catchphrase
,
on: :conference
end
activerecord/test/models/ship.rb
浏览文件 @
a98736f8
...
...
@@ -36,6 +36,6 @@ class Prisoner < ActiveRecord::Base
class
FamousShip
<
ActiveRecord
::
Base
self
.
table_name
=
"ships"
belongs_to
:famous_pirate
belongs_to
:famous_pirate
,
foreign_key: :pirate_id
validates_presence_of
:name
,
on: :conference
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录