Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2a29dde5
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 搜索 >>
未验证
提交
2a29dde5
编写于
8月 24, 2020
作者:
R
Ryuta Kamizono
提交者:
GitHub
8月 24, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39685 from kamipo/store_full_class_name
Support storing demodulized class name for polymorphic type
上级
f22dd39c
62cfbdf3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
100 addition
and
17 deletion
+100
-17
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+9
-0
activerecord/lib/active_record/inheritance.rb
activerecord/lib/active_record/inheritance.rb
+10
-4
activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb
...s/associations/eager_load_includes_full_sti_class_test.rb
+81
-13
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
2a29dde5
*
Support storing demodulized class name for polymorphic type.
Before Rails 6.1, storing demodulized class name is supported only for STI type
by `store_full_sti_class` class attribute.
Now `store_full_class_name` class attribute can handle both STI and polymorphic types.
*Ryuta Kamizono*
*
Deprecate
`rails db:structure:{load, dump}`
tasks and extend
`rails db:schema:{load, dump}`
tasks to work with either
`:ruby`
or
`:sql`
format,
depending on
`config.active_record.schema_format`
configuration value.
...
...
activerecord/lib/active_record/inheritance.rb
浏览文件 @
2a29dde5
...
...
@@ -38,6 +38,8 @@ module Inheritance
extend
ActiveSupport
::
Concern
included
do
class_attribute
:store_full_class_name
,
instance_writer:
false
,
default:
true
# Determines whether to store the full constant name including namespace when using STI.
# This is true, by default.
class_attribute
:store_full_sti_class
,
instance_writer:
false
,
default:
true
...
...
@@ -164,14 +166,14 @@ def abstract_class?
# Returns the value to be stored in the inheritance column for STI.
def
sti_name
store_full_sti_class
?
name
:
name
.
demodulize
store_full_sti_class
&&
store_full_class_name
?
name
:
name
.
demodulize
end
# Returns the class for the provided +type_name+.
#
# It is used to find the class correspondent to the value stored in the inheritance column.
def
sti_class_for
(
type_name
)
if
store_full_sti_class
if
store_full_sti_class
&&
store_full_class_name
ActiveSupport
::
Dependencies
.
constantize
(
type_name
)
else
compute_type
(
type_name
)
...
...
@@ -186,14 +188,18 @@ def sti_class_for(type_name)
# Returns the value to be stored in the polymorphic type column for Polymorphic Associations.
def
polymorphic_name
base_class
.
nam
e
store_full_class_name
?
base_class
.
name
:
base_class
.
name
.
demoduliz
e
end
# Returns the class for the provided +name+.
#
# It is used to find the class correspondent to the value stored in the polymorphic type column.
def
polymorphic_class_for
(
name
)
name
.
constantize
if
store_full_class_name
ActiveSupport
::
Dependencies
.
constantize
(
name
)
else
compute_type
(
name
)
end
end
def
inherited
(
subclass
)
...
...
activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb
浏览文件 @
2a29dde5
...
...
@@ -8,14 +8,10 @@ module Namespaced
class
Post
<
ActiveRecord
::
Base
self
.
table_name
=
"posts"
has_one
:tagging
,
as: :taggable
,
class_name:
"Tagging"
def
self
.
polymorphic_name
sti_name
end
end
end
module
Polymorphic
FullStiClassNamesSharedTest
module
FullStiClassNamesSharedTest
def
setup
@old_store_full_sti_class
=
ActiveRecord
::
Base
.
store_full_sti_class
ActiveRecord
::
Base
.
store_full_sti_class
=
store_full_sti_class
...
...
@@ -31,7 +27,7 @@ def teardown
def
test_class_names
ActiveRecord
::
Base
.
store_full_sti_class
=
!
store_full_sti_class
post
=
Namespaced
::
Post
.
find_by_title
(
"Great stuff"
)
assert_
nil
post
.
tagging
assert_
equal
@tagging
,
post
.
tagging
ActiveRecord
::
Base
.
store_full_sti_class
=
store_full_sti_class
post
=
Namespaced
::
Post
.
find_by_title
(
"Great stuff"
)
...
...
@@ -41,7 +37,7 @@ def test_class_names
def
test_class_names_with_includes
ActiveRecord
::
Base
.
store_full_sti_class
=
!
store_full_sti_class
post
=
Namespaced
::
Post
.
includes
(
:tagging
).
find_by_title
(
"Great stuff"
)
assert_
nil
post
.
tagging
assert_
equal
@tagging
,
post
.
tagging
ActiveRecord
::
Base
.
store_full_sti_class
=
store_full_sti_class
post
=
Namespaced
::
Post
.
includes
(
:tagging
).
find_by_title
(
"Great stuff"
)
...
...
@@ -51,7 +47,7 @@ def test_class_names_with_includes
def
test_class_names_with_eager_load
ActiveRecord
::
Base
.
store_full_sti_class
=
!
store_full_sti_class
post
=
Namespaced
::
Post
.
eager_load
(
:tagging
).
find_by_title
(
"Great stuff"
)
assert_
nil
post
.
tagging
assert_
equal
@tagging
,
post
.
tagging
ActiveRecord
::
Base
.
store_full_sti_class
=
store_full_sti_class
post
=
Namespaced
::
Post
.
eager_load
(
:tagging
).
find_by_title
(
"Great stuff"
)
...
...
@@ -62,15 +58,15 @@ def test_class_names_with_find_by
post
=
Namespaced
::
Post
.
find_by_title
(
"Great stuff"
)
ActiveRecord
::
Base
.
store_full_sti_class
=
!
store_full_sti_class
assert_
nil
Tagging
.
find_by
(
taggable:
post
)
assert_
equal
@tagging
,
Tagging
.
find_by
(
taggable:
post
)
ActiveRecord
::
Base
.
store_full_sti_class
=
store_full_sti_class
assert_equal
@tagging
,
Tagging
.
find_by
(
taggable:
post
)
end
end
class
Polymorphic
FullStiClassNamesTest
<
ActiveRecord
::
TestCase
include
Polymorphic
FullStiClassNamesSharedTest
class
FullStiClassNamesTest
<
ActiveRecord
::
TestCase
include
FullStiClassNamesSharedTest
private
def
store_full_sti_class
...
...
@@ -78,11 +74,83 @@ def store_full_sti_class
end
end
class
Polymorphic
NonFullStiClassNamesTest
<
ActiveRecord
::
TestCase
include
Polymorphic
FullStiClassNamesSharedTest
class
NonFullStiClassNamesTest
<
ActiveRecord
::
TestCase
include
FullStiClassNamesSharedTest
private
def
store_full_sti_class
false
end
end
module
PolymorphicFullClassNamesSharedTest
def
setup
@old_store_full_class_name
=
ActiveRecord
::
Base
.
store_full_class_name
ActiveRecord
::
Base
.
store_full_class_name
=
store_full_class_name
post
=
Namespaced
::
Post
.
create
(
title:
"Great stuff"
,
body:
"This is not"
,
author_id:
1
)
@tagging
=
post
.
create_tagging!
end
def
teardown
ActiveRecord
::
Base
.
store_full_class_name
=
@old_store_full_class_name
end
def
test_class_names
ActiveRecord
::
Base
.
store_full_class_name
=
!
store_full_class_name
post
=
Namespaced
::
Post
.
find_by_title
(
"Great stuff"
)
assert_nil
post
.
tagging
ActiveRecord
::
Base
.
store_full_class_name
=
store_full_class_name
post
=
Namespaced
::
Post
.
find_by_title
(
"Great stuff"
)
assert_equal
@tagging
,
post
.
tagging
end
def
test_class_names_with_includes
ActiveRecord
::
Base
.
store_full_class_name
=
!
store_full_class_name
post
=
Namespaced
::
Post
.
includes
(
:tagging
).
find_by_title
(
"Great stuff"
)
assert_nil
post
.
tagging
ActiveRecord
::
Base
.
store_full_class_name
=
store_full_class_name
post
=
Namespaced
::
Post
.
includes
(
:tagging
).
find_by_title
(
"Great stuff"
)
assert_equal
@tagging
,
post
.
tagging
end
def
test_class_names_with_eager_load
ActiveRecord
::
Base
.
store_full_class_name
=
!
store_full_class_name
post
=
Namespaced
::
Post
.
eager_load
(
:tagging
).
find_by_title
(
"Great stuff"
)
assert_nil
post
.
tagging
ActiveRecord
::
Base
.
store_full_class_name
=
store_full_class_name
post
=
Namespaced
::
Post
.
eager_load
(
:tagging
).
find_by_title
(
"Great stuff"
)
assert_equal
@tagging
,
post
.
tagging
end
def
test_class_names_with_find_by
post
=
Namespaced
::
Post
.
find_by_title
(
"Great stuff"
)
ActiveRecord
::
Base
.
store_full_class_name
=
!
store_full_class_name
assert_nil
Tagging
.
find_by
(
taggable:
post
)
ActiveRecord
::
Base
.
store_full_class_name
=
store_full_class_name
assert_equal
@tagging
,
Tagging
.
find_by
(
taggable:
post
)
end
end
class
PolymorphicFullClassNamesTest
<
ActiveRecord
::
TestCase
include
PolymorphicFullClassNamesSharedTest
private
def
store_full_class_name
true
end
end
class
PolymorphicNonFullClassNamesTest
<
ActiveRecord
::
TestCase
include
PolymorphicFullClassNamesSharedTest
private
def
store_full_class_name
false
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录