Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2c2792b6
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,发现更多精彩内容 >>
提交
2c2792b6
编写于
1月 28, 2014
作者:
G
Godfrey Chan
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #11821 from kuroda/select-and-order-by-aliased-attributes
Handle aliased attributes in AR::Relation#select, #order, etc.
上级
b9cd5a29
c1d99344
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
44 addition
and
1 deletion
+44
-1
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/relation/query_methods.rb
activerecord/lib/active_record/relation/query_methods.rb
+5
-1
activerecord/test/cases/relation/mutation_test.rb
activerecord/test/cases/relation/mutation_test.rb
+4
-0
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+31
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
2c2792b6
*
Handle aliased attributes
`select()`
,
`order()`
and
`reorder()`
.
*Tsutomu Kuroda*
*
Reset the collection association when calling
`reset`
on it.
Before:
...
...
activerecord/lib/active_record/relation/query_methods.rb
浏览文件 @
2c2792b6
...
...
@@ -234,7 +234,9 @@ def select(*fields)
def
select!
(
*
fields
)
# :nodoc:
fields
.
flatten!
fields
.
map!
do
|
field
|
klass
.
attribute_alias?
(
field
)
?
klass
.
attribute_alias
(
field
).
to_sym
:
field
end
self
.
select_values
+=
fields
self
end
...
...
@@ -1048,9 +1050,11 @@ def preprocess_order_args(order_args)
order_args
.
map!
do
|
arg
|
case
arg
when
Symbol
arg
=
klass
.
attribute_alias
(
arg
).
to_sym
if
klass
.
attribute_alias?
(
arg
)
table
[
arg
].
asc
when
Hash
arg
.
map
{
|
field
,
dir
|
field
=
klass
.
attribute_alias
(
field
).
to_sym
if
klass
.
attribute_alias?
(
field
)
table
[
field
].
send
(
dir
)
}
else
...
...
activerecord/test/cases/relation/mutation_test.rb
浏览文件 @
2c2792b6
...
...
@@ -14,6 +14,10 @@ def connection
def
relation_delegate_class
(
klass
)
self
.
class
.
relation_delegate_class
(
klass
)
end
def
attribute_alias?
(
name
)
false
end
end
def
relation
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
2c2792b6
...
...
@@ -206,12 +206,36 @@ def test_finding_with_order_concatenated
assert_equal
topics
(
:fourth
).
title
,
topics
.
first
.
title
end
def
test_finding_with_order_by_aliased_attributes
topics
=
Topic
.
order
(
:heading
)
assert_equal
5
,
topics
.
to_a
.
size
assert_equal
topics
(
:fifth
).
title
,
topics
.
first
.
title
end
def
test_finding_with_assoc_order_by_aliased_attributes
topics
=
Topic
.
order
(
heading: :desc
)
assert_equal
5
,
topics
.
to_a
.
size
assert_equal
topics
(
:third
).
title
,
topics
.
first
.
title
end
def
test_finding_with_reorder
topics
=
Topic
.
order
(
'author_name'
).
order
(
'title'
).
reorder
(
'id'
).
to_a
topics_titles
=
topics
.
map
{
|
t
|
t
.
title
}
assert_equal
[
'The First Topic'
,
'The Second Topic of the day'
,
'The Third Topic of the day'
,
'The Fourth Topic of the day'
,
'The Fifth Topic of the day'
],
topics_titles
end
def
test_finding_with_reorder_by_aliased_attributes
topics
=
Topic
.
order
(
'author_name'
).
reorder
(
:heading
)
assert_equal
5
,
topics
.
to_a
.
size
assert_equal
topics
(
:fifth
).
title
,
topics
.
first
.
title
end
def
test_finding_with_assoc_reorder_by_aliased_attributes
topics
=
Topic
.
order
(
'author_name'
).
reorder
(
heading: :desc
)
assert_equal
5
,
topics
.
to_a
.
size
assert_equal
topics
(
:third
).
title
,
topics
.
first
.
title
end
def
test_finding_with_order_and_take
entrants
=
Entrant
.
order
(
"id ASC"
).
limit
(
2
).
to_a
...
...
@@ -775,6 +799,13 @@ def test_select_takes_a_variable_list_of_args
assert_equal
david
.
salary
,
developer
.
salary
end
def
test_select_takes_an_aliased_attribute
first
=
topics
(
:first
)
topic
=
Topic
.
where
(
id:
first
.
id
).
select
(
:heading
).
first
assert_equal
first
.
heading
,
topic
.
heading
end
def
test_select_argument_error
assert_raises
(
ArgumentError
)
{
Developer
.
select
}
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录