Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e981adef
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,发现更多精彩内容 >>
未验证
提交
e981adef
编写于
1月 24, 2018
作者:
R
Rafael França
提交者:
GitHub
1月 24, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #31784 from composerinteralia/relation-signature
Avoid passing unnecessary arguments to relation
上级
a5e6f44e
6928950d
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
66 addition
and
38 deletion
+66
-38
activerecord/lib/active_record/association_relation.rb
activerecord/lib/active_record/association_relation.rb
+2
-2
activerecord/lib/active_record/associations/association.rb
activerecord/lib/active_record/associations/association.rb
+1
-1
activerecord/lib/active_record/associations/collection_proxy.rb
...record/lib/active_record/associations/collection_proxy.rb
+1
-1
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+1
-1
activerecord/lib/active_record/reflection.rb
activerecord/lib/active_record/reflection.rb
+5
-1
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+1
-1
activerecord/lib/active_record/relation/merger.rb
activerecord/lib/active_record/relation/merger.rb
+5
-1
activerecord/lib/active_record/relation/spawn_methods.rb
activerecord/lib/active_record/relation/spawn_methods.rb
+1
-1
activerecord/test/cases/batches_test.rb
activerecord/test/cases/batches_test.rb
+5
-1
activerecord/test/cases/collection_cache_key_test.rb
activerecord/test/cases/collection_cache_key_test.rb
+5
-1
activerecord/test/cases/relation/mutation_test.rb
activerecord/test/cases/relation/mutation_test.rb
+1
-1
activerecord/test/cases/relation_test.rb
activerecord/test/cases/relation_test.rb
+25
-25
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+5
-1
activerecord/test/models/post.rb
activerecord/test/models/post.rb
+8
-0
未找到文件。
activerecord/lib/active_record/association_relation.rb
浏览文件 @
e981adef
...
...
@@ -2,8 +2,8 @@
module
ActiveRecord
class
AssociationRelation
<
Relation
def
initialize
(
klass
,
table
,
predicate_builder
,
association
)
super
(
klass
,
table
,
predicate_builder
)
def
initialize
(
klass
,
association
)
super
(
klass
)
@association
=
association
end
...
...
activerecord/lib/active_record/associations/association.rb
浏览文件 @
e981adef
...
...
@@ -124,7 +124,7 @@ def klass
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
# through association's scope)
def
target_scope
AssociationRelation
.
create
(
klass
,
klass
.
arel_table
,
klass
.
predicate_builder
,
self
).
merge!
(
klass
.
all
)
AssociationRelation
.
create
(
klass
,
self
).
merge!
(
klass
.
all
)
end
def
extensions
...
...
activerecord/lib/active_record/associations/collection_proxy.rb
浏览文件 @
e981adef
...
...
@@ -32,7 +32,7 @@ module Associations
class
CollectionProxy
<
Relation
def
initialize
(
klass
,
association
)
#:nodoc:
@association
=
association
super
klass
,
klass
.
arel_table
,
klass
.
predicate_builder
super
klass
extensions
=
association
.
extensions
extend
(
*
extensions
)
if
extensions
.
any?
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
e981adef
...
...
@@ -281,7 +281,7 @@ def cached_find_by_statement(key, &block)
end
def
relation
relation
=
Relation
.
create
(
self
,
arel_table
,
predicate_builder
)
relation
=
Relation
.
create
(
self
)
if
finder_needs_type_condition?
&&
!
ignore_default_scope?
relation
.
where!
(
type_condition
)
...
...
activerecord/lib/active_record/reflection.rb
浏览文件 @
e981adef
...
...
@@ -291,7 +291,11 @@ def get_join_keys(association_klass)
end
def
build_scope
(
table
,
predicate_builder
=
predicate_builder
(
table
))
Relation
.
create
(
klass
,
table
,
predicate_builder
)
Relation
.
create
(
klass
,
table:
table
,
predicate_builder:
predicate_builder
)
end
def
join_primary_key
(
*
)
...
...
activerecord/lib/active_record/relation.rb
浏览文件 @
e981adef
...
...
@@ -22,7 +22,7 @@ class Relation
alias
:loaded?
:loaded
alias
:locked?
:lock_value
def
initialize
(
klass
,
table
,
predicate_builder
,
values
=
{})
def
initialize
(
klass
,
table
:
klass
.
arel_table
,
predicate_builder:
klass
.
predicate_builder
,
values:
{})
@klass
=
klass
@table
=
table
@values
=
values
...
...
activerecord/lib/active_record/relation/merger.rb
浏览文件 @
e981adef
...
...
@@ -23,7 +23,11 @@ def merge #:nodoc:
# build a relation to merge in rather than directly merging
# the values.
def
other
other
=
Relation
.
create
(
relation
.
klass
,
relation
.
table
,
relation
.
predicate_builder
)
other
=
Relation
.
create
(
relation
.
klass
,
table:
relation
.
table
,
predicate_builder:
relation
.
predicate_builder
)
hash
.
each
{
|
k
,
v
|
if
k
==
:joins
if
Hash
===
v
...
...
activerecord/lib/active_record/relation/spawn_methods.rb
浏览文件 @
e981adef
...
...
@@ -69,7 +69,7 @@ def only(*onlies)
private
def
relation_with
(
values
)
result
=
Relation
.
create
(
klass
,
table
,
predicate_builder
,
values
)
result
=
Relation
.
create
(
klass
,
values:
values
)
result
.
extend
(
*
extending_values
)
if
extending_values
.
any?
result
end
...
...
activerecord/test/cases/batches_test.rb
浏览文件 @
e981adef
...
...
@@ -592,7 +592,11 @@ def test_find_in_batches_should_return_a_sized_enumerator
table_metadata
=
ActiveRecord
::
TableMetadata
.
new
(
Post
,
table_alias
)
predicate_builder
=
ActiveRecord
::
PredicateBuilder
.
new
(
table_metadata
)
posts
=
ActiveRecord
::
Relation
.
create
(
Post
,
table_alias
,
predicate_builder
)
posts
=
ActiveRecord
::
Relation
.
create
(
Post
,
table:
table_alias
,
predicate_builder:
predicate_builder
)
posts
.
find_each
{}
end
end
...
...
activerecord/test/cases/collection_cache_key_test.rb
浏览文件 @
e981adef
...
...
@@ -60,7 +60,11 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
table_metadata
=
ActiveRecord
::
TableMetadata
.
new
(
Developer
,
table_alias
)
predicate_builder
=
ActiveRecord
::
PredicateBuilder
.
new
(
table_metadata
)
developers
=
ActiveRecord
::
Relation
.
create
(
Developer
,
table_alias
,
predicate_builder
)
developers
=
ActiveRecord
::
Relation
.
create
(
Developer
,
table:
table_alias
,
predicate_builder:
predicate_builder
)
developers
=
developers
.
where
(
salary:
100000
).
order
(
updated_at: :desc
)
last_developer_timestamp
=
developers
.
first
.
updated_at
...
...
activerecord/test/cases/relation/mutation_test.rb
浏览文件 @
e981adef
...
...
@@ -139,7 +139,7 @@ class RelationMutationTest < ActiveRecord::TestCase
private
def
relation
@relation
||=
Relation
.
new
(
FakeKlass
,
Post
.
arel_table
,
Post
.
predicate_builder
)
@relation
||=
Relation
.
new
(
FakeKlass
)
end
end
end
activerecord/test/cases/relation_test.rb
浏览文件 @
e981adef
...
...
@@ -11,19 +11,19 @@ class RelationTest < ActiveRecord::TestCase
fixtures
:posts
,
:comments
,
:authors
,
:author_addresses
,
:ratings
def
test_construction
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
,
table: :b
)
assert_equal
FakeKlass
,
relation
.
klass
assert_equal
:b
,
relation
.
table
assert
!
relation
.
loaded
,
"relation is not loaded"
end
def
test_responds_to_model_and_returns_klass
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
assert_equal
FakeKlass
,
relation
.
model
end
def
test_initialize_single_values
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
(
Relation
::
SINGLE_VALUE_METHODS
-
[
:create_with
]).
each
do
|
method
|
assert_nil
relation
.
send
(
"
#{
method
}
_value"
),
method
.
to_s
end
...
...
@@ -33,7 +33,7 @@ def test_initialize_single_values
end
def
test_multi_value_initialize
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
Relation
::
MULTI_VALUE_METHODS
.
each
do
|
method
|
values
=
relation
.
send
(
"
#{
method
}
_values"
)
assert_equal
[],
values
,
method
.
to_s
...
...
@@ -42,29 +42,29 @@ def test_multi_value_initialize
end
def
test_extensions
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
assert_equal
[],
relation
.
extensions
end
def
test_empty_where_values_hash
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
assert_equal
({},
relation
.
where_values_hash
)
end
def
test_has_values
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
)
relation
=
Relation
.
new
(
Post
)
relation
.
where!
(
id:
10
)
assert_equal
({
"id"
=>
10
},
relation
.
where_values_hash
)
end
def
test_values_wrong_table
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
)
relation
=
Relation
.
new
(
Post
)
relation
.
where!
Comment
.
arel_table
[
:id
].
eq
(
10
)
assert_equal
({},
relation
.
where_values_hash
)
end
def
test_tree_is_not_traversed
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
)
relation
=
Relation
.
new
(
Post
)
left
=
relation
.
table
[
:id
].
eq
(
10
)
right
=
relation
.
table
[
:id
].
eq
(
10
)
combine
=
left
.
or
(
right
)
...
...
@@ -73,18 +73,18 @@ def test_tree_is_not_traversed
end
def
test_scope_for_create
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
assert_equal
({},
relation
.
scope_for_create
)
end
def
test_create_with_value
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
)
relation
=
Relation
.
new
(
Post
)
relation
.
create_with_value
=
{
hello:
"world"
}
assert_equal
({
"hello"
=>
"world"
},
relation
.
scope_for_create
)
end
def
test_create_with_value_with_wheres
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
)
relation
=
Relation
.
new
(
Post
)
assert_equal
({},
relation
.
scope_for_create
)
relation
.
where!
(
id:
10
)
...
...
@@ -95,7 +95,7 @@ def test_create_with_value_with_wheres
end
def
test_empty_scope
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
)
relation
=
Relation
.
new
(
Post
)
assert
relation
.
empty_scope?
relation
.
merge!
(
relation
)
...
...
@@ -109,31 +109,31 @@ def test_bad_constants_raise_errors
end
def
test_empty_eager_loading?
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
assert
!
relation
.
eager_loading?
end
def
test_eager_load_values
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
relation
.
eager_load!
:b
assert
relation
.
eager_loading?
end
def
test_references_values
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
assert_equal
[],
relation
.
references_values
relation
=
relation
.
references
(
:foo
).
references
(
:omg
,
:lol
)
assert_equal
[
"foo"
,
"omg"
,
"lol"
],
relation
.
references_values
end
def
test_references_values_dont_duplicate
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
relation
=
relation
.
references
(
:foo
).
references
(
:foo
)
assert_equal
[
"foo"
],
relation
.
references_values
end
test
"merging a hash into a relation"
do
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
)
relation
=
Relation
.
new
(
Post
)
relation
=
relation
.
merge
where:
{
name: :lol
},
readonly:
true
assert_equal
({
"name"
=>
:lol
},
relation
.
where_clause
.
to_h
)
...
...
@@ -141,7 +141,7 @@ def test_references_values_dont_duplicate
end
test
"merging an empty hash into a relation"
do
assert_equal
Relation
::
WhereClause
.
empty
,
Relation
.
new
(
FakeKlass
,
:b
,
nil
).
merge
({}).
where_clause
assert_equal
Relation
::
WhereClause
.
empty
,
Relation
.
new
(
FakeKlass
).
merge
({}).
where_clause
end
test
"merging a hash with unknown keys raises"
do
...
...
@@ -149,7 +149,7 @@ def test_references_values_dont_duplicate
end
test
"merging nil or false raises"
do
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
e
=
assert_raises
(
ArgumentError
)
do
relation
=
relation
.
merge
nil
...
...
@@ -165,7 +165,7 @@ def test_references_values_dont_duplicate
end
test
"#values returns a dup of the values"
do
relation
=
Relation
.
new
(
Post
,
Post
.
arel_table
,
Post
.
predicate_builder
).
where!
(
name: :foo
)
relation
=
Relation
.
new
(
Post
).
where!
(
name: :foo
)
values
=
relation
.
values
values
[
:where
]
=
nil
...
...
@@ -173,7 +173,7 @@ def test_references_values_dont_duplicate
end
test
"relations can be created with a values hash"
do
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
,
select:
[
:foo
]
)
relation
=
Relation
.
new
(
FakeKlass
,
values:
{
select:
[
:foo
]
}
)
assert_equal
[
:foo
],
relation
.
select_values
end
...
...
@@ -185,13 +185,13 @@ def self.sanitize_sql(args)
end
end
relation
=
Relation
.
new
(
klass
,
:b
,
nil
)
relation
=
Relation
.
new
(
klass
)
relation
.
merge!
(
where:
[
"foo = ?"
,
"bar"
])
assert_equal
Relation
::
WhereClause
.
new
([
"foo = bar"
]),
relation
.
where_clause
end
def
test_merging_readonly_false
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
readonly_false_relation
=
relation
.
readonly
(
false
)
# test merging in both directions
assert_equal
false
,
relation
.
merge
(
readonly_false_relation
).
readonly_value
...
...
@@ -235,7 +235,7 @@ def test_relation_merging_with_joins_as_join_dependency_pick_proper_parent
def
test_merge_raises_with_invalid_argument
assert_raises
ArgumentError
do
relation
=
Relation
.
new
(
FakeKlass
,
:b
,
nil
)
relation
=
Relation
.
new
(
FakeKlass
)
relation
.
merge
(
true
)
end
end
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
e981adef
...
...
@@ -1934,6 +1934,10 @@ def custom_post_relation(alias_name = "omg_posts")
table_metadata
=
ActiveRecord
::
TableMetadata
.
new
(
Post
,
table_alias
)
predicate_builder
=
ActiveRecord
::
PredicateBuilder
.
new
(
table_metadata
)
ActiveRecord
::
Relation
.
create
(
Post
,
table_alias
,
predicate_builder
)
ActiveRecord
::
Relation
.
create
(
Post
,
table:
table_alias
,
predicate_builder:
predicate_builder
)
end
end
activerecord/test/models/post.rb
浏览文件 @
e981adef
...
...
@@ -326,5 +326,13 @@ def arel_attribute(name, table)
def
enforce_raw_sql_whitelist
(
*
args
)
# noop
end
def
arel_table
Post
.
arel_table
end
def
predicate_builder
Post
.
predicate_builder
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录