Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
7cd1d37a
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,发现更多精彩内容 >>
提交
7cd1d37a
编写于
4月 26, 2010
作者:
S
Santiago Pastorino
提交者:
Jeremy Kemper
4月 26, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reuse Active Model serialization in Active Resource. [#2584 state:committed]
Signed-off-by:
N
Jeremy Kemper
<
jeremy@bitsweat.net
>
上级
76d6a993
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
25 addition
and
69 deletion
+25
-69
activeresource/lib/active_resource/base.rb
activeresource/lib/active_resource/base.rb
+3
-63
activeresource/test/cases/base_test.rb
activeresource/test/cases/base_test.rb
+22
-6
未找到文件。
activeresource/lib/active_resource/base.rb
浏览文件 @
7cd1d37a
...
...
@@ -1176,73 +1176,11 @@ def exists?
!
new?
&&
self
.
class
.
exists?
(
to_param
,
:params
=>
prefix_options
)
end
# Converts the resource to an XML string representation.
#
# ==== Options
# The +options+ parameter is handed off to the +to_xml+ method on each
# attribute, so it has the same options as the +to_xml+ methods in
# Active Support.
#
# * <tt>:indent</tt> - Set the indent level for the XML output (default is +2+).
# * <tt>:dasherize</tt> - Boolean option to determine whether or not element names should
# replace underscores with dashes (default is <tt>false</tt>).
# * <tt>:skip_instruct</tt> - Toggle skipping the +instruct!+ call on the XML builder
# that generates the XML declaration (default is <tt>false</tt>).
#
# ==== Examples
# my_group = SubsidiaryGroup.find(:first)
# my_group.to_xml
# # => <?xml version="1.0" encoding="UTF-8"?>
# # <subsidiary_group> [...] </subsidiary_group>
#
# my_group.to_xml(:dasherize => true)
# # => <?xml version="1.0" encoding="UTF-8"?>
# # <subsidiary-group> [...] </subsidiary-group>
#
# my_group.to_xml(:skip_instruct => true)
# # => <subsidiary_group> [...] </subsidiary_group>
def
to_xml
(
options
=
{})
attributes
.
to_xml
({
:root
=>
self
.
class
.
element_name
}.
merge
(
options
))
end
# Coerces to a hash for JSON encoding.
#
# ==== Options
# The +options+ are passed to the +to_json+ method on each
# attribute, so the same options as the +to_json+ methods in
# Active Support.
#
# * <tt>:only</tt> - Only include the specified attribute or list of
# attributes in the serialized output. Attribute names must be specified
# as strings.
# * <tt>:except</tt> - Do not include the specified attribute or list of
# attributes in the serialized output. Attribute names must be specified
# as strings.
#
# ==== Examples
# person = Person.new(:first_name => "Jim", :last_name => "Smith")
# person.to_json
# # => {"first_name": "Jim", "last_name": "Smith"}
#
# person.to_json(:only => ["first_name"])
# # => {"first_name": "Jim"}
#
# person.to_json(:except => ["first_name"])
# # => {"last_name": "Smith"}
def
as_json
(
options
=
nil
)
attributes
.
as_json
(
options
)
end
# Returns the serialized string representation of the resource in the configured
# serialization format specified in ActiveResource::Base.format. The options
# applicable depend on the configured encoding format.
def
encode
(
options
=
{})
case
self
.
class
.
format
when
ActiveResource
::
Formats
::
XmlFormat
self
.
class
.
format
.
encode
(
attributes
,
{
:root
=>
self
.
class
.
element_name
}.
merge
(
options
))
else
self
.
class
.
format
.
encode
(
attributes
,
options
)
end
send
(
"to_
#{
self
.
class
.
format
.
extension
}
"
,
options
)
end
# A method to \reload the attributes of this object from the remote web service.
...
...
@@ -1466,5 +1404,7 @@ def method_missing(method_symbol, *arguments) #:nodoc:
class
Base
extend
ActiveModel
::
Naming
include
CustomMethods
,
Observing
,
Validations
include
ActiveModel
::
Serializers
::
JSON
include
ActiveModel
::
Serializers
::
Xml
end
end
activeresource/test/cases/base_test.rb
浏览文件 @
7cd1d37a
...
...
@@ -4,20 +4,22 @@
require
"fixtures/street_address"
require
"fixtures/beast"
require
"fixtures/proxy"
require
'active_support/json'
require
'active_support/core_ext/hash/conversions'
require
'mocha'
class
BaseTest
<
Test
::
Unit
::
TestCase
def
setup
@matz
=
{
:id
=>
1
,
:name
=>
'Matz'
}.
to_xml
(
:root
=>
'person'
)
@david
=
{
:id
=>
2
,
:name
=>
'David'
}.
to_xml
(
:root
=>
'person'
)
@greg
=
{
:id
=>
3
,
:name
=>
'Greg'
}.
to_xml
(
:root
=>
'person'
)
@addy
=
{
:id
=>
1
,
:street
=>
'12345 Street'
,
:country
=>
'Australia'
}.
to_xml
(
:root
=>
'address'
)
@default_request_headers
=
{
'Content-Type'
=>
'application/xml'
}
@rick
=
{
:name
=>
"Rick"
,
:age
=>
25
}.
to_xml
(
:root
=>
"person"
)
@matz
=
{
:id
=>
1
,
:name
=>
'Matz'
}.
to_xml
(
:root
=>
'person'
)
@david
=
{
:id
=>
2
,
:name
=>
'David'
}.
to_xml
(
:root
=>
'person'
)
@greg
=
{
:id
=>
3
,
:name
=>
'Greg'
}.
to_xml
(
:root
=>
'person'
)
@addy
=
{
:id
=>
1
,
:street
=>
'12345 Street'
,
:country
=>
'Australia'
}.
to_xml
(
:root
=>
'address'
)
@rick
=
{
:name
=>
"Rick"
,
:age
=>
25
}.
to_xml
(
:root
=>
"person"
)
@joe
=
{
'person'
=>
{
:id
=>
6
,
:name
=>
'Joe'
}}.
to_json
@people
=
[{
:id
=>
1
,
:name
=>
'Matz'
},
{
:id
=>
2
,
:name
=>
'David'
}].
to_xml
(
:root
=>
'people'
)
@people_david
=
[{
:id
=>
2
,
:name
=>
'David'
}].
to_xml
(
:root
=>
'people'
)
@addresses
=
[{
:id
=>
1
,
:street
=>
'12345 Street'
,
:country
=>
'Australia'
}].
to_xml
(
:root
=>
'addresses'
)
@addresses
=
[{
:id
=>
1
,
:street
=>
'12345 Street'
,
:country
=>
'Australia'
}].
to_xml
(
:root
=>
'addresses'
)
# - deep nested resource -
# - Luis (Customer)
...
...
@@ -66,6 +68,7 @@ def setup
ActiveResource
::
HttpMock
.
respond_to
do
|
mock
|
mock
.
get
"/people/1.xml"
,
{},
@matz
mock
.
get
"/people/2.xml"
,
{},
@david
mock
.
get
"/people/6.json"
,
{},
@joe
mock
.
get
"/people/5.xml"
,
{},
@marty
mock
.
get
"/people/Greg.xml"
,
{},
@greg
mock
.
get
"/people/4.xml"
,
{
'key'
=>
'value'
},
nil
,
404
...
...
@@ -1012,6 +1015,19 @@ def test_to_xml
assert
xml
.
include?
(
'<id type="integer">1</id>'
)
end
def
test_to_json
Person
.
include_root_in_json
=
true
Person
.
format
=
:json
joe
=
Person
.
find
(
6
)
json
=
joe
.
encode
Person
.
format
=
:xml
assert_match
%r{^
\{
"person":
\{
"person":
\{
}
,
json
assert_match
%r{"id":6}
,
json
assert_match
%r{"name":"Joe"}
,
json
assert_match
%r{
\}\}\}
$}
,
json
end
def
test_to_param_quacks_like_active_record
new_person
=
Person
.
new
assert_nil
new_person
.
to_param
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录