Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
58999af9
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 搜索 >>
未验证
提交
58999af9
编写于
10月 10, 2018
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
10月 10, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #34110 from albertoalmagro/enum-raises-on-invalid-definition-values
Enum raises on invalid definition values
上级
9413ef56
df115589
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
33 addition
and
0 deletion
+33
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+11
-0
activerecord/lib/active_record/enum.rb
activerecord/lib/active_record/enum.rb
+11
-0
activerecord/test/cases/enum_test.rb
activerecord/test/cases/enum_test.rb
+11
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
58999af9
*
Enum raises on invalid definition values
When defining a Hash enum it can be easy to use [] instead of {}. This
commit checks that only valid definition values are provided, those can
be a Hash, an array of Symbols or an array of Strings. Otherwise it
raises an ArgumentError.
Fixes #33961
*Alberto Almagro*
*
Reloading associations now clears the Query Cache like
`Persistence#reload`
does.
```
...
...
activerecord/lib/active_record/enum.rb
浏览文件 @
58999af9
...
...
@@ -150,6 +150,7 @@ def enum(definitions)
enum_prefix
=
definitions
.
delete
(
:_prefix
)
enum_suffix
=
definitions
.
delete
(
:_suffix
)
definitions
.
each
do
|
name
,
values
|
assert_valid_enum_definition_values
(
values
)
# statuses = { }
enum_values
=
ActiveSupport
::
HashWithIndifferentAccess
.
new
name
=
name
.
to_s
...
...
@@ -210,10 +211,20 @@ def _enum_methods_module
end
end
def
assert_valid_enum_definition_values
(
values
)
unless
values
.
is_a?
(
Hash
)
||
values
.
all?
{
|
v
|
v
.
is_a?
(
Symbol
)
}
||
values
.
all?
{
|
v
|
v
.
is_a?
(
String
)
}
error_message
=
<<~
MSG
Enum values
#{
values
}
must be either a hash, an array of symbols, or an array of strings.
MSG
raise
ArgumentError
,
error_message
end
end
ENUM_CONFLICT_MESSAGE
=
\
"You tried to define an enum named
\"
%{enum}
\"
on the model
\"
%{klass}
\"
, but "
\
"this will generate a %{type} method
\"
%{method}
\"
, which is already defined "
\
"by %{source}."
private_constant
:ENUM_CONFLICT_MESSAGE
def
detect_enum_conflict!
(
enum_name
,
method_name
,
klass_method
=
false
)
if
klass_method
&&
dangerous_class_method?
(
method_name
)
...
...
activerecord/test/cases/enum_test.rb
浏览文件 @
58999af9
...
...
@@ -265,6 +265,17 @@ class EnumTest < ActiveRecord::TestCase
assert_equal
"published"
,
@book
.
status
end
test
"invalid definition values raise an ArgumentError"
do
e
=
assert_raises
(
ArgumentError
)
do
Class
.
new
(
ActiveRecord
::
Base
)
do
self
.
table_name
=
"books"
enum
status:
[
proposed:
1
,
written:
2
,
published:
3
]
end
end
assert_match
(
/must be either a hash, an array of symbols, or an array of strings./
,
e
.
message
)
end
test
"reserved enum names"
do
klass
=
Class
.
new
(
ActiveRecord
::
Base
)
do
self
.
table_name
=
"books"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录