Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
90c9ae58
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,发现更多精彩内容 >>
提交
90c9ae58
编写于
7月 26, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #7171 from beerlington/refactor_inheritance_base_class
Refactor ActiveRecord::Inheritance.base_class logic
上级
f4c2a12f
d0aebd53
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
45 addition
and
30 deletion
+45
-30
activerecord/lib/active_record/inheritance.rb
activerecord/lib/active_record/inheritance.rb
+15
-18
activerecord/test/cases/base_test.rb
activerecord/test/cases/base_test.rb
+0
-12
activerecord/test/cases/inheritance_test.rb
activerecord/test/cases/inheritance_test.rb
+30
-0
未找到文件。
activerecord/lib/active_record/inheritance.rb
浏览文件 @
90c9ae58
...
@@ -41,14 +41,26 @@ def symbolized_sti_name
...
@@ -41,14 +41,26 @@ def symbolized_sti_name
@symbolized_sti_name
||=
sti_name
.
present?
?
sti_name
.
to_sym
:
symbolized_base_class
@symbolized_sti_name
||=
sti_name
.
present?
?
sti_name
.
to_sym
:
symbolized_base_class
end
end
# Returns the base AR subclass that this class descends from. If A
# Returns the class descending directly from ActiveRecord::Base (or
# extends AR::Base, A.base_class will return A. If B descends from A
# that includes ActiveRecord::Model), or an abstract class, if any, in
# the inheritance hierarchy.
#
# If A extends AR::Base, A.base_class will return A. If B descends from A
# through some arbitrarily deep hierarchy, B.base_class will return A.
# through some arbitrarily deep hierarchy, B.base_class will return A.
#
#
# If B < A and C < B and if A is an abstract_class then both B.base_class
# If B < A and C < B and if A is an abstract_class then both B.base_class
# and C.base_class would return B as the answer since A is an abstract_class.
# and C.base_class would return B as the answer since A is an abstract_class.
def
base_class
def
base_class
class_of_active_record_descendant
(
self
)
unless
self
<
Model
::
Tag
raise
ActiveRecordError
,
"
#{
name
}
doesn't belong in a hierarchy descending from ActiveRecord"
end
sup
=
active_record_super
if
sup
.
in?
([
Base
,
Model
])
||
sup
.
abstract_class?
self
else
sup
.
base_class
end
end
end
# Set this to true if this is an abstract class (see <tt>abstract_class?</tt>).
# Set this to true if this is an abstract class (see <tt>abstract_class?</tt>).
...
@@ -96,21 +108,6 @@ def active_record_super #:nodoc:
...
@@ -96,21 +108,6 @@ def active_record_super #:nodoc:
protected
protected
# Returns the class descending directly from ActiveRecord::Base or an
# abstract class, if any, in the inheritance hierarchy.
def
class_of_active_record_descendant
(
klass
)
unless
klass
<
Model
::
Tag
raise
ActiveRecordError
,
"
#{
name
}
doesn't belong in a hierarchy descending from ActiveRecord"
end
sup
=
klass
.
active_record_super
if
[
Base
,
Model
].
include?
(
klass
)
||
[
Base
,
Model
].
include?
(
sup
)
||
sup
.
abstract_class?
klass
else
class_of_active_record_descendant
(
sup
)
end
end
# Returns the class type of the record using the current module as a prefix. So descendants of
# Returns the class type of the record using the current module as a prefix. So descendants of
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
def
compute_type
(
type_name
)
def
compute_type
(
type_name
)
...
...
activerecord/test/cases/base_test.rb
浏览文件 @
90c9ae58
...
@@ -1619,18 +1619,6 @@ def test_abstract_class_table_name
...
@@ -1619,18 +1619,6 @@ def test_abstract_class_table_name
assert_nil
AbstractCompany
.
table_name
assert_nil
AbstractCompany
.
table_name
end
end
def
test_base_class
assert_equal
LoosePerson
,
LoosePerson
.
base_class
assert_equal
LooseDescendant
,
LooseDescendant
.
base_class
assert_equal
TightPerson
,
TightPerson
.
base_class
assert_equal
TightPerson
,
TightDescendant
.
base_class
assert_equal
Post
,
Post
.
base_class
assert_equal
Post
,
SpecialPost
.
base_class
assert_equal
Post
,
StiPost
.
base_class
assert_equal
SubStiPost
,
SubStiPost
.
base_class
end
def
test_descends_from_active_record
def
test_descends_from_active_record
assert
!
ActiveRecord
::
Base
.
descends_from_active_record?
assert
!
ActiveRecord
::
Base
.
descends_from_active_record?
...
...
activerecord/test/cases/inheritance_test.rb
浏览文件 @
90c9ae58
require
"cases/helper"
require
"cases/helper"
require
'models/company'
require
'models/company'
require
'models/person'
require
'models/post'
require
'models/project'
require
'models/project'
require
'models/subscriber'
require
'models/subscriber'
require
'models/teapot'
class
InheritanceTest
<
ActiveRecord
::
TestCase
class
InheritanceTest
<
ActiveRecord
::
TestCase
fixtures
:companies
,
:projects
,
:subscribers
,
:accounts
fixtures
:companies
,
:projects
,
:subscribers
,
:accounts
...
@@ -70,6 +73,33 @@ def test_company_descends_from_active_record
...
@@ -70,6 +73,33 @@ def test_company_descends_from_active_record
assert
!
Class
.
new
(
Company
).
descends_from_active_record?
,
'Company subclass should not descend from ActiveRecord::Base'
assert
!
Class
.
new
(
Company
).
descends_from_active_record?
,
'Company subclass should not descend from ActiveRecord::Base'
end
end
def
test_inheritance_base_class
assert_equal
Post
,
Post
.
base_class
assert_equal
Post
,
SpecialPost
.
base_class
assert_equal
Post
,
StiPost
.
base_class
assert_equal
SubStiPost
,
SubStiPost
.
base_class
end
def
test_active_record_model_included_base_class
assert_equal
Teapot
,
Teapot
.
base_class
end
def
test_abstract_inheritance_base_class
assert_equal
LoosePerson
,
LoosePerson
.
base_class
assert_equal
LooseDescendant
,
LooseDescendant
.
base_class
assert_equal
TightPerson
,
TightPerson
.
base_class
assert_equal
TightPerson
,
TightDescendant
.
base_class
end
def
test_base_class_activerecord_error
klass
=
Class
.
new
{
extend
ActiveRecord
::
Configuration
include
ActiveRecord
::
Inheritance
}
assert_raise
(
ActiveRecord
::
ActiveRecordError
)
{
klass
.
base_class
}
end
def
test_a_bad_type_column
def
test_a_bad_type_column
#SQLServer need to turn Identity Insert On before manually inserting into the Identity column
#SQLServer need to turn Identity Insert On before manually inserting into the Identity column
if
current_adapter?
(
:SybaseAdapter
)
if
current_adapter?
(
:SybaseAdapter
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录