Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
9dd67fce
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,发现更多精彩内容 >>
提交
9dd67fce
编写于
2月 21, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add to_key and to_param methods to ActiveModel::Conversion.
上级
f81c6bc0
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
70 addition
and
27 deletion
+70
-27
activemodel/lib/active_model/conversion.rb
activemodel/lib/active_model/conversion.rb
+35
-10
activemodel/test/cases/conversion_test.rb
activemodel/test/cases/conversion_test.rb
+25
-0
activemodel/test/cases/lint_test.rb
activemodel/test/cases/lint_test.rb
+2
-15
activemodel/test/models/contact.rb
activemodel/test/models/contact.rb
+7
-1
activerecord/lib/active_record/attribute_methods/primary_key.rb
...record/lib/active_record/attribute_methods/primary_key.rb
+1
-1
未找到文件。
activemodel/lib/active_model/conversion.rb
浏览文件 @
9dd67fce
module
ActiveModel
# If your object is already designed to implement all of the Active Model featurs
# include this module in your Class.
#
# class MyClass
# Handle default conversions: to_model, to_key and to_param.
#
# == Example
#
# Let's take for example this non persisted object.
#
# class ContactMessage
# include ActiveModel::Conversion
#
# # Always a new record, since it's not persisted in the DB.
# def new_record?
# true
# end
# end
#
#
Returns self to the <tt>:to_model</tt> method.
#
#
If your model does not act like an Active Model object, then you should define
#
<tt>:to_model</tt> yourself returning a proxy object that wraps your object
#
with Active Model compliant methods.
#
#
cm = ContactMessage.new
#
cm.to_model == self #=> true
#
cm.to_key #=> nil
#
cm.to_param #=> nil
#
module
Conversion
# If your object is already designed to implement all of the Active Model you can use
# the default to_model implementation, which simply returns self.
#
# If your model does not act like an Active Model object, then you should define
# <tt>:to_model</tt> yourself returning a proxy object that wraps your object
# with Active Model compliant methods.
def
to_model
self
end
# Returns an Enumerable of all (primary) key attributes or nil if new_record? is true
def
to_key
new_record?
?
nil
:
[
id
]
end
# Returns a string representing the object's key suitable for use in URLs,
# or nil if new_record? is true
def
to_param
to_key
?
to_key
.
join
(
'-'
)
:
nil
end
end
end
activemodel/test/cases/conversion_test.rb
0 → 100644
浏览文件 @
9dd67fce
require
'cases/helper'
require
'models/contact'
class
ConversionTest
<
ActiveModel
::
TestCase
test
"to_model default implementation returns self"
do
contact
=
Contact
.
new
assert_equal
contact
,
contact
.
to_model
end
test
"to_key default implementation returns nil for new records"
do
assert_nil
Contact
.
new
.
to_key
end
test
"to_key default implementation returns the id in an array for persisted records"
do
assert_equal
[
1
],
Contact
.
new
(
:new_record
=>
false
,
:id
=>
1
).
to_key
end
test
"to_param default implementation returns nil for new records"
do
assert_nil
Contact
.
new
.
to_param
end
test
"to_param default implementation returns a string of ids for persisted records"
do
assert_equal
"1"
,
Contact
.
new
(
:new_record
=>
false
,
:id
=>
1
).
to_param
end
end
\ No newline at end of file
activemodel/test/cases/lint_test.rb
浏览文件 @
9dd67fce
require
"cases/helper"
require
'cases/helper'
class
LintTest
<
ActiveModel
::
TestCase
include
ActiveModel
::
Lint
::
Tests
class
CompliantModel
extend
ActiveModel
::
Naming
def
to_model
self
end
def
to_key
new_record?
?
nil
:
[
id
]
end
def
to_param
return
nil
if
to_key
.
nil?
# some default for CPKs, real implementations will differ
to_key
.
length
>
1
?
to_key
.
join
(
'-'
)
:
to_key
.
first
.
to_s
end
include
ActiveModel
::
Conversion
def
valid?
()
true
end
def
new_record?
()
true
end
...
...
activemodel/test/models/contact.rb
浏览文件 @
9dd67fce
class
Contact
attr_accessor
:name
,
:age
,
:created_at
,
:awesome
,
:preferences
include
ActiveModel
::
Conversion
attr_accessor
:id
,
:name
,
:age
,
:created_at
,
:awesome
,
:preferences
,
:new_record
def
initialize
(
options
=
{})
options
.
each
{
|
name
,
value
|
send
(
"
#{
name
}
="
,
value
)
}
end
def
new_record?
defined?
(
@new_record
)
?
@new_record
:
true
end
end
activerecord/lib/active_record/attribute_methods/primary_key.rb
浏览文件 @
9dd67fce
...
...
@@ -50,7 +50,7 @@ module InstanceMethods
# This method also takes custom primary keys specified via
# the +set_primary_key+ into account.
def
to_key
new_record?
?
nil
:
[
self
.
send
(
self
.
class
.
primary_key
)
]
new_record?
?
nil
:
[
self
.
primary_key
]
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录