Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6b18bdd0
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,发现更多精彩内容 >>
提交
6b18bdd0
编写于
12月 02, 2015
作者:
K
Kuldeep Aggarwal
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
STI cast new instances to `default type` on initialize.
fixes #17121
上级
203db6b8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
42 addition
and
0 deletion
+42
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+10
-0
activerecord/lib/active_record/inheritance.rb
activerecord/lib/active_record/inheritance.rb
+12
-0
activerecord/test/cases/inheritance_test.rb
activerecord/test/cases/inheritance_test.rb
+20
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
6b18bdd0
*
Use the default inheritance
`:type`
when instantiating a new object.
Example:
# In the schema, BaseModel specifies 'SubType' as the default `:type` value
subtype = BaseModel.new
assert_equals SubType, subtype.class
*Kuldeep Aggarwal*
*
Fix
`rake db:structure:dump`
on Postgres when multiple schemas are used.
Fixes #22346.
...
...
activerecord/lib/active_record/inheritance.rb
浏览文件 @
6b18bdd0
...
...
@@ -55,6 +55,8 @@ def new(*args, &block)
subclass
=
subclass_from_attributes
(
attrs
)
end
subclass
||=
subclass_from_defaults
if
subclass
&&
subclass
!=
self
subclass
.
new
(
*
args
,
&
block
)
else
...
...
@@ -201,6 +203,16 @@ def subclass_from_attributes?(attrs)
attribute_names
.
include?
(
inheritance_column
)
&&
(
attrs
.
is_a?
(
Hash
)
||
attrs
.
respond_to?
(
:permitted?
))
end
def
subclass_from_defaults?
attribute_names
.
include?
(
inheritance_column
)
&&
columns_hash
[
inheritance_column
].
try
(
:default
)
end
def
subclass_from_defaults
if
subclass_from_defaults?
find_sti_class
(
columns_hash
[
inheritance_column
].
default
)
end
end
def
subclass_from_attributes
(
attrs
)
attrs
=
attrs
.
to_h
if
attrs
.
respond_to?
(
:permitted?
)
subclass_name
=
attrs
.
with_indifferent_access
[
inheritance_column
]
...
...
activerecord/test/cases/inheritance_test.rb
浏览文件 @
6b18bdd0
...
...
@@ -478,4 +478,24 @@ def test_sti_type_from_attributes_disabled_in_non_sti_class
product
=
Shop
::
Product
.
new
(
:type
=>
phone
)
assert
product
.
save
end
def
test_inheritance_new_with_subclass_as_default
original_type
=
Company
.
columns_hash
[
"type"
].
default
ActiveRecord
::
Base
.
connection
.
change_column_default
:companies
,
:type
,
'Firm'
Company
.
reset_column_information
# this is the case when attrs is a +Hash+, but we didn't specify the type,
# so we need default type.
firm
=
Company
.
new
(
firm_name:
'Shri Hans Plastic'
)
assert_equal
'Firm'
,
firm
.
type
assert_instance_of
Firm
,
firm
firm
=
Company
.
new
# this is the case when attrs is nil
assert_equal
'Firm'
,
firm
.
type
assert_instance_of
Firm
,
firm
firm
=
Company
.
new
(
type:
'Client'
)
assert_equal
'Client'
,
firm
.
type
assert_instance_of
Client
,
firm
ensure
ActiveRecord
::
Base
.
connection
.
change_column_default
:companies
,
:type
,
original_type
Company
.
reset_column_information
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录