Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
19304a97
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,发现更多精彩内容 >>
提交
19304a97
编写于
10月 13, 2010
作者:
J
Jon Leighton
提交者:
Aaron Patterson
10月 13, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Renaming and formatting changes in JoinDependency
上级
7dcb6334
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
56 addition
and
49 deletion
+56
-49
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
+51
-47
activerecord/lib/active_record/relation/finder_methods.rb
activerecord/lib/active_record/relation/finder_methods.rb
+5
-2
未找到文件。
activerecord/lib/active_record/associations.rb
浏览文件 @
19304a97
...
...
@@ -1833,10 +1833,10 @@ def create_extension_modules(association_id, block_extension, extensions)
end
class
JoinDependency
# :nodoc:
attr_reader
:joins
,
:reflections
,
:table_aliases
attr_reader
:join
_part
s
,
:reflections
,
:table_aliases
def
initialize
(
base
,
associations
,
joins
)
@join
s
=
[
JoinBase
.
new
(
base
,
joins
)]
@join
_parts
=
[
JoinBase
.
new
(
base
,
joins
)]
@associations
=
associations
@reflections
=
[]
@base_records_hash
=
{}
...
...
@@ -1849,17 +1849,17 @@ def initialize(base, associations, joins)
def
graft
(
*
associations
)
associations
.
each
do
|
association
|
join_associations
.
detect
{
|
a
|
association
==
a
}
||
build
(
association
.
reflection
.
name
,
association
.
find_parent_in
(
self
)
||
join_base
,
association
.
join_
class
)
build
(
association
.
reflection
.
name
,
association
.
find_parent_in
(
self
)
||
join_base
,
association
.
join_
type
)
end
self
end
def
join_associations
@joins
.
last
(
@join
s
.
length
-
1
)
join_parts
.
last
(
join_part
s
.
length
-
1
)
end
def
join_base
@joins
[
0
]
join_parts
.
first
end
def
count_aliases_from_table_joins
(
name
)
...
...
@@ -1917,24 +1917,24 @@ def remove_duplicate_results!(base, records, associations)
protected
def
build
(
associations
,
parent
=
nil
,
join_
class
=
Arel
::
InnerJoin
)
parent
||=
@join
s
.
last
def
build
(
associations
,
parent
=
nil
,
join_
type
=
Arel
::
InnerJoin
)
parent
||=
join_part
s
.
last
case
associations
when
Symbol
,
String
reflection
=
parent
.
reflections
[
associations
.
to_s
.
intern
]
or
raise
ConfigurationError
,
"Association named '
#{
associations
}
' was not found; perhaps you misspelled it?"
@reflections
<<
reflection
join_association
=
build_join_association
(
reflection
,
parent
)
join_association
.
join_
class
=
join_class
@joins
<<
join_association
join_association
.
join_
type
=
join_type
@join
_part
s
<<
join_association
when
Array
associations
.
each
do
|
association
|
build
(
association
,
parent
,
join_
class
)
build
(
association
,
parent
,
join_
type
)
end
when
Hash
associations
.
keys
.
sort
{
|
a
,
b
|
a
.
to_s
<=>
b
.
to_s
}.
each
do
|
name
|
build
(
name
,
parent
,
join_
class
)
build
(
associations
[
name
],
nil
,
join_
class
)
build
(
name
,
parent
,
join_
type
)
build
(
associations
[
name
],
nil
,
join_
type
)
end
else
raise
ConfigurationError
,
associations
.
inspect
...
...
@@ -1951,60 +1951,64 @@ def build_join_association(reflection, parent)
JoinAssociation
.
new
(
reflection
,
self
,
parent
)
end
def
construct
(
parent
,
associations
,
joins
,
row
)
def
construct
(
parent
,
associations
,
join
_part
s
,
row
)
case
associations
when
Symbol
,
String
join
=
joins
.
detect
{
|
j
|
j
.
reflection
.
name
.
to_s
==
associations
.
to_s
&&
j
.
parent_table_name
==
parent
.
class
.
table_name
}
raise
(
ConfigurationError
,
"No such association"
)
if
join
.
nil?
join_part
=
join_parts
.
detect
{
|
j
|
j
.
reflection
.
name
.
to_s
==
associations
.
to_s
&&
j
.
parent_table_name
==
parent
.
class
.
table_name
}
raise
(
ConfigurationError
,
"No such association"
)
if
join_part
.
nil?
join
s
.
delete
(
join
)
construct_association
(
parent
,
join
,
row
)
join
_parts
.
delete
(
join_part
)
construct_association
(
parent
,
join
_part
,
row
)
when
Array
associations
.
each
do
|
association
|
construct
(
parent
,
association
,
joins
,
row
)
construct
(
parent
,
association
,
join
_part
s
,
row
)
end
when
Hash
associations
.
sort_by
{
|
k
,
_
|
k
.
to_s
}.
each
do
|
name
,
assoc
|
join
=
joins
.
detect
{
|
j
|
j
.
reflection
.
name
.
to_s
==
name
.
to_s
&&
j
.
parent_table_name
==
parent
.
class
.
table_name
}
raise
(
ConfigurationError
,
"No such association"
)
if
join
.
nil?
association
=
construct_association
(
parent
,
join
,
row
)
joins
.
delete
(
join
)
construct
(
association
,
assoc
,
joins
,
row
)
if
association
join_part
=
join_parts
.
detect
{
|
j
|
j
.
reflection
.
name
.
to_s
==
name
.
to_s
&&
j
.
parent_table_name
==
parent
.
class
.
table_name
}
raise
(
ConfigurationError
,
"No such association"
)
if
join_part
.
nil?
association
=
construct_association
(
parent
,
join_part
,
row
)
join_parts
.
delete
(
join_part
)
construct
(
association
,
assoc
,
join_parts
,
row
)
if
association
end
else
raise
ConfigurationError
,
associations
.
inspect
end
end
def
construct_association
(
record
,
join
,
row
)
return
if
record
.
id
.
to_s
!=
join
.
parent
.
record_id
(
row
).
to_s
def
construct_association
(
record
,
join
_part
,
row
)
return
if
record
.
id
.
to_s
!=
join
_part
.
parent
.
record_id
(
row
).
to_s
macro
=
join
.
reflection
.
macro
macro
=
join
_part
.
reflection
.
macro
if
macro
==
:has_one
return
if
record
.
instance_variable_defined?
(
"@
#{
join
.
reflection
.
name
}
"
)
association
=
join
.
instantiate
(
row
)
unless
row
[
join
.
aliased_primary_key
].
nil?
set_target_and_inverse
(
join
,
association
,
record
)
return
if
record
.
instance_variable_defined?
(
"@
#{
join
_part
.
reflection
.
name
}
"
)
association
=
join
_part
.
instantiate
(
row
)
unless
row
[
join_part
.
aliased_primary_key
].
nil?
set_target_and_inverse
(
join
_part
,
association
,
record
)
else
return
if
row
[
join
.
aliased_primary_key
].
nil?
association
=
join
.
instantiate
(
row
)
return
if
row
[
join
_part
.
aliased_primary_key
].
nil?
association
=
join
_part
.
instantiate
(
row
)
case
macro
when
:has_many
,
:has_and_belongs_to_many
collection
=
record
.
send
(
join
.
reflection
.
name
)
collection
=
record
.
send
(
join
_part
.
reflection
.
name
)
collection
.
loaded
collection
.
target
.
push
(
association
)
collection
.
__send__
(
:set_inverse_instance
,
association
,
record
)
when
:belongs_to
set_target_and_inverse
(
join
,
association
,
record
)
set_target_and_inverse
(
join
_part
,
association
,
record
)
else
raise
ConfigurationError
,
"unknown macro:
#{
join
.
reflection
.
macro
}
"
raise
ConfigurationError
,
"unknown macro:
#{
join
_part
.
reflection
.
macro
}
"
end
end
association
end
def
set_target_and_inverse
(
join
,
association
,
record
)
association_proxy
=
record
.
send
(
"set_
#{
join
.
reflection
.
name
}
_target"
,
association
)
def
set_target_and_inverse
(
join
_part
,
association
,
record
)
association_proxy
=
record
.
send
(
"set_
#{
join
_part
.
reflection
.
name
}
_target"
,
association
)
association_proxy
.
__send__
(
:set_inverse_instance
,
association
,
record
)
end
...
...
@@ -2119,7 +2123,7 @@ class JoinAssociation < JoinPart # :nodoc:
attr_reader
:parent
# What type of join will be generated, either Arel::InnerJoin (default) or Arel::OuterJoin
attr_accessor
:join_
class
attr_accessor
:join_
type
# These implement abstract methods from the superclass
attr_reader
:aliased_prefix
,
:aliased_table_name
...
...
@@ -2139,7 +2143,7 @@ def initialize(reflection, join_dependency, parent = nil)
@reflection
=
reflection
@join_dependency
=
join_dependency
@parent
=
parent
@join_
class
=
Arel
::
InnerJoin
@join_
type
=
Arel
::
InnerJoin
# This must be done eagerly upon initialisation because the alias which is produced
# depends on the state of the join dependency, but we want it to work the same way
...
...
@@ -2154,8 +2158,8 @@ def ==(other)
end
def
find_parent_in
(
other_join_dependency
)
other_join_dependency
.
join
s
.
detect
do
|
join
|
self
.
parent
==
join
other_join_dependency
.
join
_parts
.
detect
do
|
join_part
|
self
.
parent
==
join
_part
end
end
...
...
@@ -2164,7 +2168,7 @@ def join_to(relation)
end
def
join_relation
(
joining_relation
)
self
.
join_
class
=
Arel
::
OuterJoin
self
.
join_
type
=
Arel
::
OuterJoin
joining_relation
.
joins
(
self
)
end
...
...
@@ -2210,7 +2214,7 @@ def table_alias_for(table_name, table_alias)
end
def
table_name_and_alias
table_alias_for
table_name
,
@
aliased_table_name
table_alias_for
table_name
,
aliased_table_name
end
def
interpolate_sql
(
sql
)
...
...
@@ -2220,7 +2224,7 @@ def interpolate_sql(sql)
private
def
allocate_aliases
@aliased_prefix
=
"t
#{
join_dependency
.
joins
.
size
}
"
@aliased_prefix
=
"t
#{
join_dependency
.
join
_part
s
.
size
}
"
@aliased_table_name
=
aliased_table_name_for
(
table_name
)
if
reflection
.
macro
==
:has_and_belongs_to_many
...
...
@@ -2235,7 +2239,7 @@ def process_conditions(conditions, table_name)
end
def
join_target_table
(
relation
,
*
conditions
)
relation
=
relation
.
join
(
target_table
,
join_
class
)
relation
=
relation
.
join
(
target_table
,
join_
type
)
# If the target table is an STI model then we must be sure to only include records of
# its type and its sub-types.
...
...
@@ -2267,7 +2271,7 @@ def join_has_and_belongs_to_many_to(relation)
fk
=
options
[
:foreign_key
]
||
reflection
.
active_record
.
to_s
.
foreign_key
klass_fk
=
options
[
:association_foreign_key
]
||
reflection
.
klass
.
to_s
.
foreign_key
relation
=
relation
.
join
(
join_table
,
join_
class
)
relation
=
relation
.
join
(
join_table
,
join_
type
)
relation
=
relation
.
on
(
join_table
[
fk
].
eq
(
parent_table
[
reflection
.
active_record
.
primary_key
])
...
...
@@ -2355,7 +2359,7 @@ def join_has_many_through_to(relation)
jt_conditions
<<
process_conditions
(
through_reflection
.
options
[
:conditions
],
aliased_table_name
)
end
relation
=
relation
.
join
(
join_table
,
join_
class
).
on
(
*
jt_conditions
)
relation
=
relation
.
join
(
join_table
,
join_
type
).
on
(
*
jt_conditions
)
join_target_table
(
relation
,
...
...
activerecord/lib/active_record/relation/finder_methods.rb
浏览文件 @
19304a97
...
...
@@ -343,8 +343,11 @@ def find_last
end
def
column_aliases
(
join_dependency
)
join_dependency
.
joins
.
collect
{
|
join
|
join
.
column_names_with_alias
.
collect
{
|
column_name
,
aliased_name
|
"
#{
connection
.
quote_table_name
join
.
aliased_table_name
}
.
#{
connection
.
quote_column_name
column_name
}
AS
#{
aliased_name
}
"
}}.
flatten
.
join
(
", "
)
join_dependency
.
join_parts
.
collect
{
|
join_part
|
join_part
.
column_names_with_alias
.
collect
{
|
column_name
,
aliased_name
|
"
#{
connection
.
quote_table_name
join_part
.
aliased_table_name
}
.
#{
connection
.
quote_column_name
column_name
}
AS
#{
aliased_name
}
"
}
}.
flatten
.
join
(
", "
)
end
def
using_limitable_reflections?
(
reflections
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录