Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
85343d95
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,发现更多精彩内容 >>
提交
85343d95
编写于
7月 14, 2014
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16089 from eileencodes/refactor-reflections-from-sub-classes-to-delegates
Refactor reflections from sub classes to delegates
上级
7b52a26b
f8d2899d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
158 addition
and
101 deletion
+158
-101
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
+1
-1
activerecord/lib/active_record/reflection.rb
activerecord/lib/active_record/reflection.rb
+127
-66
activerecord/test/cases/reflection_test.rb
activerecord/test/cases/reflection_test.rb
+30
-34
未找到文件。
activerecord/lib/active_record/associations.rb
浏览文件 @
85343d95
...
@@ -1587,7 +1587,7 @@ def has_and_belongs_to_many(name, scope = nil, options = {}, &extension)
...
@@ -1587,7 +1587,7 @@ def has_and_belongs_to_many(name, scope = nil, options = {}, &extension)
scope
=
nil
scope
=
nil
end
end
habtm_reflection
=
ActiveRecord
::
Reflection
::
HasAndBelongsToManyReflection
.
new
(
:has_and_belongs_to_many
,
name
,
scope
,
options
,
self
)
habtm_reflection
=
ActiveRecord
::
Reflection
::
HasAndBelongsToManyReflection
.
new
(
name
,
scope
,
options
,
self
)
builder
=
Builder
::
HasAndBelongsToMany
.
new
name
,
self
,
options
builder
=
Builder
::
HasAndBelongsToMany
.
new
name
,
self
,
options
...
...
activerecord/lib/active_record/reflection.rb
浏览文件 @
85343d95
...
@@ -13,14 +13,21 @@ module Reflection # :nodoc:
...
@@ -13,14 +13,21 @@ module Reflection # :nodoc:
end
end
def
self
.
create
(
macro
,
name
,
scope
,
options
,
ar
)
def
self
.
create
(
macro
,
name
,
scope
,
options
,
ar
)
case
macro
klass
=
case
macro
when
:has_many
,
:belongs_to
,
:has_one
when
:composed_of
klass
=
options
[
:through
]
?
ThroughReflection
:
AssociationReflection
AggregateReflection
when
:composed_of
when
:has_many
klass
=
AggregateReflection
HasManyReflection
end
when
:has_one
HasOneReflection
klass
.
new
(
macro
,
name
,
scope
,
options
,
ar
)
when
:belongs_to
BelongsToReflection
else
raise
"Unsupported Macro:
#{
macro
}
"
end
reflection
=
klass
.
new
(
name
,
scope
,
options
,
ar
)
options
[
:through
]
?
ThroughReflection
.
new
(
reflection
)
:
reflection
end
end
def
self
.
add_reflection
(
ar
,
name
,
reflection
)
def
self
.
add_reflection
(
ar
,
name
,
reflection
)
...
@@ -110,6 +117,52 @@ def reflect_on_all_autosave_associations
...
@@ -110,6 +117,52 @@ def reflect_on_all_autosave_associations
end
end
end
end
# Holds all the methods that are shared between MacroReflection, AssociationReflection
# and ThroughReflection
class
AbstractReflection
# :nodoc:
def
table_name
klass
.
table_name
end
# Returns a new, unsaved instance of the associated class. +attributes+ will
# be passed to the class's constructor.
def
build_association
(
attributes
,
&
block
)
klass
.
new
(
attributes
,
&
block
)
end
def
quoted_table_name
klass
.
quoted_table_name
end
def
primary_key_type
klass
.
type_for_attribute
(
klass
.
primary_key
)
end
# Returns the class name for the macro.
#
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>'Money'</tt>
# <tt>has_many :clients</tt> returns <tt>'Client'</tt>
def
class_name
@class_name
||=
(
options
[
:class_name
]
||
derive_class_name
).
to_s
end
JoinKeys
=
Struct
.
new
(
:key
,
:foreign_key
)
# :nodoc:
def
join_keys
(
assoc_klass
)
if
source_macro
==
:belongs_to
if
polymorphic?
reflection_key
=
association_primary_key
(
assoc_klass
)
else
reflection_key
=
association_primary_key
end
reflection_foreign_key
=
foreign_key
else
reflection_foreign_key
=
active_record_primary_key
reflection_key
=
foreign_key
end
JoinKeys
.
new
(
reflection_key
,
reflection_foreign_key
)
end
end
# Base class for AggregateReflection and AssociationReflection. Objects of
# Base class for AggregateReflection and AssociationReflection. Objects of
# AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.
# AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.
#
#
...
@@ -117,7 +170,7 @@ def reflect_on_all_autosave_associations
...
@@ -117,7 +170,7 @@ def reflect_on_all_autosave_associations
# AggregateReflection
# AggregateReflection
# AssociationReflection
# AssociationReflection
# ThroughReflection
# ThroughReflection
class
MacroReflection
class
MacroReflection
<
AbstractReflection
# Returns the name of the macro.
# Returns the name of the macro.
#
#
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>:balance</tt>
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>:balance</tt>
...
@@ -142,8 +195,7 @@ class MacroReflection
...
@@ -142,8 +195,7 @@ class MacroReflection
attr_reader
:plural_name
# :nodoc:
attr_reader
:plural_name
# :nodoc:
def
initialize
(
macro
,
name
,
scope
,
options
,
active_record
)
def
initialize
(
name
,
scope
,
options
,
active_record
)
@macro
=
macro
@name
=
name
@name
=
name
@scope
=
scope
@scope
=
scope
@options
=
options
@options
=
options
...
@@ -167,15 +219,11 @@ def autosave=(autosave)
...
@@ -167,15 +219,11 @@ def autosave=(autosave)
# <tt>composed_of :balance, class_name: 'Money'</tt> returns the Money class
# <tt>composed_of :balance, class_name: 'Money'</tt> returns the Money class
# <tt>has_many :clients</tt> returns the Client class
# <tt>has_many :clients</tt> returns the Client class
def
klass
def
klass
@klass
||=
c
lass_name
.
constantize
@klass
||=
c
ompute_class
(
class_name
)
end
end
# Returns the class name for the macro.
def
compute_class
(
name
)
#
name
.
constantize
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>'Money'</tt>
# <tt>has_many :clients</tt> returns <tt>'Client'</tt>
def
class_name
@class_name
||=
(
options
[
:class_name
]
||
derive_class_name
).
to_s
end
end
# Returns +true+ if +self+ and +other_aggregation+ have the same +name+ attribute, +active_record+ attribute,
# Returns +true+ if +self+ and +other_aggregation+ have the same +name+ attribute, +active_record+ attribute,
...
@@ -188,23 +236,6 @@ def ==(other_aggregation)
...
@@ -188,23 +236,6 @@ def ==(other_aggregation)
active_record
==
other_aggregation
.
active_record
active_record
==
other_aggregation
.
active_record
end
end
JoinKeys
=
Struct
.
new
(
:key
,
:foreign_key
)
# :nodoc:
def
join_keys
(
assoc_klass
)
if
source_macro
==
:belongs_to
if
polymorphic?
reflection_key
=
association_primary_key
(
assoc_klass
)
else
reflection_key
=
association_primary_key
end
reflection_foreign_key
=
foreign_key
else
reflection_key
=
foreign_key
reflection_foreign_key
=
active_record_primary_key
end
JoinKeys
.
new
(
reflection_key
,
reflection_foreign_key
)
end
private
private
def
derive_class_name
def
derive_class_name
name
.
to_s
.
camelize
name
.
to_s
.
camelize
...
@@ -237,15 +268,18 @@ class AssociationReflection < MacroReflection #:nodoc:
...
@@ -237,15 +268,18 @@ class AssociationReflection < MacroReflection #:nodoc:
# a new association object. Use +build_association+ or +create_association+
# a new association object. Use +build_association+ or +create_association+
# instead. This allows plugins to hook into association object creation.
# instead. This allows plugins to hook into association object creation.
def
klass
def
klass
@klass
||=
active_record
.
send
(
:compute_type
,
class_name
)
@klass
||=
compute_class
(
class_name
)
end
def
compute_class
(
name
)
active_record
.
send
(
:compute_type
,
name
)
end
end
attr_reader
:type
,
:foreign_type
attr_reader
:type
,
:foreign_type
attr_accessor
:parent_reflection
# [:name, Reflection]
attr_accessor
:parent_reflection
# [:name, Reflection]
def
initialize
(
macro
,
name
,
scope
,
options
,
active_record
)
def
initialize
(
name
,
scope
,
options
,
active_record
)
super
super
@collection
=
macro
==
:has_many
@automatic_inverse_of
=
nil
@automatic_inverse_of
=
nil
@type
=
options
[
:as
]
&&
"
#{
options
[
:as
]
}
_type"
@type
=
options
[
:as
]
&&
"
#{
options
[
:as
]
}
_type"
@foreign_type
=
options
[
:foreign_type
]
||
"
#{
name
}
_type"
@foreign_type
=
options
[
:foreign_type
]
||
"
#{
name
}
_type"
...
@@ -264,24 +298,10 @@ def association_scope_cache(conn, owner)
...
@@ -264,24 +298,10 @@ def association_scope_cache(conn, owner)
}
}
end
end
# Returns a new, unsaved instance of the associated class. +attributes+ will
# be passed to the class's constructor.
def
build_association
(
attributes
,
&
block
)
klass
.
new
(
attributes
,
&
block
)
end
def
constructable?
# :nodoc:
def
constructable?
# :nodoc:
@constructable
@constructable
end
end
def
table_name
klass
.
table_name
end
def
quoted_table_name
klass
.
quoted_table_name
end
def
join_table
def
join_table
@join_table
||=
options
[
:join_table
]
||
derive_join_table
@join_table
||=
options
[
:join_table
]
||
derive_join_table
end
end
...
@@ -290,10 +310,6 @@ def foreign_key
...
@@ -290,10 +310,6 @@ def foreign_key
@foreign_key
||=
options
[
:foreign_key
]
||
derive_foreign_key
@foreign_key
||=
options
[
:foreign_key
]
||
derive_foreign_key
end
end
def
primary_key_type
klass
.
type_for_attribute
(
klass
.
primary_key
)
end
def
association_foreign_key
def
association_foreign_key
@association_foreign_key
||=
options
[
:association_foreign_key
]
||
class_name
.
foreign_key
@association_foreign_key
||=
options
[
:association_foreign_key
]
||
class_name
.
foreign_key
end
end
...
@@ -396,7 +412,7 @@ def polymorphic_inverse_of(associated_class)
...
@@ -396,7 +412,7 @@ def polymorphic_inverse_of(associated_class)
# association. Returns +true+ if the +macro+ is either +has_many+ or
# association. Returns +true+ if the +macro+ is either +has_many+ or
# +has_and_belongs_to_many+, +false+ otherwise.
# +has_and_belongs_to_many+, +false+ otherwise.
def
collection?
def
collection?
@collection
false
end
end
# Returns whether or not the association should be validated as part of
# Returns whether or not the association should be validated as part of
...
@@ -560,22 +576,57 @@ def primary_key(klass)
...
@@ -560,22 +576,57 @@ def primary_key(klass)
end
end
end
end
class
HasManyReflection
<
AssociationReflection
#:nodoc:
def
initialize
(
name
,
scope
,
options
,
active_record
)
@macro
=
:has_many
super
(
name
,
scope
,
options
,
active_record
)
end
def
collection?
true
end
end
class
HasOneReflection
<
AssociationReflection
#:nodoc:
def
initialize
(
name
,
scope
,
options
,
active_record
)
@macro
=
:has_one
super
(
name
,
scope
,
options
,
active_record
)
end
end
class
BelongsToReflection
<
AssociationReflection
#:nodoc:
def
initialize
(
name
,
scope
,
options
,
active_record
)
@macro
=
:belongs_to
super
(
name
,
scope
,
options
,
active_record
)
end
end
class
HasAndBelongsToManyReflection
<
AssociationReflection
#:nodoc:
class
HasAndBelongsToManyReflection
<
AssociationReflection
#:nodoc:
def
initialize
(
macro
,
name
,
scope
,
options
,
active_record
)
def
initialize
(
name
,
scope
,
options
,
active_record
)
@macro
=
:has_and_belongs_to_many
super
super
@collection
=
true
end
def
collection?
true
end
end
end
end
# Holds all the meta-data about a :through association as it was specified
# Holds all the meta-data about a :through association as it was specified
# in the Active Record class.
# in the Active Record class.
class
ThroughReflection
<
AssociationReflection
#:nodoc:
class
ThroughReflection
<
AbstractReflection
#:nodoc:
attr_reader
:delegate_reflection
delegate
:foreign_key
,
:foreign_type
,
:association_foreign_key
,
delegate
:foreign_key
,
:foreign_type
,
:association_foreign_key
,
:active_record_primary_key
,
:type
,
:to
=>
:source_reflection
:active_record_primary_key
,
:type
,
:to
=>
:source_reflection
def
initialize
(
macro
,
name
,
scope
,
options
,
active_record
)
def
initialize
(
delegate_reflection
)
super
@delegate_reflection
=
delegate_reflection
@source_reflection_name
=
options
[
:source
]
@klass
=
delegate_reflection
.
options
[
:class
]
@source_reflection_name
=
delegate_reflection
.
options
[
:source
]
end
def
klass
@klass
||=
delegate_reflection
.
compute_class
(
class_name
)
end
end
# Returns the source of the through reflection. It checks both a singularized
# Returns the source of the through reflection. It checks both a singularized
...
@@ -777,15 +828,25 @@ def check_validity!
...
@@ -777,15 +828,25 @@ def check_validity!
protected
protected
def
actual_source_reflection
# FIXME: this is a horrible name
def
actual_source_reflection
# FIXME: this is a horrible name
source_reflection
.
actual_source_reflection
source_reflection
.
send
(
:actual_source_reflection
)
end
end
def
primary_key
(
klass
)
klass
.
primary_key
||
raise
(
UnknownPrimaryKey
.
new
(
klass
))
end
private
private
def
derive_class_name
def
derive_class_name
# get the class_name of the belongs_to association of the through reflection
# get the class_name of the belongs_to association of the through reflection
options
[
:source_type
]
||
source_reflection
.
class_name
options
[
:source_type
]
||
source_reflection
.
class_name
end
end
delegate_methods
=
AssociationReflection
.
public_instance_methods
-
public_instance_methods
delegate
(
*
delegate_methods
,
to: :delegate_reflection
)
end
end
end
end
end
end
activerecord/test/cases/reflection_test.rb
浏览文件 @
85343d95
...
@@ -87,7 +87,7 @@ def test_non_existent_columns_return_nil
...
@@ -87,7 +87,7 @@ def test_non_existent_columns_return_nil
end
end
def
test_reflection_klass_for_nested_class_name
def
test_reflection_klass_for_nested_class_name
reflection
=
MacroReflection
.
new
(
:comp
any
,
nil
,
nil
,
{
:class_name
=>
'MyApplication::Business::Company'
},
ActiveRecord
::
Base
)
reflection
=
ActiveRecord
::
Reflection
.
create
(
:has_m
any
,
nil
,
nil
,
{
:class_name
=>
'MyApplication::Business::Company'
},
ActiveRecord
::
Base
)
assert_nothing_raised
do
assert_nothing_raised
do
assert_equal
MyApplication
::
Business
::
Company
,
reflection
.
klass
assert_equal
MyApplication
::
Business
::
Company
,
reflection
.
klass
end
end
...
@@ -97,21 +97,21 @@ def test_irregular_reflection_class_name
...
@@ -97,21 +97,21 @@ def test_irregular_reflection_class_name
ActiveSupport
::
Inflector
.
inflections
do
|
inflect
|
ActiveSupport
::
Inflector
.
inflections
do
|
inflect
|
inflect
.
irregular
'plural_irregular'
,
'plurales_irregulares'
inflect
.
irregular
'plural_irregular'
,
'plurales_irregulares'
end
end
reflection
=
A
ssociationReflection
.
new
(
:has_many
,
'plurales_irregulares'
,
nil
,
{},
ActiveRecord
::
Base
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has_many
,
'plurales_irregulares'
,
nil
,
{},
ActiveRecord
::
Base
)
assert_equal
'PluralIrregular'
,
reflection
.
class_name
assert_equal
'PluralIrregular'
,
reflection
.
class_name
end
end
def
test_aggregation_reflection
def
test_aggregation_reflection
reflection_for_address
=
AggregateReflection
.
new
(
reflection_for_address
=
AggregateReflection
.
new
(
:
composed_of
,
:
address
,
nil
,
{
:mapping
=>
[
%w(address_street street)
,
%w(address_city city)
,
%w(address_country country)
]
},
Customer
:address
,
nil
,
{
:mapping
=>
[
%w(address_street street)
,
%w(address_city city)
,
%w(address_country country)
]
},
Customer
)
)
reflection_for_balance
=
AggregateReflection
.
new
(
reflection_for_balance
=
AggregateReflection
.
new
(
:
composed_of
,
:
balance
,
nil
,
{
:class_name
=>
"Money"
,
:mapping
=>
%w(balance amount)
},
Customer
:balance
,
nil
,
{
:class_name
=>
"Money"
,
:mapping
=>
%w(balance amount)
},
Customer
)
)
reflection_for_gps_location
=
AggregateReflection
.
new
(
reflection_for_gps_location
=
AggregateReflection
.
new
(
:
composed_of
,
:
gps_location
,
nil
,
{
},
Customer
:gps_location
,
nil
,
{
},
Customer
)
)
assert
Customer
.
reflect_on_all_aggregations
.
include?
(
reflection_for_gps_location
)
assert
Customer
.
reflect_on_all_aggregations
.
include?
(
reflection_for_gps_location
)
...
@@ -135,7 +135,7 @@ def test_reflect_on_all_autosave_associations
...
@@ -135,7 +135,7 @@ def test_reflect_on_all_autosave_associations
end
end
def
test_has_many_reflection
def
test_has_many_reflection
reflection_for_clients
=
A
ssociationReflection
.
new
(
:has_many
,
:clients
,
nil
,
{
:order
=>
"id"
,
:dependent
=>
:destroy
},
Firm
)
reflection_for_clients
=
A
ctiveRecord
::
Reflection
.
create
(
:has_many
,
:clients
,
nil
,
{
:order
=>
"id"
,
:dependent
=>
:destroy
},
Firm
)
assert_equal
reflection_for_clients
,
Firm
.
reflect_on_association
(
:clients
)
assert_equal
reflection_for_clients
,
Firm
.
reflect_on_association
(
:clients
)
...
@@ -147,7 +147,7 @@ def test_has_many_reflection
...
@@ -147,7 +147,7 @@ def test_has_many_reflection
end
end
def
test_has_one_reflection
def
test_has_one_reflection
reflection_for_account
=
A
ssociationReflection
.
new
(
:has_one
,
:account
,
nil
,
{
:foreign_key
=>
"firm_id"
,
:dependent
=>
:destroy
},
Firm
)
reflection_for_account
=
A
ctiveRecord
::
Reflection
.
create
(
:has_one
,
:account
,
nil
,
{
:foreign_key
=>
"firm_id"
,
:dependent
=>
:destroy
},
Firm
)
assert_equal
reflection_for_account
,
Firm
.
reflect_on_association
(
:account
)
assert_equal
reflection_for_account
,
Firm
.
reflect_on_association
(
:account
)
assert_equal
Account
,
Firm
.
reflect_on_association
(
:account
).
klass
assert_equal
Account
,
Firm
.
reflect_on_association
(
:account
).
klass
...
@@ -284,12 +284,12 @@ def test_association_primary_key
...
@@ -284,12 +284,12 @@ def test_association_primary_key
end
end
def
test_association_primary_key_raises_when_missing_primary_key
def
test_association_primary_key_raises_when_missing_primary_key
reflection
=
ActiveRecord
::
Reflection
::
AssociationReflection
.
new
(
:fuu
,
:edge
,
nil
,
{},
Author
)
reflection
=
ActiveRecord
::
Reflection
.
create
(
:has_many
,
:edge
,
nil
,
{},
Author
)
assert_raises
(
ActiveRecord
::
UnknownPrimaryKey
)
{
reflection
.
association_primary_key
}
assert_raises
(
ActiveRecord
::
UnknownPrimaryKey
)
{
reflection
.
association_primary_key
}
through
=
Class
.
new
(
ActiveRecord
::
Reflection
::
ThroughReflection
)
{
through
=
Class
.
new
(
ActiveRecord
::
Reflection
::
ThroughReflection
)
{
define_method
(
:source_reflection
)
{
reflection
}
define_method
(
:source_reflection
)
{
reflection
}
}.
new
(
:fuu
,
:edge
,
nil
,
{},
Author
)
}.
new
(
reflection
)
assert_raises
(
ActiveRecord
::
UnknownPrimaryKey
)
{
through
.
association_primary_key
}
assert_raises
(
ActiveRecord
::
UnknownPrimaryKey
)
{
through
.
association_primary_key
}
end
end
...
@@ -299,7 +299,7 @@ def test_active_record_primary_key
...
@@ -299,7 +299,7 @@ def test_active_record_primary_key
end
end
def
test_active_record_primary_key_raises_when_missing_primary_key
def
test_active_record_primary_key_raises_when_missing_primary_key
reflection
=
ActiveRecord
::
Reflection
::
AssociationReflection
.
new
(
:fuu
,
:author
,
nil
,
{},
Edge
)
reflection
=
ActiveRecord
::
Reflection
.
create
(
:has_many
,
:author
,
nil
,
{},
Edge
)
assert_raises
(
ActiveRecord
::
UnknownPrimaryKey
)
{
reflection
.
active_record_primary_key
}
assert_raises
(
ActiveRecord
::
UnknownPrimaryKey
)
{
reflection
.
active_record_primary_key
}
end
end
...
@@ -317,32 +317,28 @@ def test_collection_association
...
@@ -317,32 +317,28 @@ def test_collection_association
end
end
def
test_default_association_validation
def
test_default_association_validation
assert
A
ssociationReflection
.
new
(
:has_many
,
:clients
,
nil
,
{},
Firm
).
validate?
assert
A
ctiveRecord
::
Reflection
.
create
(
:has_many
,
:clients
,
nil
,
{},
Firm
).
validate?
assert
!
AssociationReflection
.
new
(
:has_one
,
:client
,
nil
,
{},
Firm
).
validate?
assert
!
ActiveRecord
::
Reflection
.
create
(
:has_one
,
:client
,
nil
,
{},
Firm
).
validate?
assert
!
AssociationReflection
.
new
(
:belongs_to
,
:client
,
nil
,
{},
Firm
).
validate?
assert
!
ActiveRecord
::
Reflection
.
create
(
:belongs_to
,
:client
,
nil
,
{},
Firm
).
validate?
assert
!
AssociationReflection
.
new
(
:has_and_belongs_to_many
,
:clients
,
nil
,
{},
Firm
).
validate?
end
end
def
test_always_validate_association_if_explicit
def
test_always_validate_association_if_explicit
assert
AssociationReflection
.
new
(
:has_one
,
:client
,
nil
,
{
:validate
=>
true
},
Firm
).
validate?
assert
ActiveRecord
::
Reflection
.
create
(
:has_one
,
:client
,
nil
,
{
:validate
=>
true
},
Firm
).
validate?
assert
AssociationReflection
.
new
(
:belongs_to
,
:client
,
nil
,
{
:validate
=>
true
},
Firm
).
validate?
assert
ActiveRecord
::
Reflection
.
create
(
:belongs_to
,
:client
,
nil
,
{
:validate
=>
true
},
Firm
).
validate?
assert
AssociationReflection
.
new
(
:has_many
,
:clients
,
nil
,
{
:validate
=>
true
},
Firm
).
validate?
assert
ActiveRecord
::
Reflection
.
create
(
:has_many
,
:clients
,
nil
,
{
:validate
=>
true
},
Firm
).
validate?
assert
AssociationReflection
.
new
(
:has_and_belongs_to_many
,
:clients
,
nil
,
{
:validate
=>
true
},
Firm
).
validate?
end
end
def
test_validate_association_if_autosave
def
test_validate_association_if_autosave
assert
AssociationReflection
.
new
(
:has_one
,
:client
,
nil
,
{
:autosave
=>
true
},
Firm
).
validate?
assert
ActiveRecord
::
Reflection
.
create
(
:has_one
,
:client
,
nil
,
{
:autosave
=>
true
},
Firm
).
validate?
assert
AssociationReflection
.
new
(
:belongs_to
,
:client
,
nil
,
{
:autosave
=>
true
},
Firm
).
validate?
assert
ActiveRecord
::
Reflection
.
create
(
:belongs_to
,
:client
,
nil
,
{
:autosave
=>
true
},
Firm
).
validate?
assert
AssociationReflection
.
new
(
:has_many
,
:clients
,
nil
,
{
:autosave
=>
true
},
Firm
).
validate?
assert
ActiveRecord
::
Reflection
.
create
(
:has_many
,
:clients
,
nil
,
{
:autosave
=>
true
},
Firm
).
validate?
assert
AssociationReflection
.
new
(
:has_and_belongs_to_many
,
:clients
,
nil
,
{
:autosave
=>
true
},
Firm
).
validate?
end
end
def
test_never_validate_association_if_explicit
def
test_never_validate_association_if_explicit
assert
!
AssociationReflection
.
new
(
:has_one
,
:client
,
nil
,
{
:autosave
=>
true
,
:validate
=>
false
},
Firm
).
validate?
assert
!
ActiveRecord
::
Reflection
.
create
(
:has_one
,
:client
,
nil
,
{
:autosave
=>
true
,
:validate
=>
false
},
Firm
).
validate?
assert
!
AssociationReflection
.
new
(
:belongs_to
,
:client
,
nil
,
{
:autosave
=>
true
,
:validate
=>
false
},
Firm
).
validate?
assert
!
ActiveRecord
::
Reflection
.
create
(
:belongs_to
,
:client
,
nil
,
{
:autosave
=>
true
,
:validate
=>
false
},
Firm
).
validate?
assert
!
AssociationReflection
.
new
(
:has_many
,
:clients
,
nil
,
{
:autosave
=>
true
,
:validate
=>
false
},
Firm
).
validate?
assert
!
ActiveRecord
::
Reflection
.
create
(
:has_many
,
:clients
,
nil
,
{
:autosave
=>
true
,
:validate
=>
false
},
Firm
).
validate?
assert
!
AssociationReflection
.
new
(
:has_and_belongs_to_many
,
:clients
,
nil
,
{
:autosave
=>
true
,
:validate
=>
false
},
Firm
).
validate?
end
end
def
test_foreign_key
def
test_foreign_key
...
@@ -364,11 +360,11 @@ def test_join_table
...
@@ -364,11 +360,11 @@ def test_join_table
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'categories'
,
true
)
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'categories'
,
true
)
product
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'products'
,
true
)
product
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'products'
,
true
)
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:categories
,
nil
,
{},
product
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:categories
,
nil
,
{},
product
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
assert_equal
'categories_products'
,
reflection
.
join_table
assert_equal
'categories_products'
,
reflection
.
join_table
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:products
,
nil
,
{},
category
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:products
,
nil
,
{},
category
)
reflection
.
stubs
(
:klass
).
returns
(
product
)
reflection
.
stubs
(
:klass
).
returns
(
product
)
assert_equal
'categories_products'
,
reflection
.
join_table
assert_equal
'categories_products'
,
reflection
.
join_table
end
end
...
@@ -377,11 +373,11 @@ def test_join_table_with_common_prefix
...
@@ -377,11 +373,11 @@ def test_join_table_with_common_prefix
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'catalog_categories'
,
true
)
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'catalog_categories'
,
true
)
product
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'catalog_products'
,
true
)
product
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'catalog_products'
,
true
)
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:categories
,
nil
,
{},
product
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:categories
,
nil
,
{},
product
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
assert_equal
'catalog_categories_products'
,
reflection
.
join_table
assert_equal
'catalog_categories_products'
,
reflection
.
join_table
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:products
,
nil
,
{},
category
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:products
,
nil
,
{},
category
)
reflection
.
stubs
(
:klass
).
returns
(
product
)
reflection
.
stubs
(
:klass
).
returns
(
product
)
assert_equal
'catalog_categories_products'
,
reflection
.
join_table
assert_equal
'catalog_categories_products'
,
reflection
.
join_table
end
end
...
@@ -390,11 +386,11 @@ def test_join_table_with_different_prefix
...
@@ -390,11 +386,11 @@ def test_join_table_with_different_prefix
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'catalog_categories'
,
true
)
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'catalog_categories'
,
true
)
page
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'content_pages'
,
true
)
page
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'content_pages'
,
true
)
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:categories
,
nil
,
{},
page
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:categories
,
nil
,
{},
page
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
assert_equal
'catalog_categories_content_pages'
,
reflection
.
join_table
assert_equal
'catalog_categories_content_pages'
,
reflection
.
join_table
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:pages
,
nil
,
{},
category
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:pages
,
nil
,
{},
category
)
reflection
.
stubs
(
:klass
).
returns
(
page
)
reflection
.
stubs
(
:klass
).
returns
(
page
)
assert_equal
'catalog_categories_content_pages'
,
reflection
.
join_table
assert_equal
'catalog_categories_content_pages'
,
reflection
.
join_table
end
end
...
@@ -403,11 +399,11 @@ def test_join_table_can_be_overridden
...
@@ -403,11 +399,11 @@ def test_join_table_can_be_overridden
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'categories'
,
true
)
category
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'categories'
,
true
)
product
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'products'
,
true
)
product
=
Struct
.
new
(
:table_name
,
:pluralize_table_names
).
new
(
'products'
,
true
)
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:categories
,
nil
,
{
:join_table
=>
'product_categories'
},
product
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:categories
,
nil
,
{
:join_table
=>
'product_categories'
},
product
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
reflection
.
stubs
(
:klass
).
returns
(
category
)
assert_equal
'product_categories'
,
reflection
.
join_table
assert_equal
'product_categories'
,
reflection
.
join_table
reflection
=
A
ssociationReflection
.
new
(
:has_and_belongs_to
_many
,
:products
,
nil
,
{
:join_table
=>
'product_categories'
},
category
)
reflection
=
A
ctiveRecord
::
Reflection
.
create
(
:has
_many
,
:products
,
nil
,
{
:join_table
=>
'product_categories'
},
category
)
reflection
.
stubs
(
:klass
).
returns
(
product
)
reflection
.
stubs
(
:klass
).
returns
(
product
)
assert_equal
'product_categories'
,
reflection
.
join_table
assert_equal
'product_categories'
,
reflection
.
join_table
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录