Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
36bd52b4
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,发现更多精彩内容 >>
提交
36bd52b4
编写于
6月 18, 2014
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Don't use `Column` for type casting in Relation calculations
上级
dccf6da6
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
8 addition
and
17 deletion
+8
-17
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+8
-12
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+0
-5
未找到文件。
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
36bd52b4
...
...
@@ -273,7 +273,7 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
row
=
result
.
first
value
=
row
&&
row
.
values
.
first
column
=
result
.
column_types
.
fetch
(
column_alias
)
do
column
_for
(
column_name
)
type
_for
(
column_name
)
end
type_cast_calculated_value
(
value
,
column
,
operation
)
...
...
@@ -336,14 +336,14 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
Hash
[
calculated_data
.
map
do
|
row
|
key
=
group_columns
.
map
{
|
aliaz
,
col_name
|
column
=
calculated_data
.
column_types
.
fetch
(
aliaz
)
do
column
_for
(
col_name
)
type
_for
(
col_name
)
end
type_cast_calculated_value
(
row
[
aliaz
],
column
)
}
key
=
key
.
first
if
key
.
size
==
1
key
=
key_records
[
key
]
if
associated
column_type
=
calculated_data
.
column_types
.
fetch
(
aggregate_alias
)
{
column
_for
(
column_name
)
}
column_type
=
calculated_data
.
column_types
.
fetch
(
aggregate_alias
)
{
type
_for
(
column_name
)
}
[
key
,
type_cast_calculated_value
(
row
[
aggregate_alias
],
column_type
,
operation
)]
end
]
end
...
...
@@ -370,24 +370,20 @@ def column_alias_for(keys)
@klass
.
connection
.
table_alias_for
(
table_name
)
end
def
column
_for
(
field
)
def
type
_for
(
field
)
field_name
=
field
.
respond_to?
(
:name
)
?
field
.
name
.
to_s
:
field
.
to_s
.
split
(
'.'
).
last
@klass
.
columns_hash
[
field_name
]
@klass
.
type_for_attribute
(
field_name
)
end
def
type_cast_calculated_value
(
value
,
column
,
operation
=
nil
)
def
type_cast_calculated_value
(
value
,
type
,
operation
=
nil
)
case
operation
when
'count'
then
value
.
to_i
when
'sum'
then
type
_cast_using_column
(
value
||
0
,
column
)
when
'sum'
then
type
.
type_cast_from_database
(
value
||
0
)
when
'average'
then
value
.
respond_to?
(
:to_d
)
?
value
.
to_d
:
value
else
type
_cast_using_column
(
value
,
column
)
else
type
.
type_cast_from_database
(
value
)
end
end
def
type_cast_using_column
(
value
,
column
)
column
?
column
.
type_cast_from_database
(
value
)
:
value
end
# TODO: refactor to allow non-string `select_values` (eg. Arel nodes).
def
select_for_count
if
select_values
.
present?
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
36bd52b4
...
...
@@ -53,11 +53,6 @@ def test_should_return_nil_as_average
assert_nil
NumericData
.
average
(
:bank_balance
)
end
def
test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
assert_equal
0
,
NumericData
.
all
.
send
(
:type_cast_calculated_value
,
0
,
nil
,
'avg'
)
assert_equal
53.0
,
NumericData
.
all
.
send
(
:type_cast_calculated_value
,
53
,
nil
,
'avg'
)
end
def
test_should_get_maximum_of_field
assert_equal
60
,
Account
.
maximum
(
:credit_limit
)
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录