Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
45c6cab2
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,发现更多精彩内容 >>
提交
45c6cab2
编写于
12月 02, 2014
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #17888 from mrgilman/dot-notation
Refactor `build_from_hash` to convert dot notation to hash first
上级
ae54cb29
fcc3cbc7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
40 addition
and
29 deletion
+40
-29
activerecord/lib/active_record/relation/predicate_builder.rb
activerecord/lib/active_record/relation/predicate_builder.rb
+34
-29
activerecord/test/cases/finder_test.rb
activerecord/test/cases/finder_test.rb
+6
-0
未找到文件。
activerecord/lib/active_record/relation/predicate_builder.rb
浏览文件 @
45c6cab2
...
...
@@ -21,35 +21,8 @@ def resolve_column_aliases(hash)
end
def
build_from_hash
(
attributes
)
queries
=
[]
builder
=
self
attributes
.
each
do
|
column
,
value
|
if
value
.
is_a?
(
Hash
)
if
value
.
empty?
queries
<<
'1=0'
else
arel_table
=
Arel
::
Table
.
new
(
column
)
association
=
klass
.
_reflect_on_association
(
column
)
builder
=
self
.
class
.
new
(
association
&&
association
.
klass
,
arel_table
)
value
.
each
do
|
k
,
v
|
queries
.
concat
builder
.
expand
(
k
,
v
)
end
end
else
column
=
column
.
to_s
if
column
.
include?
(
'.'
)
table_name
,
column
=
column
.
split
(
'.'
,
2
)
arel_table
=
Arel
::
Table
.
new
(
table_name
)
builder
=
self
.
class
.
new
(
klass
,
arel_table
)
end
queries
.
concat
builder
.
expand
(
column
,
value
)
end
end
queries
attributes
=
convert_dot_notation_to_hash
(
attributes
.
stringify_keys
)
expand_from_hash
(
attributes
)
end
def
expand
(
column
,
value
)
...
...
@@ -130,5 +103,37 @@ def self.handler_for(object)
protected
attr_reader
:klass
,
:table
def
expand_from_hash
(
attributes
)
return
[
"1=0"
]
if
attributes
.
empty?
attributes
.
flat_map
do
|
key
,
value
|
if
value
.
is_a?
(
Hash
)
arel_table
=
Arel
::
Table
.
new
(
key
)
association
=
klass
.
_reflect_on_association
(
key
)
builder
=
self
.
class
.
new
(
association
&&
association
.
klass
,
arel_table
)
builder
.
expand_from_hash
(
value
)
else
expand
(
key
,
value
)
end
end
end
private
def
convert_dot_notation_to_hash
(
attributes
)
dot_notation
=
attributes
.
keys
.
select
{
|
s
|
s
.
include?
(
"."
)
}
dot_notation
.
each
do
|
key
|
table_name
,
column_name
=
key
.
split
(
"."
)
value
=
attributes
.
delete
(
key
)
attributes
[
table_name
]
||=
{}
attributes
[
table_name
]
=
attributes
[
table_name
].
merge
(
column_name
=>
value
)
end
attributes
end
end
end
activerecord/test/cases/finder_test.rb
浏览文件 @
45c6cab2
...
...
@@ -489,6 +489,12 @@ def test_find_on_hash_conditions_with_hashed_table_name
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
Topic
.
where
(
topics:
{
approved:
true
}).
find
(
1
)
}
end
def
test_find_on_combined_explicit_and_hashed_table_names
assert
Topic
.
where
(
'topics.approved'
=>
false
,
topics:
{
author_name:
"David"
}).
find
(
1
)
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
Topic
.
where
(
'topics.approved'
=>
true
,
topics:
{
author_name:
"David"
}).
find
(
1
)
}
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
Topic
.
where
(
'topics.approved'
=>
false
,
topics:
{
author_name:
"Melanie"
}).
find
(
1
)
}
end
def
test_find_with_hash_conditions_on_joined_table
firms
=
Firm
.
joins
(
:account
).
where
(
:accounts
=>
{
:credit_limit
=>
50
})
assert_equal
1
,
firms
.
size
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录