Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5aa1f5d3
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,发现更多精彩内容 >>
提交
5aa1f5d3
编写于
5月 26, 2014
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow specifying a default value in overloaded properties
上级
8c77b0a0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
19 addition
and
6 deletion
+19
-6
activerecord/lib/active_record/properties.rb
activerecord/lib/active_record/properties.rb
+13
-2
activerecord/test/cases/custom_properties_test.rb
activerecord/test/cases/custom_properties_test.rb
+5
-4
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+1
-0
未找到文件。
activerecord/lib/active_record/properties.rb
浏览文件 @
5aa1f5d3
...
...
@@ -14,6 +14,17 @@ module ClassMethods
# Active Record's type casting behavior, as well as adding support for user defined
# types.
#
# +name+ The name of the methods to define attribute methods for, and the column which
# this will persist to.
#
# +cast_type+ A type object that contains information about how to type cast the value.
# See the examples section for more information.
#
# ==== Options
# The options hash accepts the following options:
#
# +default+ is the default value that the column should use on a new record.
#
# ==== Examples
#
# The type detected by Active Record can be overriden.
...
...
@@ -62,11 +73,11 @@ module ClassMethods
#
# store_listing = StoreListing.new(price_in_cents: '$10.00')
# store_listing.price_in_cents # => 1000
def
property
(
name
,
cast_type
)
def
property
(
name
,
cast_type
,
options
=
{}
)
name
=
name
.
to_s
clear_properties_cache
# Assign a new hash to ensure that subclasses do not share a hash
self
.
user_provided_columns
=
user_provided_columns
.
merge
(
name
=>
connection
.
new_column
(
name
,
nil
,
cast_type
))
self
.
user_provided_columns
=
user_provided_columns
.
merge
(
name
=>
connection
.
new_column
(
name
,
options
[
:default
]
,
cast_type
))
end
# Returns an array of column objects for the table associated with this class.
...
...
activerecord/test/cases/custom_properties_test.rb
浏览文件 @
5aa1f5d3
...
...
@@ -4,6 +4,7 @@ class OverloadedType < ActiveRecord::Base
property
:overloaded_float
,
Type
::
Integer
.
new
property
:overloaded_string_with_limit
,
Type
::
String
.
new
(
limit:
50
)
property
:non_existent_decimal
,
Type
::
Decimal
.
new
property
:string_with_default
,
Type
::
String
.
new
,
default:
'the overloaded default'
end
class
ChildOfOverloadedType
<
OverloadedType
...
...
@@ -62,12 +63,12 @@ def test_nonexistent_property
end
end
def
test_
overloaded_properties_have_no_default
def
test_
changing_defaults
data
=
OverloadedType
.
new
unoverloaded_data
=
UnoverloadedType
.
new
assert_
nil
data
.
overloaded_floa
t
assert
unoverloaded_data
.
overloaded_floa
t
assert_
equal
'the overloaded default'
,
data
.
string_with_defaul
t
assert
_equal
'the original default'
,
unoverloaded_data
.
string_with_defaul
t
end
def
test_children_inherit_custom_properties
...
...
@@ -84,7 +85,7 @@ def test_children_can_override_parents
def
test_overloading_properties_does_not_change_column_order
column_names
=
OverloadedType
.
column_names
assert_equal
%w(id overloaded_float unoverloaded_float overloaded_string_with_limit non_existent_decimal)
,
column_names
assert_equal
%w(id overloaded_float unoverloaded_float overloaded_string_with_limit
string_with_default
non_existent_decimal)
,
column_names
end
end
end
activerecord/test/schema/schema.rb
浏览文件 @
5aa1f5d3
...
...
@@ -860,6 +860,7 @@ def except(adapter_names_to_exclude)
t
.
float
:overloaded_float
,
default:
500
t
.
float
:unoverloaded_float
t
.
string
:overloaded_string_with_limit
,
limit:
255
t
.
string
:string_with_default
,
default:
'the original default'
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录