Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f4f68885
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,发现更多精彩内容 >>
提交
f4f68885
编写于
10月 02, 2009
作者:
T
Taryn East
提交者:
Joshua Peek
10月 02, 2009
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update_attribute(s) added to Active Resource
Signed-off-by:
N
Joshua Peek
<
josh@joshpeek.com
>
上级
420004e0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
81 addition
and
2 deletion
+81
-2
activeresource/lib/active_resource/base.rb
activeresource/lib/active_resource/base.rb
+30
-0
activeresource/test/cases/base_test.rb
activeresource/test/cases/base_test.rb
+51
-2
未找到文件。
activeresource/lib/active_resource/base.rb
浏览文件 @
f4f68885
...
...
@@ -1099,6 +1099,36 @@ def load(attributes)
self
end
# Updates a single attribute and then saves the object.
#
# Note: Unlike ActiveRecord::Base.update_attribute, this method <b>is</b>
# subject to normal validation routines as an update sends the whole body
# of the resource in the request. (See Validations).
#
# As such, this method is equivalent to calling update_attributes with a single attribute/value pair.
#
# If the saving fails because of a connection or remote service error, an
# exception will be raised. If saving fails because the resource is
# invalid then <tt>false</tt> will be returned.
def
update_attribute
(
name
,
value
)
self
.
send
(
"
#{
name
}
="
.
to_sym
,
value
)
self
.
save
end
# Updates this resource with all the attributes from the passed-in Hash
# and requests that the record be saved.
#
# If the saving fails because of a connection or remote service error, an
# exception will be raised. If saving fails because the resource is
# invalid then <tt>false</tt> will be returned.
#
# Note: Though this request can be made with a partial set of the
# resource's attributes, the full body of the request will still be sent
# in the save request to the remote service.
def
update_attributes
(
attributes
)
load
(
attributes
)
&&
save
end
# For checking <tt>respond_to?</tt> without searching the attributes (which is faster).
alias_method
:respond_to_without_attributes?
,
:respond_to?
...
...
activeresource/test/cases/base_test.rb
浏览文件 @
f4f68885
...
...
@@ -11,12 +11,12 @@ 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'
}.
to_xml
(
:root
=>
'address'
)
@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"
)
@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'
}].
to_xml
(
:root
=>
'addresses'
)
@addresses
=
[{
:id
=>
1
,
:street
=>
'12345 Street'
,
:country
=>
'Australia'
}].
to_xml
(
:root
=>
'addresses'
)
# - deep nested resource -
# - Luis (Customer)
...
...
@@ -813,6 +813,55 @@ def test_update_conflict
assert_raise
(
ActiveResource
::
ResourceConflict
)
{
Person
.
find
(
2
).
save
}
end
######
# update_attribute(s)(!)
def
test_update_attribute_as_symbol
matz
=
Person
.
first
matz
.
expects
(
:save
).
returns
(
true
)
assert_equal
"Matz"
,
matz
.
name
assert
matz
.
update_attribute
(
:name
,
"David"
)
assert_equal
"David"
,
matz
.
name
end
def
test_update_attribute_as_string
matz
=
Person
.
first
matz
.
expects
(
:save
).
returns
(
true
)
assert_equal
"Matz"
,
matz
.
name
assert
matz
.
update_attribute
(
'name'
,
"David"
)
assert_equal
"David"
,
matz
.
name
end
def
test_update_attributes_as_symbols
addy
=
StreetAddress
.
first
(
:params
=>
{
:person_id
=>
1
})
addy
.
expects
(
:save
).
returns
(
true
)
assert_equal
"12345 Street"
,
addy
.
street
assert_equal
"Australia"
,
addy
.
country
assert
addy
.
update_attributes
(
:street
=>
'54321 Street'
,
:country
=>
'USA'
)
assert_equal
"54321 Street"
,
addy
.
street
assert_equal
"USA"
,
addy
.
country
end
def
test_update_attributes_as_strings
addy
=
StreetAddress
.
first
(
:params
=>
{
:person_id
=>
1
})
addy
.
expects
(
:save
).
returns
(
true
)
assert_equal
"12345 Street"
,
addy
.
street
assert_equal
"Australia"
,
addy
.
country
assert
addy
.
update_attributes
(
'street'
=>
'54321 Street'
,
'country'
=>
'USA'
)
assert_equal
"54321 Street"
,
addy
.
street
assert_equal
"USA"
,
addy
.
country
end
#####
# Mayhem and destruction
def
test_destroy
assert
Person
.
find
(
1
).
destroy
ActiveResource
::
HttpMock
.
respond_to
do
|
mock
|
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录