Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
38d05cee
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,发现更多精彩内容 >>
提交
38d05cee
编写于
2月 26, 2017
作者:
A
Andrew White
提交者:
GitHub
2月 26, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #28177 from kami-zh/remove-duplicated-private
Remove duplicated private method in ActiveRecord::FinderMethods
上级
691a39b2
0737dc8d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
100 addition
and
102 deletion
+100
-102
activerecord/lib/active_record/relation/finder_methods.rb
activerecord/lib/active_record/relation/finder_methods.rb
+100
-102
未找到文件。
activerecord/lib/active_record/relation/finder_methods.rb
浏览文件 @
38d05cee
...
...
@@ -430,140 +430,138 @@ def using_limitable_reflections?(reflections)
reflections
.
none?
(
&
:collection?
)
end
private
def
find_with_ids
(
*
ids
)
raise
UnknownPrimaryKey
.
new
(
@klass
)
if
primary_key
.
nil?
def
find_with_ids
(
*
ids
)
raise
UnknownPrimaryKey
.
new
(
@klass
)
if
primary_key
.
nil
?
expects_array
=
ids
.
first
.
kind_of?
(
Array
)
return
ids
.
first
if
expects_array
&&
ids
.
first
.
empty
?
expects_array
=
ids
.
first
.
kind_of?
(
Array
)
return
ids
.
first
if
expects_array
&&
ids
.
first
.
empty?
ids
=
ids
.
flatten
.
compact
.
uniq
ids
=
ids
.
flatten
.
compact
.
uniq
case
ids
.
size
when
0
raise
RecordNotFound
,
"Couldn't find
#{
@klass
.
name
}
without an ID"
when
1
result
=
find_one
(
ids
.
first
)
expects_array
?
[
result
]
:
result
else
find_some
(
ids
)
end
rescue
::
RangeError
raise
RecordNotFound
,
"Couldn't find
#{
@klass
.
name
}
with an out of range ID"
case
ids
.
size
when
0
raise
RecordNotFound
,
"Couldn't find
#{
@klass
.
name
}
without an ID"
when
1
result
=
find_one
(
ids
.
first
)
expects_array
?
[
result
]
:
result
else
find_some
(
ids
)
end
rescue
::
RangeError
raise
RecordNotFound
,
"Couldn't find
#{
@klass
.
name
}
with an out of range ID"
end
def
find_one
(
id
)
if
ActiveRecord
::
Base
===
id
raise
ArgumentError
,
<<-
MSG
.
squish
You are passing an instance of ActiveRecord::Base to `find`.
Please pass the id of the object by calling `.id`.
MSG
end
relation
=
where
(
primary_key
=>
id
)
record
=
relation
.
take
raise_record_not_found_exception!
(
id
,
0
,
1
)
unless
record
record
def
find_one
(
id
)
if
ActiveRecord
::
Base
===
id
raise
ArgumentError
,
<<-
MSG
.
squish
You are passing an instance of ActiveRecord::Base to `find`.
Please pass the id of the object by calling `.id`.
MSG
end
def
find_some
(
ids
)
return
find_some_ordered
(
ids
)
unless
order_values
.
present?
relation
=
where
(
primary_key
=>
id
)
record
=
relation
.
take
raise_record_not_found_exception!
(
id
,
0
,
1
)
unless
record
result
=
where
(
primary_key
=>
ids
).
to_a
record
end
expected_size
=
if
limit_value
&&
ids
.
size
>
limit_value
limit_value
else
ids
.
size
end
def
find_some
(
ids
)
return
find_some_ordered
(
ids
)
unless
order_values
.
present?
# 11 ids with limit 3, offset 9 should give 2 results.
if
offset_value
&&
(
ids
.
size
-
offset_value
<
expected_size
)
expected_size
=
ids
.
size
-
offset_value
end
result
=
where
(
primary_key
=>
ids
).
to_a
if
result
.
size
==
expected_size
result
expected_size
=
if
limit_value
&&
ids
.
size
>
limit_value
limit_value
else
raise_record_not_found_exception!
(
ids
,
result
.
size
,
expected_size
)
ids
.
size
end
# 11 ids with limit 3, offset 9 should give 2 results.
if
offset_value
&&
(
ids
.
size
-
offset_value
<
expected_size
)
expected_size
=
ids
.
size
-
offset_value
end
def
find_some_ordered
(
ids
)
ids
=
ids
.
slice
(
offset_value
||
0
,
limit_value
||
ids
.
size
)
||
[]
if
result
.
size
==
expected_size
result
else
raise_record_not_found_exception!
(
ids
,
result
.
size
,
expected_size
)
end
end
result
=
except
(
:limit
,
:offset
).
where
(
primary_key
=>
ids
).
records
def
find_some_ordered
(
ids
)
ids
=
ids
.
slice
(
offset_value
||
0
,
limit_value
||
ids
.
size
)
||
[]
if
result
.
size
==
ids
.
size
pk_type
=
@klass
.
type_for_attribute
(
primary_key
)
result
=
except
(
:limit
,
:offset
).
where
(
primary_key
=>
ids
).
records
records_by_id
=
result
.
index_by
(
&
:id
)
ids
.
map
{
|
id
|
records_by_id
.
fetch
(
pk_type
.
cast
(
id
))
}
else
raise_record_not_found_exception!
(
ids
,
result
.
size
,
ids
.
size
)
end
end
if
result
.
size
==
ids
.
size
pk_type
=
@klass
.
type_for_attribute
(
primary_key
)
def
find_take
if
loaded?
records
.
first
else
@take
||=
limit
(
1
).
records
.
first
end
records_by_id
=
result
.
index_by
(
&
:id
)
ids
.
map
{
|
id
|
records_by_id
.
fetch
(
pk_type
.
cast
(
id
))
}
else
raise_record_not_found_exception!
(
ids
,
result
.
size
,
ids
.
size
)
end
end
def
find_take_with_limit
(
limit
)
if
loaded?
records
.
take
(
limit
)
else
limit
(
limit
).
to_a
end
def
find_take
if
loaded?
records
.
first
else
@take
||=
limit
(
1
).
records
.
first
end
end
def
find_nth
(
index
)
@offsets
[
offset_index
+
index
]
||=
find_nth_with_limit
(
index
,
1
).
first
def
find_take_with_limit
(
limit
)
if
loaded?
records
.
take
(
limit
)
else
limit
(
limit
).
to_a
end
end
def
find_nth_with_limit
(
index
,
limit
)
if
loaded?
records
[
index
,
limit
]
||
[]
def
find_nth
(
index
)
@offsets
[
offset_index
+
index
]
||=
find_nth_with_limit
(
index
,
1
).
first
end
def
find_nth_with_limit
(
index
,
limit
)
if
loaded?
records
[
index
,
limit
]
||
[]
else
relation
=
if
order_values
.
empty?
&&
primary_key
order
(
arel_attribute
(
primary_key
).
asc
)
else
relation
=
if
order_values
.
empty?
&&
primary_key
order
(
arel_attribute
(
primary_key
).
asc
)
else
self
end
relation
=
relation
.
offset
(
offset_index
+
index
)
unless
index
.
zero?
relation
.
limit
(
limit
).
to_a
self
end
relation
=
relation
.
offset
(
offset_index
+
index
)
unless
index
.
zero?
relation
.
limit
(
limit
).
to_a
end
end
def
find_nth_from_last
(
index
)
if
loaded?
records
[
-
index
]
def
find_nth_from_last
(
index
)
if
loaded?
records
[
-
index
]
else
relation
=
if
order_values
.
empty?
&&
primary_key
order
(
arel_attribute
(
primary_key
).
asc
)
else
relation
=
if
order_values
.
empty?
&&
primary_key
order
(
arel_attribute
(
primary_key
).
asc
)
else
self
end
relation
.
to_a
[
-
index
]
# TODO: can be made more performant on large result sets by
# for instance, last(index)[-index] (which would require
# refactoring the last(n) finder method to make test suite pass),
# or by using a combination of reverse_order, limit, and offset,
# e.g., reverse_order.offset(index-1).first
self
end
end
def
find_last
(
limit
)
limit
?
records
.
last
(
limit
)
:
records
.
last
relation
.
to_a
[
-
index
]
# TODO: can be made more performant on large result sets by
# for instance, last(index)[-index] (which would require
# refactoring the last(n) finder method to make test suite pass),
# or by using a combination of reverse_order, limit, and offset,
# e.g., reverse_order.offset(index-1).first
end
end
def
find_last
(
limit
)
limit
?
records
.
last
(
limit
)
:
records
.
last
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录