Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
eca11bfd
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,发现更多精彩内容 >>
提交
eca11bfd
编写于
1月 04, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of
git://github.com/rails/rails
上级
b0947bf9
a6816583
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
46 addition
and
10 deletion
+46
-10
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+8
-2
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+10
-8
activerecord/lib/active_record/relation/query_methods.rb
activerecord/lib/active_record/relation/query_methods.rb
+4
-0
activerecord/lib/active_record/relation/spawn_methods.rb
activerecord/lib/active_record/relation/spawn_methods.rb
+10
-0
activerecord/test/cases/multiple_db_test.rb
activerecord/test/cases/multiple_db_test.rb
+6
-0
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+8
-0
未找到文件。
activerecord/lib/active_record/base.rb
浏览文件 @
eca11bfd
...
...
@@ -1510,11 +1510,17 @@ def active_relation
end
def
active_relation_table
(
table_name_alias
=
nil
)
Arel
::
Table
.
new
(
table_name
,
:as
=>
table_name_alias
)
Arel
::
Table
.
new
(
table_name
,
:as
=>
table_name_alias
,
:engine
=>
active_relation_engine
)
end
def
active_relation_engine
@active_relation_engine
||=
Arel
::
Sql
::
Engine
.
new
(
self
)
@active_relation_engine
||=
begin
if
self
==
ActiveRecord
::
Base
Arel
::
Table
.
engine
else
connection_handler
.
connection_pools
[
name
]
?
Arel
::
Sql
::
Engine
.
new
(
self
)
:
superclass
.
active_relation_engine
end
end
end
private
...
...
activerecord/lib/active_record/relation.rb
浏览文件 @
eca11bfd
...
...
@@ -6,7 +6,7 @@ class Relation
attr_reader
:relation
,
:klass
attr_writer
:readonly
,
:table
attr_accessor
:preload_associations
,
:eager_load_associations
,
:includes_associations
attr_accessor
:preload_associations
,
:eager_load_associations
,
:includes_associations
,
:create_with_attributes
def
initialize
(
klass
,
relation
)
@klass
,
@relation
=
klass
,
relation
...
...
@@ -124,13 +124,13 @@ def reload
end
def
reset
@first
=
@last
=
@
create_scope
=
@to_sql
=
@order_claus
e
=
nil
@first
=
@last
=
@
to_sql
=
@order_clause
=
@scope_for_creat
e
=
nil
@records
=
[]
self
end
def
table
@table
||=
Arel
::
Table
.
new
(
@klass
.
table_name
,
Arel
::
Sql
::
Engine
.
new
(
@klass
)
)
@table
||=
Arel
::
Table
.
new
(
@klass
.
table_name
,
:engine
=>
@klass
.
active_relation_engine
)
end
def
primary_key
...
...
@@ -163,13 +163,15 @@ def method_missing(method, *args, &block)
end
def
with_create_scope
@klass
.
send
(
:with_scope
,
:create
=>
create_scop
e
)
{
yield
}
@klass
.
send
(
:with_scope
,
:create
=>
scope_for_creat
e
)
{
yield
}
end
def
create_scope
@create_scope
||=
wheres
.
inject
({})
do
|
hash
,
where
|
hash
[
where
.
operand1
.
name
]
=
where
.
operand2
.
value
if
where
.
is_a?
(
Arel
::
Predicates
::
Equality
)
hash
def
scope_for_create
@scope_for_create
||=
begin
@create_with_attributes
||
wheres
.
inject
({})
do
|
hash
,
where
|
hash
[
where
.
operand1
.
name
]
=
where
.
operand2
.
value
if
where
.
is_a?
(
Arel
::
Predicates
::
Equality
)
hash
end
end
end
...
...
activerecord/lib/active_record/relation/query_methods.rb
浏览文件 @
eca11bfd
...
...
@@ -17,6 +17,10 @@ def readonly(status = true)
spawn
.
tap
{
|
r
|
r
.
readonly
=
status
}
end
def
create_with
(
attributes
=
{})
spawn
.
tap
{
|
r
|
r
.
create_with_attributes
=
attributes
}
end
def
select
(
selects
)
if
selects
.
present?
relation
=
spawn
(
@relation
.
project
(
selects
))
...
...
activerecord/lib/active_record/relation/spawn_methods.rb
浏览文件 @
eca11bfd
...
...
@@ -6,6 +6,7 @@ def spawn(relation = @relation)
relation
.
preload_associations
=
@preload_associations
relation
.
eager_load_associations
=
@eager_load_associations
relation
.
includes_associations
=
@includes_associations
relation
.
create_with_attributes
=
@create_with_attributes
relation
.
table
=
table
relation
end
...
...
@@ -32,6 +33,14 @@ def merge(r)
merged_order
=
relation_order
.
present?
?
relation_order
:
order_clause
merged_relation
=
merged_relation
.
order
(
merged_order
)
merged_relation
.
create_with_attributes
=
@create_with_attributes
if
@create_with_attributes
&&
r
.
create_with_attributes
merged_relation
.
create_with_attributes
=
@create_with_attributes
.
merge
(
r
.
create_with_attributes
)
else
merged_relation
.
create_with_attributes
=
r
.
create_with_attributes
||
@create_with_attributes
end
merged_wheres
=
@relation
.
wheres
r
.
wheres
.
each
do
|
w
|
...
...
@@ -56,6 +65,7 @@ def except(*skips)
end
result
.
readonly
=
self
.
readonly
unless
skips
.
include?
(
:readonly
)
result
.
create_with_attributes
=
@create_with_attributes
unless
skips
.
include?
(
:create_with
)
result
=
result
.
joins
(
@relation
.
joins
(
@relation
))
unless
skips
.
include?
(
:joins
)
result
=
result
.
group
(
@relation
.
groupings
)
unless
skips
.
include?
(
:group
)
...
...
activerecord/test/cases/multiple_db_test.rb
浏览文件 @
eca11bfd
require
"cases/helper"
require
'models/entrant'
require
'models/bird'
# So we can test whether Course.connection survives a reload.
require_dependency
'models/course'
...
...
@@ -82,4 +83,9 @@ def test_transactions_across_databases
assert_equal
"Ruby Development"
,
Course
.
find
(
1
).
name
assert_equal
"Ruby Developer"
,
Entrant
.
find
(
1
).
name
end
def
test_arel_table_engines
assert_not_equal
Entrant
.
active_relation_engine
,
Course
.
active_relation_engine
assert_equal
Entrant
.
active_relation_engine
,
Bird
.
active_relation_engine
end
end
activerecord/test/cases/relations_test.rb
浏览文件 @
eca11bfd
...
...
@@ -545,6 +545,14 @@ def test_create_bang
assert_equal
'hen'
,
hen
.
name
end
def
test_explicit_create_scope
hens
=
Bird
.
where
(
:name
=>
'hen'
)
assert_equal
'hen'
,
hens
.
new
.
name
hens
=
hens
.
create_with
(
:name
=>
'cock'
)
assert_equal
'cock'
,
hens
.
new
.
name
end
def
test_except
relation
=
Post
.
where
(
:author_id
=>
1
).
order
(
'id ASC'
).
limit
(
1
)
assert_equal
[
posts
(
:welcome
)],
relation
.
all
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录