Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
064c28d6
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 搜索 >>
提交
064c28d6
编写于
11月 23, 2010
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixing dup regressions
上级
93d78b83
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
35 addition
and
28 deletion
+35
-28
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+8
-4
activerecord/test/cases/base_test.rb
activerecord/test/cases/base_test.rb
+16
-16
activerecord/test/cases/dirty_test.rb
activerecord/test/cases/dirty_test.rb
+4
-4
activerecord/test/cases/dup_test.rb
activerecord/test/cases/dup_test.rb
+6
-3
activerecord/test/cases/inheritance_test.rb
activerecord/test/cases/inheritance_test.rb
+1
-1
未找到文件。
activerecord/lib/active_record/base.rb
浏览文件 @
064c28d6
...
...
@@ -1592,8 +1592,8 @@ def frozen?
end
# Duped objects have no id assigned and are treated as new records. Note
# that this is a "shallow" c
lone
as it copies the object's attributes
# only, not its associations. The extent of a "deep"
dup
is application
# that this is a "shallow" c
opy
as it copies the object's attributes
# only, not its associations. The extent of a "deep"
copy
is application
# specific and is therefore left to the application to implement according
# to its need.
def
initialize_dup
(
other
)
...
...
@@ -1602,8 +1602,12 @@ def initialize_dup(other)
cloned_attributes
=
other
.
clone_attributes
(
:read_attribute_before_type_cast
)
cloned_attributes
.
delete
(
self
.
class
.
primary_key
)
@attributes
=
cloned_attributes
@changed_attributes
=
other
.
changed_attributes
.
dup
@attributes
=
cloned_attributes
@changed_attributes
=
{}
attributes_from_column_definition
.
each
do
|
attr
,
orig_value
|
@changed_attributes
[
attr
]
=
orig_value
if
field_changed?
(
attr
,
orig_value
,
@attributes
[
attr
])
end
clear_aggregation_cache
clear_association_cache
...
...
activerecord/test/cases/base_test.rb
浏览文件 @
064c28d6
...
...
@@ -701,24 +701,24 @@ def test_dup
assert_equal
({
'a'
=>
'c'
}.
to_s
,
duped_topic
.
title
)
end
def
test_
clone
_with_aggregate_of_same_name_as_attribute
def
test_
dup
_with_aggregate_of_same_name_as_attribute
dev
=
DeveloperWithAggregate
.
find
(
1
)
assert_kind_of
DeveloperSalary
,
dev
.
salary
clone
=
nil
assert_nothing_raised
{
clone
=
dev
.
clone
}
assert_kind_of
DeveloperSalary
,
clone
.
salary
assert_equal
dev
.
salary
.
amount
,
clone
.
salary
.
amount
assert
!
clone
.
persisted?
dup
=
nil
assert_nothing_raised
{
dup
=
dev
.
dup
}
assert_kind_of
DeveloperSalary
,
dup
.
salary
assert_equal
dev
.
salary
.
amount
,
dup
.
salary
.
amount
assert
!
dup
.
persisted?
# test if the attributes have been
clone
d
original_amount
=
clone
.
salary
.
amount
# test if the attributes have been
dup
d
original_amount
=
dup
.
salary
.
amount
dev
.
salary
.
amount
=
1
assert_equal
original_amount
,
clone
.
salary
.
amount
assert_equal
original_amount
,
dup
.
salary
.
amount
assert
clone
.
save
assert
clone
.
persisted?
assert_not_equal
clone
.
id
,
dev
.
id
assert
dup
.
save
assert
dup
.
persisted?
assert_not_equal
dup
.
id
,
dev
.
id
end
def
test_dup_does_not_copy_associations
...
...
@@ -766,22 +766,22 @@ def test_clone_of_new_object_marks_as_dirty_only_changed_attributes
assert
!
cloned_developer
.
salary_changed?
# ... and cloned instance should behave same
end
def
test_
clone
_of_saved_object_marks_attributes_as_dirty
def
test_
dup
_of_saved_object_marks_attributes_as_dirty
developer
=
Developer
.
create!
:name
=>
'Bjorn'
,
:salary
=>
100000
assert
!
developer
.
name_changed?
assert
!
developer
.
salary_changed?
cloned_developer
=
developer
.
clone
cloned_developer
=
developer
.
dup
assert
cloned_developer
.
name_changed?
# both attributes differ from defaults
assert
cloned_developer
.
salary_changed?
end
def
test_
clone
_of_saved_object_marks_as_dirty_only_changed_attributes
def
test_
dup
_of_saved_object_marks_as_dirty_only_changed_attributes
developer
=
Developer
.
create!
:name
=>
'Bjorn'
assert
!
developer
.
name_changed?
# both attributes of saved object should be threated as not changed
assert
!
developer
.
salary_changed?
cloned_developer
=
developer
.
clone
cloned_developer
=
developer
.
dup
assert
cloned_developer
.
name_changed?
# ... but on cloned object should be
assert
!
cloned_developer
.
salary_changed?
# ... BUT salary has non-nil default which should be threated as not changed on cloned instance
end
...
...
activerecord/test/cases/dirty_test.rb
浏览文件 @
064c28d6
...
...
@@ -338,13 +338,13 @@ def test_reload_should_clear_changed_attributes
assert
!
pirate
.
changed?
end
def
test_
cloned
_objects_should_not_copy_dirty_flag_from_creator
def
test_
dup
_objects_should_not_copy_dirty_flag_from_creator
pirate
=
Pirate
.
create!
(
:catchphrase
=>
"shiver me timbers"
)
pirate_
clone
=
pirate
.
clone
pirate_
clone
.
reset_catchphrase!
pirate_
dup
=
pirate
.
dup
pirate_
dup
.
reset_catchphrase!
pirate
.
catchphrase
=
"I love Rum"
assert
pirate
.
catchphrase_changed?
assert
!
pirate_
clone
.
catchphrase_changed?
assert
!
pirate_
dup
.
catchphrase_changed?
end
def
test_reverted_changes_are_not_dirty
...
...
activerecord/test/cases/dup_test.rb
浏览文件 @
064c28d6
...
...
@@ -31,9 +31,12 @@ def test_dup_with_modified_attributes
end
def
test_dup_with_changes
topic
=
Topic
.
first
topic
.
author_name
=
'Aaron'
duped
=
topic
.
dup
dbtopic
=
Topic
.
first
topic
=
Topic
.
new
topic
.
attributes
=
dbtopic
.
attributes
duped
=
dbtopic
.
dup
assert_equal
topic
.
changes
,
duped
.
changes
end
...
...
activerecord/test/cases/inheritance_test.rb
浏览文件 @
064c28d6
...
...
@@ -16,7 +16,7 @@ def test_class_with_store_full_sti_class_returns_full_name
def
test_class_with_blank_sti_name
company
=
Company
.
find
(
:first
)
company
=
company
.
clone
company
=
company
.
dup
company
.
extend
(
Module
.
new
{
def
read_attribute
(
name
)
return
' '
if
name
==
'type'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录