Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6e5aed05
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,发现更多精彩内容 >>
提交
6e5aed05
编写于
8月 31, 2010
作者:
P
Piotr Sarnacki
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Prepared ActiveModel::Naming to handle cases for namespaced isolated engines
上级
34cd8a68
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
134 addition
and
4 deletion
+134
-4
activemodel/lib/active_model/naming.rb
activemodel/lib/active_model/naming.rb
+22
-4
activemodel/test/cases/naming_test.rb
activemodel/test/cases/naming_test.rb
+99
-0
activemodel/test/models/blog_post.rb
activemodel/test/models/blog_post.rb
+13
-0
未找到文件。
activemodel/lib/active_model/naming.rb
浏览文件 @
6e5aed05
...
...
@@ -2,18 +2,22 @@
module
ActiveModel
class
Name
<
String
attr_reader
:singular
,
:plural
,
:element
,
:collection
,
:partial_path
attr_reader
:singular
,
:plural
,
:element
,
:collection
,
:partial_path
,
:route_key
,
:param_key
alias_method
:cache_key
,
:collection
def
initialize
(
klass
)
def
initialize
(
klass
,
namespace
=
nil
)
super
(
klass
.
name
)
@unnamespaced
=
self
.
sub
(
/^
#{
namespace
.
name
}
::/
,
''
)
if
namespace
@klass
=
klass
@singular
=
ActiveSupport
::
Inflector
.
underscore
(
self
).
tr
(
'/'
,
'_'
).
freeze
@singular
=
_singularize
(
self
).
freeze
@plural
=
ActiveSupport
::
Inflector
.
pluralize
(
@singular
).
freeze
@element
=
ActiveSupport
::
Inflector
.
underscore
(
ActiveSupport
::
Inflector
.
demodulize
(
self
)).
freeze
@human
=
ActiveSupport
::
Inflector
.
humanize
(
@element
).
freeze
@collection
=
ActiveSupport
::
Inflector
.
tableize
(
self
).
freeze
@partial_path
=
"
#{
@collection
}
/
#{
@element
}
"
.
freeze
@param_key
=
(
namespace
?
_singularize
(
@unnamespaced
)
:
@singular
).
freeze
@route_key
=
(
namespace
?
ActiveSupport
::
Inflector
.
pluralize
(
@param_key
)
:
@plural
).
freeze
end
# Transform the model name into a more humane format, using I18n. By default,
...
...
@@ -36,6 +40,11 @@ def human(options={})
options
.
reverse_merge!
:scope
=>
[
@klass
.
i18n_scope
,
:models
],
:count
=>
1
,
:default
=>
defaults
I18n
.
translate
(
defaults
.
shift
,
options
)
end
private
def
_singularize
(
str
)
ActiveSupport
::
Inflector
.
underscore
(
str
).
tr
(
'/'
,
'_'
)
end
end
# == Active Model Naming
...
...
@@ -58,7 +67,8 @@ module Naming
# Returns an ActiveModel::Name object for module. It can be
# used to retrieve all kinds of naming-related information.
def
model_name
@_model_name
||=
ActiveModel
::
Name
.
new
(
self
)
namespace
=
self
.
parents
.
detect
{
|
n
|
n
.
respond_to?
(
:_railtie
)
}
@_model_name
||=
ActiveModel
::
Name
.
new
(
self
,
namespace
)
end
# Returns the plural class name of a record or class. Examples:
...
...
@@ -85,6 +95,14 @@ def self.uncountable?(record_or_class)
plural
(
record_or_class
)
==
singular
(
record_or_class
)
end
def
self
.
route_key
(
record_or_class
)
model_name_from_record_or_class
(
record_or_class
).
route_key
end
def
self
.
param_key
(
record_or_class
)
model_name_from_record_or_class
(
record_or_class
).
param_key
end
private
def
self
.
model_name_from_record_or_class
(
record_or_class
)
(
record_or_class
.
is_a?
(
Class
)
?
record_or_class
:
record_or_class
.
class
).
model_name
...
...
activemodel/test/cases/naming_test.rb
浏览文件 @
6e5aed05
...
...
@@ -2,6 +2,7 @@
require
'models/contact'
require
'models/sheep'
require
'models/track_back'
require
'models/blog_post'
class
NamingTest
<
ActiveModel
::
TestCase
def
setup
...
...
@@ -29,6 +30,86 @@ def test_partial_path
end
end
class
NamingWithNamespacedModelInIsolatedNamespaceTest
<
ActiveModel
::
TestCase
def
setup
@model_name
=
ActiveModel
::
Name
.
new
(
Blog
::
Post
,
Blog
)
end
def
test_singular
assert_equal
'blog_post'
,
@model_name
.
singular
end
def
test_plural
assert_equal
'blog_posts'
,
@model_name
.
plural
end
def
test_element
assert_equal
'post'
,
@model_name
.
element
end
def
test_collection
assert_equal
'blog/posts'
,
@model_name
.
collection
end
def
test_partial_path
assert_equal
'blog/posts/post'
,
@model_name
.
partial_path
end
def
test_human
assert_equal
'Post'
,
@model_name
.
human
end
def
test_route_key
assert_equal
'posts'
,
@model_name
.
route_key
end
def
test_param_key
assert_equal
'post'
,
@model_name
.
param_key
end
def
test_recognizing_namespace
assert_equal
'Post'
,
Blog
::
Post
.
model_name
.
instance_variable_get
(
"@unnamespaced"
)
end
end
class
NamingWithNamespacedModelInSharedNamespaceTest
<
ActiveModel
::
TestCase
def
setup
@model_name
=
ActiveModel
::
Name
.
new
(
Blog
::
Post
)
end
def
test_singular
assert_equal
'blog_post'
,
@model_name
.
singular
end
def
test_plural
assert_equal
'blog_posts'
,
@model_name
.
plural
end
def
test_element
assert_equal
'post'
,
@model_name
.
element
end
def
test_collection
assert_equal
'blog/posts'
,
@model_name
.
collection
end
def
test_partial_path
assert_equal
'blog/posts/post'
,
@model_name
.
partial_path
end
def
test_human
assert_equal
'Post'
,
@model_name
.
human
end
def
test_route_key
assert_equal
'blog_posts'
,
@model_name
.
route_key
end
def
test_param_key
assert_equal
'blog_post'
,
@model_name
.
param_key
end
end
class
NamingHelpersTest
<
Test
::
Unit
::
TestCase
def
setup
@klass
=
Contact
...
...
@@ -36,6 +117,8 @@ def setup
@singular
=
'contact'
@plural
=
'contacts'
@uncountable
=
Sheep
@route_key
=
'contacts'
@param_key
=
'contact'
end
def
test_singular
...
...
@@ -54,6 +137,22 @@ def test_plural_for_class
assert_equal
@plural
,
plural
(
@klass
)
end
def
test_route_key
assert_equal
@route_key
,
route_key
(
@record
)
end
def
test_route_key_for_class
assert_equal
@route_key
,
route_key
(
@klass
)
end
def
test_param_key
assert_equal
@param_key
,
param_key
(
@record
)
end
def
test_param_key_for_class
assert_equal
@param_key
,
param_key
(
@klass
)
end
def
test_uncountable
assert
uncountable?
(
@uncountable
),
"Expected 'sheep' to be uncoutable"
assert
!
uncountable?
(
@klass
),
"Expected 'contact' to be countable"
...
...
activemodel/test/models/blog_post.rb
0 → 100644
浏览文件 @
6e5aed05
module
Blog
def
self
.
_railtie
Object
.
new
end
def
self
.
table_name_prefix
"blog_"
end
class
Post
extend
ActiveModel
::
Naming
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录