Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c40c0fe9
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,发现更多精彩内容 >>
提交
c40c0fe9
编写于
9月 19, 2016
作者:
R
Rafael França
提交者:
GitHub
9月 19, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #26552 from rafaelfranca/fix-errors-details-storage
Always store errors details information with symbols
上级
c6612172
d406014b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
32 addition
and
15 deletion
+32
-15
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+15
-0
activerecord/lib/active_record/autosave_association.rb
activerecord/lib/active_record/autosave_association.rb
+12
-10
activerecord/test/cases/autosave_association_test.rb
activerecord/test/cases/autosave_association_test.rb
+5
-5
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
c40c0fe9
*
Always store errors details information with symbols.
When the association is autosaved we were storing the details with
string keys. This was creating inconsistency with other details that are
added using the `Errors#add` method. It was also inconsistent with the
`Errors#messages` storage.
To fix this inconsistency we are always storing with symbols. This will
cause a small breaking change because in those cases the details could
be accessed as strings keys but now it can not.
Fix #26499.
*Rafael Mendonça França*, *Marcus Vieira*
*
Calling
`touch`
on a model using optimistic locking will now leave the model
in a non-dirty state with no attribute changes.
...
...
activerecord/lib/active_record/autosave_association.rb
浏览文件 @
c40c0fe9
...
...
@@ -329,26 +329,20 @@ def association_valid?(reflection, record, index=nil)
return
true
if
record
.
destroyed?
||
(
reflection
.
options
[
:autosave
]
&&
record
.
marked_for_destruction?
)
validation_context
=
self
.
validation_context
unless
[
:create
,
:update
].
include?
(
self
.
validation_context
)
unless
valid
=
record
.
valid?
(
validation_context
)
if
reflection
.
options
[
:autosave
]
indexed_attribute
=
!
index
.
nil?
&&
(
reflection
.
options
[
:index_errors
]
||
ActiveRecord
::
Base
.
index_nested_attribute_errors
)
record
.
errors
.
each
do
|
attribute
,
message
|
if
indexed_attribute
attribute
=
"
#{
reflection
.
name
}
[
#{
index
}
].
#{
attribute
}
"
else
attribute
=
"
#{
reflection
.
name
}
.
#{
attribute
}
"
end
attribute
=
normalize_reflection_attribute
(
indexed_attribute
,
reflection
,
index
,
attribute
)
errors
[
attribute
]
<<
message
errors
[
attribute
].
uniq!
end
record
.
errors
.
details
.
each_key
do
|
attribute
|
if
indexed_attribute
reflection_attribute
=
"
#{
reflection
.
name
}
[
#{
index
}
].
#{
attribute
}
"
else
reflection_attribute
=
"
#{
reflection
.
name
}
.
#{
attribute
}
"
end
reflection_attribute
=
normalize_reflection_attribute
(
indexed_attribute
,
reflection
,
index
,
attribute
).
to_sym
record
.
errors
.
details
[
attribute
].
each
do
|
error
|
errors
.
details
[
reflection_attribute
]
<<
error
...
...
@@ -362,6 +356,14 @@ def association_valid?(reflection, record, index=nil)
valid
end
def
normalize_reflection_attribute
(
indexed_attribute
,
reflection
,
index
,
attribute
)
if
indexed_attribute
"
#{
reflection
.
name
}
[
#{
index
}
].
#{
attribute
}
"
else
"
#{
reflection
.
name
}
.
#{
attribute
}
"
end
end
# Is used as a before_save callback to check while saving a collection
# association whether or not the parent was a new record before saving.
def
before_save_collection_association
...
...
activerecord/test/cases/autosave_association_test.rb
浏览文件 @
c40c0fe9
...
...
@@ -443,7 +443,7 @@ def test_errors_details_should_be_set
assert_not
invalid_electron
.
valid?
assert
valid_electron
.
valid?
assert_not
molecule
.
valid?
assert_equal
[{
error: :blank
}],
molecule
.
errors
.
details
[
"electrons.name"
]
assert_equal
[{
error: :blank
}],
molecule
.
errors
.
details
[
:
"electrons.name"
]
end
def
test_errors_details_should_be_indexed_when_passed_as_array
...
...
@@ -457,8 +457,8 @@ def test_errors_details_should_be_indexed_when_passed_as_array
assert_not
tuning_peg_invalid
.
valid?
assert
tuning_peg_valid
.
valid?
assert_not
guitar
.
valid?
assert_equal
[{
error: :not_a_number
,
value:
nil
}]
,
guitar
.
errors
.
details
[
"tuning_pegs[1].pitch"
]
assert_equal
[],
guitar
.
errors
.
details
[
"tuning_pegs.pitch"
]
assert_equal
[{
error: :not_a_number
,
value:
nil
}]
,
guitar
.
errors
.
details
[
:
"tuning_pegs[1].pitch"
]
assert_equal
[],
guitar
.
errors
.
details
[
:
"tuning_pegs.pitch"
]
end
def
test_errors_details_should_be_indexed_when_global_flag_is_set
...
...
@@ -474,8 +474,8 @@ def test_errors_details_should_be_indexed_when_global_flag_is_set
assert_not
invalid_electron
.
valid?
assert
valid_electron
.
valid?
assert_not
molecule
.
valid?
assert_equal
[{
error: :blank
}],
molecule
.
errors
.
details
[
"electrons[1].name"
]
assert_equal
[],
molecule
.
errors
.
details
[
"electrons.name"
]
assert_equal
[{
error: :blank
}],
molecule
.
errors
.
details
[
:
"electrons[1].name"
]
assert_equal
[],
molecule
.
errors
.
details
[
:
"electrons.name"
]
ensure
ActiveRecord
::
Base
.
index_nested_attribute_errors
=
old_attribute_config
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录