Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
86335671
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,发现更多精彩内容 >>
未验证
提交
86335671
编写于
11月 28, 2017
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #31179 from kinnrot/scoping-reserved-names
Scoping reserved names
上级
591c4a44
6552ce03
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
49 addition
and
0 deletion
+49
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+7
-0
activerecord/lib/active_record/enum.rb
activerecord/lib/active_record/enum.rb
+2
-0
activerecord/lib/active_record/scoping/named.rb
activerecord/lib/active_record/scoping/named.rb
+6
-0
activerecord/test/cases/enum_test.rb
activerecord/test/cases/enum_test.rb
+18
-0
activerecord/test/cases/scoping/named_scoping_test.rb
activerecord/test/cases/scoping/named_scoping_test.rb
+16
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
86335671
*
Don't allow scopes to be defined which conflict with instance methods on
`Relation`
.
Fixes #31120.
*kinnrot*
## Rails 5.2.0.beta2 (November 28, 2017) ##
*
No changes.
...
...
activerecord/lib/active_record/enum.rb
浏览文件 @
86335671
...
...
@@ -221,6 +221,8 @@ def _enum_methods_module
def
detect_enum_conflict!
(
enum_name
,
method_name
,
klass_method
=
false
)
if
klass_method
&&
dangerous_class_method?
(
method_name
)
raise_conflict_error
(
enum_name
,
method_name
,
type:
"class"
)
elsif
klass_method
&&
method_defined_within?
(
method_name
,
Relation
)
raise_conflict_error
(
enum_name
,
method_name
,
type:
"class"
,
source:
Relation
.
name
)
elsif
!
klass_method
&&
dangerous_attribute_method?
(
method_name
)
raise_conflict_error
(
enum_name
,
method_name
)
elsif
!
klass_method
&&
method_defined_within?
(
method_name
,
_enum_methods_module
,
Module
)
...
...
activerecord/lib/active_record/scoping/named.rb
浏览文件 @
86335671
...
...
@@ -171,6 +171,12 @@ def scope(name, body, &block)
"a class method with the same name."
end
if
method_defined_within?
(
name
,
Relation
)
raise
ArgumentError
,
"You tried to define a scope named
\"
#{
name
}
\"
"
\
"on the model
\"
#{
self
.
name
}
\"
, but ActiveRecord::Relation already defined "
\
"an instance method with the same name."
end
valid_scope_name?
(
name
)
extension
=
Module
.
new
(
&
block
)
if
block
...
...
activerecord/test/cases/enum_test.rb
浏览文件 @
86335671
...
...
@@ -308,6 +308,24 @@ class EnumTest < ActiveRecord::TestCase
end
end
test
"reserved enum values for relation"
do
relation_method_samples
=
[
:records
,
:to_ary
,
:scope_for_create
]
relation_method_samples
.
each
do
|
value
|
e
=
assert_raises
(
ArgumentError
,
"enum value `
#{
value
}
` should not be allowed"
)
do
Class
.
new
(
ActiveRecord
::
Base
)
do
self
.
table_name
=
"books"
enum
category:
[
:other
,
value
]
end
end
assert_match
(
/You tried to define an enum named .* on the model/
,
e
.
message
)
end
end
test
"overriding enum method should not raise"
do
assert_nothing_raised
do
Class
.
new
(
ActiveRecord
::
Base
)
do
...
...
activerecord/test/cases/scoping/named_scoping_test.rb
浏览文件 @
86335671
...
...
@@ -151,6 +151,22 @@ def test_scopes_body_is_a_callable
assert_equal
"The scope body needs to be callable."
,
e
.
message
end
def
test_scopes_name_is_relation_method
conflicts
=
[
:records
,
:to_ary
,
:to_sql
,
:explain
]
conflicts
.
each
do
|
name
|
e
=
assert_raises
ArgumentError
do
Class
.
new
(
Post
).
class_eval
{
scope
name
,
->
{
where
(
approved:
true
)
}
}
end
assert_match
(
/You tried to define a scope named \"
#{
name
}
\" on the model/
,
e
.
message
)
end
end
def
test_active_records_have_scope_named__all__
assert
!
Topic
.
all
.
empty?
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录