Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
0dd2f96f
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,发现更多精彩内容 >>
提交
0dd2f96f
编写于
2月 13, 2009
作者:
D
Diego Algorta
提交者:
rick
2月 25, 2009
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixed bug that makes named_scopes _forgot_ current scope
上级
92a30b0c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
26 addition
and
2 deletion
+26
-2
activerecord/lib/active_record/named_scope.rb
activerecord/lib/active_record/named_scope.rb
+11
-2
activerecord/test/cases/named_scope_test.rb
activerecord/test/cases/named_scope_test.rb
+9
-0
activerecord/test/models/post.rb
activerecord/test/models/post.rb
+6
-0
未找到文件。
activerecord/lib/active_record/named_scope.rb
浏览文件 @
0dd2f96f
...
...
@@ -100,7 +100,7 @@ def named_scope(name, options = {}, &block)
end
class
Scope
attr_reader
:proxy_scope
,
:proxy_options
attr_reader
:proxy_scope
,
:proxy_options
,
:current_scoped_methods_when_defined
NON_DELEGATE_METHODS
=
%w(nil? send object_id class extend find size count sum average maximum minimum paginate first last empty? any? respond_to?)
.
to_set
[].
methods
.
each
do
|
m
|
unless
m
=~
/^__/
||
NON_DELEGATE_METHODS
.
include?
(
m
.
to_s
)
...
...
@@ -113,6 +113,9 @@ class Scope
def
initialize
(
proxy_scope
,
options
,
&
block
)
[
options
[
:extend
]].
flatten
.
each
{
|
extension
|
extend
extension
}
if
options
[
:extend
]
extend
Module
.
new
(
&
block
)
if
block_given?
unless
Scope
===
proxy_scope
@current_scoped_methods_when_defined
=
proxy_scope
.
send
(
:current_scoped_methods
)
end
@proxy_scope
,
@proxy_options
=
proxy_scope
,
options
.
except
(
:extend
)
end
...
...
@@ -168,7 +171,13 @@ def method_missing(method, *args, &block)
else
with_scope
:find
=>
proxy_options
,
:create
=>
proxy_options
[
:conditions
].
is_a?
(
Hash
)
?
proxy_options
[
:conditions
]
:
{}
do
method
=
:new
if
method
==
:build
proxy_scope
.
send
(
method
,
*
args
,
&
block
)
if
current_scoped_methods_when_defined
with_scope
current_scoped_methods_when_defined
do
proxy_scope
.
send
(
method
,
*
args
,
&
block
)
end
else
proxy_scope
.
send
(
method
,
*
args
,
&
block
)
end
end
end
end
...
...
activerecord/test/cases/named_scope_test.rb
浏览文件 @
0dd2f96f
...
...
@@ -142,6 +142,15 @@ def test_has_many_through_associations_have_access_to_named_scopes
assert_equal
authors
(
:david
).
comments
&
Comment
.
containing_the_letter_e
,
authors
(
:david
).
comments
.
containing_the_letter_e
end
def
test_named_scopes_honor_current_scopes_from_when_defined
assert
!
Post
.
ranked_by_comments
.
limit
(
5
).
empty?
assert
!
authors
(
:david
).
posts
.
ranked_by_comments
.
limit
(
5
).
empty?
assert_not_equal
Post
.
ranked_by_comments
.
limit
(
5
),
authors
(
:david
).
posts
.
ranked_by_comments
.
limit
(
5
)
assert_not_equal
Post
.
top
(
5
),
authors
(
:david
).
posts
.
top
(
5
)
assert_equal
authors
(
:david
).
posts
.
ranked_by_comments
.
limit
(
5
),
authors
(
:david
).
posts
.
top
(
5
)
assert_equal
Post
.
ranked_by_comments
.
limit
(
5
),
Post
.
top
(
5
)
end
def
test_active_records_have_scope_named__all__
assert
!
Topic
.
find
(
:all
).
empty?
...
...
activerecord/test/models/post.rb
浏览文件 @
0dd2f96f
class
Post
<
ActiveRecord
::
Base
named_scope
:containing_the_letter_a
,
:conditions
=>
"body LIKE '%a%'"
named_scope
:ranked_by_comments
,
:order
=>
"comments_count DESC"
named_scope
:limit
,
lambda
{
|
limit
|
{
:limit
=>
limit
}
}
named_scope
:with_authors_at_address
,
lambda
{
|
address
|
{
:conditions
=>
[
'authors.author_address_id = ?'
,
address
.
id
],
:joins
=>
'JOIN authors ON authors.id = posts.author_id'
...
...
@@ -68,6 +70,10 @@ def add_joins_and_select
:before_remove
=>
lambda
{
|
owner
,
reader
|
log
(
:removed
,
:before
,
reader
.
first_name
)
},
:after_remove
=>
lambda
{
|
owner
,
reader
|
log
(
:removed
,
:after
,
reader
.
first_name
)
}
def
self
.
top
(
limit
)
ranked_by_comments
.
limit
(
limit
)
end
def
self
.
reset_log
@log
=
[]
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录