Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
75de1ce1
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,发现更多精彩内容 >>
提交
75de1ce1
编写于
1月 31, 2012
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4805 from xuanxu/none_and_null_object_pattern
Added `none` query method to return zero records.
上级
d6e41f36
8270e4a8
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
71 addition
and
1 deletion
+71
-1
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+13
-0
activerecord/lib/active_record.rb
activerecord/lib/active_record.rb
+1
-0
activerecord/lib/active_record/null_relation.rb
activerecord/lib/active_record/null_relation.rb
+10
-0
activerecord/lib/active_record/querying.rb
activerecord/lib/active_record/querying.rb
+1
-1
activerecord/lib/active_record/relation/query_methods.rb
activerecord/lib/active_record/relation/query_methods.rb
+33
-0
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+13
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
75de1ce1
## Rails 4.0.0 (unreleased) ##
*
Implemented ActiveRecord::Relation#none method
The `none` method returns a chainable relation with zero records
(an instance of the NullRelation class).
Any subsequent condition chained to the returned relation will continue
generating an empty relation and will not fire any query to the database.
*Juanjo Bazán*
*
Added the
`ActiveRecord::NullRelation`
class implementing the null
object pattern for the Relation class.
*Juanjo Bazán*
*
Added deprecation for the
`:dependent => :restrict`
association option.
Please note:
...
...
activerecord/lib/active_record.rb
浏览文件 @
75de1ce1
...
...
@@ -43,6 +43,7 @@ module ActiveRecord
autoload
:AutosaveAssociation
autoload
:Relation
autoload
:NullRelation
autoload_under
'relation'
do
autoload
:QueryMethods
...
...
activerecord/lib/active_record/null_relation.rb
0 → 100644
浏览文件 @
75de1ce1
# -*- coding: utf-8 -*-
module
ActiveRecord
# = Active Record Null Relation
class
NullRelation
<
Relation
def
exec_queries
@records
=
[]
end
end
end
\ No newline at end of file
activerecord/lib/active_record/querying.rb
浏览文件 @
75de1ce1
...
...
@@ -8,7 +8,7 @@ module Querying
delegate
:find_each
,
:find_in_batches
,
:to
=>
:scoped
delegate
:select
,
:group
,
:order
,
:except
,
:reorder
,
:limit
,
:offset
,
:joins
,
:where
,
:preload
,
:eager_load
,
:includes
,
:from
,
:lock
,
:readonly
,
:having
,
:create_with
,
:uniq
,
:references
,
:to
=>
:scoped
:having
,
:create_with
,
:uniq
,
:references
,
:
none
,
:
to
=>
:scoped
delegate
:count
,
:average
,
:minimum
,
:maximum
,
:sum
,
:calculate
,
:pluck
,
:to
=>
:scoped
# Executes a custom SQL query against your database and returns all the results. The results will
...
...
activerecord/lib/active_record/relation/query_methods.rb
浏览文件 @
75de1ce1
...
...
@@ -196,6 +196,39 @@ def lock(locks = true)
relation
end
# Returns a chainable relation with zero records, specifically an
# instance of the NullRelation class.
#
# The returned NullRelation inherits from Relation and implements the
# Null Object pattern so it is an object with defined null behavior:
# it always returns an empty array of records and avoids any database query.
#
# Any subsequent condition chained to the returned relation will continue
# generating an empty relation and will not fire any query to the database.
#
# Used in cases where is needed a method or a scope that could return zero
# results but the response has to be chainable.
#
# For example:
#
# @posts = current_user.visible_posts.where(:name => params[:name])
# # => the visible_post method response has to be a chainable Relation
#
# def visible_posts
# case role
# if 'Country Manager'
# Post.where(:country => country)
# if 'Reviewer'
# Post.published
# if 'Bad User'
# Post.none # => returning [] instead breaks the previous code
# end
# end
#
def
none
NullRelation
.
new
(
@klass
,
@table
)
end
def
readonly
(
value
=
true
)
relation
=
clone
relation
.
readonly_value
=
value
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
75de1ce1
...
...
@@ -215,6 +215,19 @@ def test_select_with_block
assert_equal
[
2
,
4
,
6
,
8
,
10
],
even_ids
.
sort
end
def
test_none
assert_no_queries
do
assert_equal
[],
Developer
.
none
assert_equal
[],
Developer
.
scoped
.
none
end
end
def
test_none_chainable
assert_no_queries
do
assert_equal
[],
Developer
.
none
.
where
(
:name
=>
'David'
)
end
end
def
test_joins_with_nil_argument
assert_nothing_raised
{
DependentFirm
.
joins
(
nil
).
first
}
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录