Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fe6ffce5
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,发现更多精彩内容 >>
提交
fe6ffce5
编写于
1月 24, 2009
作者:
P
Pratik Naik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make sure inner scope conditions get a preference over the outer ones
上级
b2192888
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
43 addition
and
3 deletion
+43
-3
actionpack/test/fixtures/replies.yml
actionpack/test/fixtures/replies.yml
+9
-1
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+6
-2
activerecord/test/cases/named_scope_test.rb
activerecord/test/cases/named_scope_test.rb
+20
-0
activerecord/test/models/post.rb
activerecord/test/models/post.rb
+6
-0
activerecord/test/models/topic.rb
activerecord/test/models/topic.rb
+2
-0
未找到文件。
actionpack/test/fixtures/replies.yml
浏览文件 @
fe6ffce5
...
...
@@ -12,4 +12,12 @@ another:
developer_id
:
1
content
:
Nuh uh!
created_at
:
<%= 1.hour.ago.to_s(:db) %>
updated_at
:
nil
\ No newline at end of file
updated_at
:
nil
best_reply
:
id
:
3
topic_id
:
3
developer_id
:
2
content
:
No one can know
created_at
:
<%= 5.hours.ago.to_s(:db) %>
updated_at
:
nil
activerecord/lib/active_record/base.rb
浏览文件 @
fe6ffce5
...
...
@@ -2097,7 +2097,11 @@ def with_scope(method_scoping = {}, action = :merge, &block)
(
hash
[
method
].
keys
+
params
.
keys
).
uniq
.
each
do
|
key
|
merge
=
hash
[
method
][
key
]
&&
params
[
key
]
# merge if both scopes have the same key
if
key
==
:conditions
&&
merge
hash
[
method
][
key
]
=
merge_conditions
(
params
[
key
],
hash
[
method
][
key
])
if
params
[
key
].
is_a?
(
Hash
)
&&
hash
[
method
][
key
].
is_a?
(
Hash
)
hash
[
method
][
key
]
=
merge_conditions
(
hash
[
method
][
key
].
deep_merge
(
params
[
key
]))
else
hash
[
method
][
key
]
=
merge_conditions
(
params
[
key
],
hash
[
method
][
key
])
end
elsif
key
==
:include
&&
merge
hash
[
method
][
key
]
=
merge_includes
(
hash
[
method
][
key
],
params
[
key
]).
uniq
elsif
key
==
:joins
&&
merge
...
...
@@ -2107,7 +2111,7 @@ def with_scope(method_scoping = {}, action = :merge, &block)
end
end
else
hash
[
method
]
=
params
.
merge
(
hash
[
method
]
)
hash
[
method
]
=
hash
[
method
].
merge
(
params
)
end
else
hash
[
method
]
=
params
...
...
activerecord/test/cases/named_scope_test.rb
浏览文件 @
fe6ffce5
...
...
@@ -277,6 +277,26 @@ def test_chaining_with_duplicate_joins
post
=
Post
.
find
(
1
)
assert_equal
post
.
comments
.
size
,
Post
.
scoped
(
:joins
=>
join
).
scoped
(
:joins
=>
join
,
:conditions
=>
"posts.id =
#{
post
.
id
}
"
).
size
end
def
test_chanining_should_use_latest_conditions_when_creating
post1
=
Topic
.
rejected
.
approved
.
new
assert
post1
.
approved?
post2
=
Topic
.
approved
.
rejected
.
new
assert
!
post2
.
approved?
end
def
test_chanining_should_use_latest_conditions_when_searching
# Normal hash conditions
assert_equal
Topic
.
all
(
:conditions
=>
{
:approved
=>
true
}),
Topic
.
rejected
.
approved
.
all
assert_equal
Topic
.
all
(
:conditions
=>
{
:approved
=>
false
}),
Topic
.
approved
.
rejected
.
all
# Nested hash conditions with same keys
assert_equal
[
posts
(
:sti_comments
)],
Post
.
with_special_comments
.
with_very_special_comments
.
all
# Nested hash conditions with different keys
assert_equal
[
posts
(
:sti_comments
)],
Post
.
with_special_comments
.
with_post
(
4
).
all
.
uniq
end
end
class
DynamicScopeMatchTest
<
ActiveRecord
::
TestCase
...
...
activerecord/test/models/post.rb
浏览文件 @
fe6ffce5
...
...
@@ -17,6 +17,12 @@ def greeting
has_one
:last_comment
,
:class_name
=>
'Comment'
,
:order
=>
'id desc'
named_scope
:with_special_comments
,
:joins
=>
:comments
,
:conditions
=>
{
:comments
=>
{
:type
=>
'SpecialComment'
}
}
named_scope
:with_very_special_comments
,
:joins
=>
:comments
,
:conditions
=>
{
:comments
=>
{
:type
=>
'VerySpecialComment'
}
}
named_scope
:with_post
,
lambda
{
|
post_id
|
{
:joins
=>
:comments
,
:conditions
=>
{
:comments
=>
{
:post_id
=>
post_id
}
}
}
}
has_many
:comments
,
:order
=>
"body"
do
def
find_most_recent
find
(
:first
,
:order
=>
"id DESC"
)
...
...
activerecord/test/models/topic.rb
浏览文件 @
fe6ffce5
...
...
@@ -4,6 +4,8 @@ class Topic < ActiveRecord::Base
{
:conditions
=>
[
'written_on < ?'
,
time
]
}
}
named_scope
:approved
,
:conditions
=>
{
:approved
=>
true
}
named_scope
:rejected
,
:conditions
=>
{
:approved
=>
false
}
named_scope
:by_lifo
,
:conditions
=>
{
:author_name
=>
'lifo'
}
named_scope
:approved_as_hash_condition
,
:conditions
=>
{
:topics
=>
{
:approved
=>
true
}}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录