Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5686fd0c
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,发现更多精彩内容 >>
提交
5686fd0c
编写于
6月 22, 2014
作者:
Y
Yves Senn
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #15839 from sgrif/sg-attr-set-null
Return a null object from `AttributeSet#[]`
上级
ebf552bd
6d7ac31d
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
17 addition
and
12 deletion
+17
-12
activerecord/lib/active_record/attribute_methods/before_type_cast.rb
...d/lib/active_record/attribute_methods/before_type_cast.rb
+1
-3
activerecord/lib/active_record/attribute_methods/dirty.rb
activerecord/lib/active_record/attribute_methods/dirty.rb
+3
-3
activerecord/lib/active_record/attribute_methods/read.rb
activerecord/lib/active_record/attribute_methods/read.rb
+0
-4
activerecord/lib/active_record/attribute_set.rb
activerecord/lib/active_record/attribute_set.rb
+2
-1
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+3
-1
activerecord/test/cases/attribute_set_test.rb
activerecord/test/cases/attribute_set_test.rb
+8
-0
未找到文件。
activerecord/lib/active_record/attribute_methods/before_type_cast.rb
浏览文件 @
5686fd0c
...
...
@@ -43,9 +43,7 @@ module BeforeTypeCast
# task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
# task.read_attribute_before_type_cast(:completed_on) # => "2012-10-21"
def
read_attribute_before_type_cast
(
attr_name
)
if
attr
=
@attributes
[
attr_name
.
to_s
]
attr
.
value_before_type_cast
end
@attributes
[
attr_name
.
to_s
].
value_before_type_cast
end
# Returns a hash of attributes before typecasting and deserialization.
...
...
activerecord/lib/active_record/attribute_methods/dirty.rb
浏览文件 @
5686fd0c
...
...
@@ -129,7 +129,7 @@ def keys_for_partial_write
end
def
_field_changed?
(
attr
,
old_value
)
attribute_named
(
attr
)
.
changed_from?
(
old_value
)
@attributes
[
attr
]
.
changed_from?
(
old_value
)
end
def
changed_in_place
...
...
@@ -140,7 +140,7 @@ def changed_in_place
def
changed_in_place?
(
attr_name
)
old_value
=
original_raw_attribute
(
attr_name
)
attribute_named
(
attr_name
)
.
changed_in_place_from?
(
old_value
)
@attributes
[
attr_name
]
.
changed_in_place_from?
(
old_value
)
end
def
original_raw_attribute
(
attr_name
)
...
...
@@ -154,7 +154,7 @@ def original_raw_attributes
end
def
store_original_raw_attribute
(
attr_name
)
original_raw_attributes
[
attr_name
]
=
attribute_named
(
attr_name
)
.
value_for_database
original_raw_attributes
[
attr_name
]
=
@attributes
[
attr_name
]
.
value_for_database
end
def
store_original_raw_attributes
...
...
activerecord/lib/active_record/attribute_methods/read.rb
浏览文件 @
5686fd0c
...
...
@@ -99,10 +99,6 @@ def read_attribute(attr_name)
def
attribute
(
attribute_name
)
read_attribute
(
attribute_name
)
end
def
attribute_named
(
attribute_name
)
@attributes
.
fetch
(
attribute_name
,
Attribute
::
Null
)
end
end
end
end
activerecord/lib/active_record/attribute_set.rb
浏览文件 @
5686fd0c
...
...
@@ -40,7 +40,8 @@ def initialize(types)
end
def
build_from_database
(
values
,
additional_types
=
{})
attributes
=
values
.
each_with_object
({})
do
|
(
name
,
value
),
hash
|
attributes
=
Hash
.
new
(
Attribute
::
Null
)
values
.
each_with_object
(
attributes
)
do
|
(
name
,
value
),
hash
|
type
=
additional_types
.
fetch
(
name
,
@types
[
name
])
hash
[
name
]
=
Attribute
.
from_database
(
value
,
type
)
end
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
5686fd0c
...
...
@@ -523,7 +523,9 @@ def to_ary # :nodoc:
def
init_internals
pk
=
self
.
class
.
primary_key
@attributes
[
pk
]
||=
Attribute
.
from_database
(
nil
,
type_for_attribute
(
pk
))
unless
@attributes
.
include?
(
pk
)
@attributes
[
pk
]
=
Attribute
.
from_database
(
nil
,
type_for_attribute
(
pk
))
end
@aggregation_cache
=
{}
@association_cache
=
{}
...
...
activerecord/test/cases/attribute_set_test.rb
浏览文件 @
5686fd0c
...
...
@@ -18,6 +18,14 @@ class AttributeSetTest < ActiveRecord::TestCase
assert_equal
4
,
attributes
[
:bar
].
value
end
test
"[] returns a null object"
do
builder
=
AttributeSet
::
Builder
.
new
(
foo:
Type
::
Float
.
new
)
attributes
=
builder
.
build_from_database
(
foo:
'3.3'
)
assert_equal
'3.3'
,
attributes
[
:foo
].
value_before_type_cast
assert_equal
nil
,
attributes
[
:bar
].
value_before_type_cast
end
test
"duping creates a new hash and dups each attribute"
do
builder
=
AttributeSet
::
Builder
.
new
(
foo:
Type
::
Integer
.
new
,
bar:
Type
::
String
.
new
)
attributes
=
builder
.
build_from_database
(
foo:
1
,
bar:
'foo'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录