Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8d954538
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,发现更多精彩内容 >>
提交
8d954538
编写于
6月 26, 2010
作者:
S
Santiago Pastorino
提交者:
Jeremy Kemper
6月 26, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor: metaprogramming here it's confusing and make use of tap
Signed-off-by:
N
Jeremy Kemper
<
jeremy@bitsweat.net
>
上级
0abf4b07
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
73 addition
and
64 deletion
+73
-64
activerecord/lib/active_record/relation/query_methods.rb
activerecord/lib/active_record/relation/query_methods.rb
+73
-64
未找到文件。
activerecord/lib/active_record/relation/query_methods.rb
浏览文件 @
8d954538
...
@@ -5,26 +5,23 @@ module ActiveRecord
...
@@ -5,26 +5,23 @@ module ActiveRecord
module
QueryMethods
module
QueryMethods
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
included
do
attr_accessor
:includes_values
,
:eager_load_values
,
:preload_values
,
(
ActiveRecord
::
Relation
::
ASSOCIATION_METHODS
+
ActiveRecord
::
Relation
::
MULTI_VALUE_METHODS
).
each
do
|
query_method
|
:select_values
,
:group_values
,
:order_values
,
:joins_values
,
:where_values
,
:having_values
,
attr_accessor
:"
#{
query_method
}
_values"
:limit_value
,
:offset_value
,
:lock_value
,
:readonly_value
,
:create_with_value
,
:from_value
next
if
[
:where
,
:having
,
:select
,
:joins
].
include?
(
query_method
)
def
includes
(
*
args
)
class_eval
<<-
CEVAL
,
__FILE__
,
__LINE__
+
1
def
#{
query_method
}
(*args)
args
.
reject!
{
|
a
|
a
.
blank?
}
args
.
reject!
{
|
a
|
a
.
blank?
}
new_relation = clone
clone
.
tap
{
|
r
|
r
.
includes_values
+=
args
if
args
.
present?
}
new_relation.
#{
query_method
}
_values += args if args.present?
new_relation
end
end
CEVAL
def
eager_load
(
*
args
)
args
.
reject!
{
|
a
|
a
.
blank?
}
clone
.
tap
{
|
r
|
r
.
eager_load_values
+=
args
if
args
.
present?
}
end
end
def
reorder
(
*
args
)
def
preload
(
*
args
)
args
.
reject!
{
|
a
|
a
.
blank?
}
args
.
reject!
{
|
a
|
a
.
blank?
}
new_relation
=
clone
clone
.
tap
{
|
r
|
r
.
preload_values
+=
args
if
args
.
present?
}
new_relation
.
order_values
=
args
if
args
.
present?
new_relation
end
end
def
select
(
*
args
)
def
select
(
*
args
)
...
@@ -32,60 +29,72 @@ def select(*args)
...
@@ -32,60 +29,72 @@ def select(*args)
to_a
.
select
{
|*
block_args
|
yield
(
*
block_args
)
}
to_a
.
select
{
|*
block_args
|
yield
(
*
block_args
)
}
else
else
args
.
reject!
{
|
a
|
a
.
blank?
}
args
.
reject!
{
|
a
|
a
.
blank?
}
new_relation
=
clone
clone
.
tap
{
|
r
|
r
.
select_values
+=
args
if
args
.
present?
}
new_relation
.
select_values
+=
args
if
args
.
present?
end
new_relation
end
def
group
(
*
args
)
args
.
reject!
{
|
a
|
a
.
blank?
}
clone
.
tap
{
|
r
|
r
.
group_values
+=
args
if
args
.
present?
}
end
end
def
order
(
*
args
)
args
.
reject!
{
|
a
|
a
.
blank?
}
clone
.
tap
{
|
r
|
r
.
order_values
+=
args
if
args
.
present?
}
end
def
reorder
(
*
args
)
args
.
reject!
{
|
a
|
a
.
blank?
}
clone
.
tap
{
|
r
|
r
.
order_values
=
args
if
args
.
present?
}
end
end
def
joins
(
*
args
)
def
joins
(
*
args
)
args
.
flatten!
args
.
flatten!
args
.
reject!
{
|
a
|
a
.
blank?
}
args
.
reject!
{
|
a
|
a
.
blank?
}
new_relation
=
clone
clone
.
tap
{
|
r
|
r
.
joins_values
+=
args
if
args
.
present?
}
new_relation
.
joins_values
+=
args
if
args
.
present?
new_relation
end
end
[
:where
,
:having
].
each
do
|
query_method
|
def
where
(
*
args
)
class_eval
<<-
CEVAL
,
__FILE__
,
__LINE__
+
1
def
#{
query_method
}
(*args)
new_relation = clone
value
=
build_where
(
*
args
)
value
=
build_where
(
*
args
)
new_relation.
#{
query_method
}
_values += Array.wrap(value) if value.present?
clone
.
tap
{
|
r
|
r
.
where_values
+=
Array
.
wrap
(
value
)
if
value
.
present?
}
new_relation
end
end
CEVAL
def
having
(
*
args
)
value
=
build_where
(
*
args
)
clone
.
tap
{
|
r
|
r
.
having_values
+=
Array
.
wrap
(
value
)
if
value
.
present?
}
end
end
ActiveRecord
::
Relation
::
SINGLE_VALUE_METHODS
.
each
do
|
query_method
|
def
limit
(
value
=
true
)
attr_accessor
:"
#{
query_method
}
_value"
clone
.
tap
{
|
r
|
r
.
limit_value
=
value
}
end
class_eval
<<-
CEVAL
,
__FILE__
,
__LINE__
+
1
def
offset
(
value
=
true
)
def
#{
query_method
}
(value = true)
clone
.
tap
{
|
r
|
r
.
offset_value
=
value
}
new_relation = clone
new_relation.
#{
query_method
}
_value = value
new_relation
end
end
CEVAL
def
lock
(
locks
=
true
)
case
locks
when
String
,
TrueClass
,
NilClass
clone
.
tap
{
|
r
|
r
.
lock_value
=
locks
||
true
}
else
clone
.
tap
{
|
r
|
r
.
lock_value
=
false
}
end
end
end
end
def
extending
(
*
modules
)
def
readonly
(
value
=
true
)
new_relation
=
clone
clone
.
tap
{
|
r
|
r
.
readonly_value
=
value
}
new_relation
.
send
:apply_modules
,
*
modules
new_relation
end
end
def
lock
(
locks
=
true
,
&
block
)
def
create_with
(
value
=
true
)
relation
=
clone
clone
.
tap
{
|
r
|
r
.
create_with_value
=
value
}
relation
.
send
(
:apply_modules
,
Module
.
new
(
&
block
))
if
block_given?
end
case
locks
def
from
(
value
=
true
)
when
String
,
TrueClass
,
NilClass
clone
.
tap
{
|
r
|
r
.
from_value
=
value
}
clone
.
tap
{
|
new_relation
|
new_relation
.
lock_value
=
locks
||
true
}
else
clone
.
tap
{
|
new_relation
|
new_relation
.
lock_value
=
false
}
end
end
def
extending
(
*
modules
)
clone
.
tap
{
|
r
|
r
.
send
:apply_modules
,
*
modules
}
end
end
def
reverse_order
def
reverse_order
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录