Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2c8a4a53
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,发现更多精彩内容 >>
提交
2c8a4a53
编写于
8月 02, 2010
作者:
T
Tore Darell
提交者:
José Valim
8月 03, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove or fix non-working examples and add a few tests to Dirty [#5185 state:resolved]
Signed-off-by:
N
José Valim
<
jose.valim@gmail.com
>
上级
fb2b8fec
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
87 addition
and
12 deletion
+87
-12
activemodel/lib/active_model/dirty.rb
activemodel/lib/active_model/dirty.rb
+2
-7
activemodel/test/cases/dirty_test.rb
activemodel/test/cases/dirty_test.rb
+85
-5
未找到文件。
activemodel/lib/active_model/dirty.rb
浏览文件 @
2c8a4a53
...
...
@@ -37,12 +37,13 @@ module ActiveModel
# end
#
# def name=(val)
# name_will_change!
# name_will_change!
unless val == @name
# @name = val
# end
#
# def save
# @previously_changed = changes
# @changed_attributes.clear
# end
#
# end
...
...
@@ -77,12 +78,6 @@ module ActiveModel
# person.changed # => ['name']
# person.changes # => { 'name' => ['Bill', 'Bob'] }
#
# Resetting an attribute returns it to its original state:
# person.reset_name! # => 'Bill'
# person.changed? # => false
# person.name_changed? # => false
# person.name # => 'Bill'
#
# If an attribute is modified in-place then make use of <tt>[attribute_name]_will_change!</tt>
# to mark that the attribute is changing. Otherwise ActiveModel can't track changes to
# in-place attributes.
...
...
activemodel/test/cases/dirty_test.rb
浏览文件 @
2c8a4a53
...
...
@@ -3,10 +3,11 @@
class
DirtyTest
<
ActiveModel
::
TestCase
class
DirtyModel
include
ActiveModel
::
Dirty
define_attribute_methods
[
:name
]
define_attribute_methods
[
:name
,
:color
]
def
initialize
@name
=
nil
@color
=
nil
end
def
name
...
...
@@ -17,13 +18,92 @@ def name=(val)
name_will_change!
@name
=
val
end
def
color
@color
end
def
color
=
(
val
)
color_will_change!
unless
val
==
@color
@color
=
val
end
def
save
@previously_changed
=
changes
@changed_attributes
.
clear
end
end
setup
do
@model
=
DirtyModel
.
new
end
test
"setting attribute will result in change"
do
assert
!
@model
.
changed?
assert
!
@model
.
name_changed?
@model
.
name
=
"Ringo"
assert
@model
.
changed?
assert
@model
.
name_changed?
end
test
"list of changed attributes"
do
assert_equal
[],
@model
.
changed
@model
.
name
=
"Paul"
assert_equal
[
'name'
],
@model
.
changed
end
test
"changes to attribute values"
do
assert
!
@model
.
changes
[
'name'
]
@model
.
name
=
"John"
assert_equal
[
nil
,
"John"
],
@model
.
changes
[
'name'
]
end
test
"changes accessible through both strings and symbols"
do
model
=
DirtyModel
.
new
model
.
name
=
"David"
assert_not_nil
model
.
changes
[
:name
]
assert_not_nil
model
.
changes
[
'name'
]
@model
.
name
=
"David"
assert_not_nil
@model
.
changes
[
:name
]
assert_not_nil
@model
.
changes
[
'name'
]
end
test
"attribute mutation"
do
@model
.
instance_variable_set
(
"@name"
,
"Yam"
)
assert
!
@model
.
name_changed?
@model
.
name
.
replace
(
"Hadad"
)
assert
!
@model
.
name_changed?
@model
.
name_will_change!
@model
.
name
.
replace
(
"Baal"
)
assert
@model
.
name_changed?
end
test
"resetting attribute"
do
@model
.
name
=
"Bob"
@model
.
reset_name!
assert_nil
@model
.
name
#assert !@model.name_changed #Doesn't work yet
end
test
"setting color to same value should not result in change being recorded"
do
@model
.
color
=
"red"
assert
@model
.
color_changed?
@model
.
save
assert
!
@model
.
color_changed?
assert
!
@model
.
changed?
@model
.
color
=
"red"
assert
!
@model
.
color_changed?
assert
!
@model
.
changed?
end
test
"saving should reset model's changed status"
do
@model
.
name
=
"Alf"
assert
@model
.
changed?
@model
.
save
assert
!
@model
.
changed?
assert
!
@model
.
name_changed?
end
test
"saving should preserve previous changes"
do
@model
.
name
=
"Jericho Cane"
@model
.
save
assert_equal
[
nil
,
"Jericho Cane"
],
@model
.
previous_changes
[
'name'
]
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录