Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f7862b2c
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,发现更多精彩内容 >>
提交
f7862b2c
编写于
5月 12, 2010
作者:
S
Santiago Pastorino
提交者:
Jeremy Kemper
5月 12, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor of active_model/naming.rb and allow collection and element to be writable
Signed-off-by:
N
Jeremy Kemper
<
jeremy@bitsweat.net
>
上级
42fa2714
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
59 addition
and
5 deletion
+59
-5
activemodel/lib/active_model/naming.rb
activemodel/lib/active_model/naming.rb
+29
-5
activemodel/test/cases/naming_test.rb
activemodel/test/cases/naming_test.rb
+30
-0
未找到文件。
activemodel/lib/active_model/naming.rb
浏览文件 @
f7862b2c
...
...
@@ -3,18 +3,36 @@
module
ActiveModel
class
Name
<
String
attr_reader
:singular
,
:plural
,
:element
,
:collection
,
:partial_path
alias_method
:cache_key
,
:collection
attr_reader
:singular
,
:plural
,
:element
def
initialize
(
klass
)
super
(
klass
.
name
)
@klass
=
klass
@singular
=
ActiveSupport
::
Inflector
.
underscore
(
self
).
tr
(
'/'
,
'_'
).
freeze
@plural
=
ActiveSupport
::
Inflector
.
pluralize
(
@singular
).
freeze
@element
=
ActiveSupport
::
Inflector
.
underscore
(
ActiveSupport
::
Inflector
.
demodulize
(
self
)).
freeze
@collection
=
nil
self
.
element
=
ActiveSupport
::
Inflector
.
underscore
(
ActiveSupport
::
Inflector
.
demodulize
(
self
)).
freeze
end
def
element
=
(
element
)
@element
=
element
@human
=
ActiveSupport
::
Inflector
.
humanize
(
@element
).
freeze
@collection
=
ActiveSupport
::
Inflector
.
tableize
(
self
).
freeze
@partial_path
=
"
#{
@collection
}
/
#{
@element
}
"
.
freeze
@default_collection
=
nil
@partial_path
=
nil
end
def
collection
@collection
||
default_collection
end
alias_method
:cache_key
,
:collection
def
collection
=
(
collection
)
@collection
=
collection
@partial_path
=
nil
end
def
partial_path
@partial_path
||=
"
#{
collection
}
/
#{
@element
}
"
end
# Transform the model name into a more humane format, using I18n. By default,
...
...
@@ -34,6 +52,12 @@ def human(options={})
options
.
reverse_merge!
:scope
=>
[
@klass
.
i18n_scope
,
:models
],
:count
=>
1
,
:default
=>
defaults
I18n
.
translate
(
defaults
.
shift
,
options
)
end
private
def
default_collection
@default_collection
||=
ActiveSupport
::
Inflector
.
tableize
(
self
.
sub
(
/[^:]*$/
,
@element
)).
freeze
end
end
# ActiveModel::Naming is a module that creates a +model_name+ method on your
...
...
activemodel/test/cases/naming_test.rb
浏览文件 @
f7862b2c
...
...
@@ -18,6 +18,26 @@ def test_element
assert_equal
'track_back'
,
@model_name
.
element
end
def
test_set_element
@model_name
.
element
=
'foo'
assert_equal
'foo'
,
@model_name
.
element
assert_equal
'Foo'
,
@model_name
.
human
assert_equal
'post/foos'
,
@model_name
.
collection
assert_equal
'post/foos/foo'
,
@model_name
.
partial_path
end
def
test_human
assert_equal
'Track back'
,
@model_name
.
human
end
def
test_set_collection
@model_name
.
collection
=
'foo'
assert_equal
'foo'
,
@model_name
.
collection
assert_equal
'foo/track_back'
,
@model_name
.
partial_path
end
def
test_collection
assert_equal
'post/track_backs'
,
@model_name
.
collection
end
...
...
@@ -25,4 +45,14 @@ def test_collection
def
test_partial_path
assert_equal
'post/track_backs/track_back'
,
@model_name
.
partial_path
end
def
test_should_preserve_custom_collection
@model_name
.
collection
=
'bar'
@model_name
.
element
=
'foo'
assert_equal
'foo'
,
@model_name
.
element
assert_equal
'Foo'
,
@model_name
.
human
assert_equal
'bar'
,
@model_name
.
collection
assert_equal
'bar/foo'
,
@model_name
.
partial_path
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录