Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f8e5022c
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,发现更多精彩内容 >>
提交
f8e5022c
编写于
11月 06, 2013
作者:
X
Xavier Noria
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12789 from claudiob/humanize-without-capitalizing
Add +capitalize+ option to Inflector.humanize
上级
256ae7de
c61544c7
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
58 addition
and
15 deletion
+58
-15
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+7
-0
activesupport/lib/active_support/core_ext/string/inflections.rb
...support/lib/active_support/core_ext/string/inflections.rb
+11
-5
activesupport/lib/active_support/inflector/methods.rb
activesupport/lib/active_support/inflector/methods.rb
+15
-9
activesupport/test/core_ext/string_ext_test.rb
activesupport/test/core_ext/string_ext_test.rb
+7
-1
activesupport/test/inflector_test.rb
activesupport/test/inflector_test.rb
+6
-0
activesupport/test/inflector_test_cases.rb
activesupport/test/inflector_test_cases.rb
+6
-0
guides/source/active_support_core_extensions.md
guides/source/active_support_core_extensions.md
+6
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
f8e5022c
*
Add
`capitalize`
option to Inflector.humanize, so strings can be humanized without being capitalized:
'employee_salary'.humanize # => "Employee salary"
'employee_salary'.humanize(capitalize: false) # => "employee salary"
*claudiob*
*
Fixed Object#as_json and Struct#as_json not working properly with options. They now take
*
Fixed Object#as_json and Struct#as_json not working properly with options. They now take
the same options as Hash#as_json:
the same options as Hash#as_json:
...
...
activesupport/lib/active_support/core_ext/string/inflections.rb
浏览文件 @
f8e5022c
...
@@ -190,13 +190,19 @@ def classify
...
@@ -190,13 +190,19 @@ def classify
ActiveSupport
::
Inflector
.
classify
(
self
)
ActiveSupport
::
Inflector
.
classify
(
self
)
end
end
# Capitalizes the first word, turns underscores into spaces, and strips '_id'.
# Capitalizes the first word, turns underscores into spaces, and strips a
# trailing '_id' if present.
# Like +titleize+, this is meant for creating pretty output.
# Like +titleize+, this is meant for creating pretty output.
#
#
# 'employee_salary'.humanize # => "Employee salary"
# The capitalization of the first word can be turned off by setting the
# 'author_id'.humanize # => "Author"
# optional parameter +capitalize+ to false.
def
humanize
# By default, this parameter is true.
ActiveSupport
::
Inflector
.
humanize
(
self
)
#
# 'employee_salary'.humanize # => "Employee salary"
# 'author_id'.humanize # => "Author"
# 'author_id'.humanize(capitalize: false) # => "author"
def
humanize
(
options
=
{})
ActiveSupport
::
Inflector
.
humanize
(
self
,
options
)
end
end
# Creates a foreign key name from a class name.
# Creates a foreign key name from a class name.
...
...
activesupport/lib/active_support/inflector/methods.rb
浏览文件 @
f8e5022c
...
@@ -98,20 +98,26 @@ def underscore(camel_cased_word)
...
@@ -98,20 +98,26 @@ def underscore(camel_cased_word)
word
word
end
end
# Capitalizes the first word and turns underscores into spaces and strips a
# Capitalizes the first word, turns underscores into spaces, and strips a
# trailing "_id", if any. Like +titleize+, this is meant for creating pretty
# trailing '_id' if present.
# output.
# Like +titleize+, this is meant for creating pretty output.
#
#
# 'employee_salary'.humanize # => "Employee salary"
# The capitalization of the first word can be turned off by setting the
# 'author_id'.humanize # => "Author"
# optional parameter +capitalize+ to false.
def
humanize
(
lower_case_and_underscored_word
)
# By default, this parameter is true.
#
# humanize('employee_salary') # => "Employee salary"
# humanize('author_id') # => "Author"
# humanize('author_id', capitalize: false) # => "author"
def
humanize
(
lower_case_and_underscored_word
,
options
=
{})
result
=
lower_case_and_underscored_word
.
to_s
.
dup
result
=
lower_case_and_underscored_word
.
to_s
.
dup
inflections
.
humans
.
each
{
|
(
rule
,
replacement
)
|
break
if
result
.
sub!
(
rule
,
replacement
)
}
inflections
.
humans
.
each
{
|
(
rule
,
replacement
)
|
break
if
result
.
sub!
(
rule
,
replacement
)
}
result
.
gsub!
(
/_id$/
,
""
)
result
.
gsub!
(
/_id$/
,
""
)
result
.
tr!
(
'_'
,
' '
)
result
.
tr!
(
'_'
,
' '
)
result
.
gsub
(
/([a-z\d]*)/i
)
{
|
match
|
result
.
gsub
!
(
/([a-z\d]*)/i
)
{
|
match
|
"
#{
inflections
.
acronyms
[
match
]
||
match
.
downcase
}
"
"
#{
inflections
.
acronyms
[
match
]
||
match
.
downcase
}
"
}.
gsub
(
/^\w/
)
{
$&
.
upcase
}
}
options
.
fetch
(
:capitalize
,
true
)
?
result
.
gsub
(
/^\w/
)
{
$&
.
upcase
}
:
result
end
end
# Capitalizes all the words and replaces some characters in the string to
# Capitalizes all the words and replaces some characters in the string to
...
...
activesupport/test/core_ext/string_ext_test.rb
浏览文件 @
f8e5022c
...
@@ -155,6 +155,12 @@ def test_humanize
...
@@ -155,6 +155,12 @@ def test_humanize
end
end
end
end
def
test_humanize_without_capitalize
UnderscoreToHumanWithoutCapitalize
.
each
do
|
underscore
,
human
|
assert_equal
(
human
,
underscore
.
humanize
(
capitalize:
false
))
end
end
def
test_ord
def
test_ord
assert_equal
97
,
'a'
.
ord
assert_equal
97
,
'a'
.
ord
assert_equal
97
,
'abc'
.
ord
assert_equal
97
,
'abc'
.
ord
...
@@ -270,7 +276,7 @@ def test_truncate_multibyte
...
@@ -270,7 +276,7 @@ def test_truncate_multibyte
def
test_truncate_should_not_be_html_safe
def
test_truncate_should_not_be_html_safe
assert
!
"Hello World!"
.
truncate
(
12
).
html_safe?
assert
!
"Hello World!"
.
truncate
(
12
).
html_safe?
end
end
def
test_remove
def
test_remove
assert_equal
"Summer"
,
"Fast Summer"
.
remove
(
/Fast /
)
assert_equal
"Summer"
,
"Fast Summer"
.
remove
(
/Fast /
)
assert_equal
"Summer"
,
"Fast Summer"
.
remove!
(
/Fast /
)
assert_equal
"Summer"
,
"Fast Summer"
.
remove!
(
/Fast /
)
...
...
activesupport/test/inflector_test.rb
浏览文件 @
f8e5022c
...
@@ -287,6 +287,12 @@ def test_humanize
...
@@ -287,6 +287,12 @@ def test_humanize
end
end
end
end
def
test_humanize_without_capitalize
UnderscoreToHumanWithoutCapitalize
.
each
do
|
underscore
,
human
|
assert_equal
(
human
,
ActiveSupport
::
Inflector
.
humanize
(
underscore
,
capitalize:
false
))
end
end
def
test_humanize_by_rule
def
test_humanize_by_rule
ActiveSupport
::
Inflector
.
inflections
do
|
inflect
|
ActiveSupport
::
Inflector
.
inflections
do
|
inflect
|
inflect
.
human
(
/_cnt$/i
,
'\1_count'
)
inflect
.
human
(
/_cnt$/i
,
'\1_count'
)
...
...
activesupport/test/inflector_test_cases.rb
浏览文件 @
f8e5022c
...
@@ -212,6 +212,12 @@ module InflectorTestCases
...
@@ -212,6 +212,12 @@ module InflectorTestCases
"underground"
=>
"Underground"
"underground"
=>
"Underground"
}
}
UnderscoreToHumanWithoutCapitalize
=
{
"employee_salary"
=>
"employee salary"
,
"employee_id"
=>
"employee"
,
"underground"
=>
"underground"
}
MixtureToTitleCase
=
{
MixtureToTitleCase
=
{
'active_record'
=>
'Active Record'
,
'active_record'
=>
'Active Record'
,
'ActiveRecord'
=>
'Active Record'
,
'ActiveRecord'
=>
'Active Record'
,
...
...
guides/source/active_support_core_extensions.md
浏览文件 @
f8e5022c
...
@@ -1772,6 +1772,12 @@ The method `humanize` gives you a sensible name for display out of an attribute
...
@@ -1772,6 +1772,12 @@ The method `humanize` gives you a sensible name for display out of an attribute
"comments_count"
.
humanize
# => "Comments count"
"comments_count"
.
humanize
# => "Comments count"
```
```
The capitalization of the first word can be turned off by setting the optional parameter
`capitalize`
to false:
```
ruby
"author_id"
.
humanize
(
capitalize:
false
)
# => "author"
```
The helper method
`full_messages`
uses
`humanize`
as a fallback to include attribute names:
The helper method
`full_messages`
uses
`humanize`
as a fallback to include attribute names:
```
ruby
```
ruby
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录