Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b2967999
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,体验更适合开发者的 AI 搜索 >>
提交
b2967999
编写于
1月 17, 2015
作者:
J
Jay Elaraj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ensure `method_missing` called for non-existing methods passed to
`ActiveModel::Serialization#serializable_hash`
上级
8979a5a4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
22 addition
and
10 deletion
+22
-10
activemodel/CHANGELOG.md
activemodel/CHANGELOG.md
+5
-0
activemodel/lib/active_model/serialization.rb
activemodel/lib/active_model/serialization.rb
+1
-1
activemodel/test/cases/serialization_test.rb
activemodel/test/cases/serialization_test.rb
+16
-9
未找到文件。
activemodel/CHANGELOG.md
浏览文件 @
b2967999
*
Ensure
`method_missing`
is called for methods passed to
`ActiveModel::Serialization#serializable_hash`
that don't exist.
*Jay Elaraj*
*
Add
`ActiveModel::Dirty#[attr_name]_previously_changed?`
and
`ActiveModel::Dirty#[attr_name]_previous_change`
to improve access
to recorded changes after the model has been saved.
...
...
activemodel/lib/active_model/serialization.rb
浏览文件 @
b2967999
...
...
@@ -107,7 +107,7 @@ def serializable_hash(options = nil)
hash
=
{}
attribute_names
.
each
{
|
n
|
hash
[
n
]
=
read_attribute_for_serialization
(
n
)
}
Array
(
options
[
:methods
]).
each
{
|
m
|
hash
[
m
.
to_s
]
=
send
(
m
)
if
respond_to?
(
m
)
}
Array
(
options
[
:methods
]).
each
{
|
m
|
hash
[
m
.
to_s
]
=
send
(
m
)
}
serializable_add_includes
(
options
)
do
|
association
,
records
,
opts
|
hash
[
association
.
to_s
]
=
if
records
.
respond_to?
(
:to_ary
)
...
...
activemodel/test/cases/serialization_test.rb
浏览文件 @
b2967999
...
...
@@ -16,6 +16,14 @@ def attributes
instance_values
.
except
(
"address"
,
"friends"
)
end
def
method_missing
(
method_name
,
*
args
)
if
method_name
==
:bar
'i_am_bar'
else
super
end
end
def
foo
'i_am_foo'
end
...
...
@@ -58,23 +66,22 @@ def test_method_serializable_hash_should_work_with_except_option
end
def
test_method_serializable_hash_should_work_with_methods_option
expected
=
{
"name"
=>
"David"
,
"gender"
=>
"male"
,
"foo"
=>
"i_am_foo"
,
"email"
=>
"david@example.com"
}
assert_equal
expected
,
@user
.
serializable_hash
(
methods:
[
:foo
])
expected
=
{
"name"
=>
"David"
,
"gender"
=>
"male"
,
"foo"
=>
"i_am_foo"
,
"
bar"
=>
"i_am_bar"
,
"
email"
=>
"david@example.com"
}
assert_equal
expected
,
@user
.
serializable_hash
(
methods:
[
:foo
,
:bar
])
end
def
test_method_serializable_hash_should_work_with_only_and_methods
expected
=
{
"foo"
=>
"i_am_foo"
}
assert_equal
expected
,
@user
.
serializable_hash
(
only:
[],
methods:
[
:foo
])
expected
=
{
"foo"
=>
"i_am_foo"
,
"bar"
=>
"i_am_bar"
}
assert_equal
expected
,
@user
.
serializable_hash
(
only:
[],
methods:
[
:foo
,
:bar
])
end
def
test_method_serializable_hash_should_work_with_except_and_methods
expected
=
{
"gender"
=>
"male"
,
"foo"
=>
"i_am_foo"
}
assert_equal
expected
,
@user
.
serializable_hash
(
except:
[
:name
,
:email
],
methods:
[
:foo
])
expected
=
{
"gender"
=>
"male"
,
"foo"
=>
"i_am_foo"
,
"bar"
=>
"i_am_bar"
}
assert_equal
expected
,
@user
.
serializable_hash
(
except:
[
:name
,
:email
],
methods:
[
:foo
,
:bar
])
end
def
test_should_not_call_methods_that_dont_respond
expected
=
{
"name"
=>
"David"
,
"gender"
=>
"male"
,
"email"
=>
"david@example.com"
}
assert_equal
expected
,
@user
.
serializable_hash
(
methods:
[
:bar
])
def
test_should_raise_NoMethodError_for_non_existing_method
assert_raise
(
NoMethodError
)
{
@user
.
serializable_hash
(
methods:
[
:nada
])
}
end
def
test_should_use_read_attribute_for_serialization
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录