Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2f3681df
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,体验更适合开发者的 AI 搜索 >>
提交
2f3681df
编写于
6月 26, 2009
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Clean up class collisions check and a class method helper.
上级
6bd10118
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
62 addition
and
46 deletion
+62
-46
railties/lib/generators/base.rb
railties/lib/generators/base.rb
+2
-3
railties/lib/generators/named_base.rb
railties/lib/generators/named_base.rb
+39
-5
railties/lib/generators/rails/helper/helper_generator.rb
railties/lib/generators/rails/helper/helper_generator.rb
+1
-3
railties/lib/generators/rails/integration_test/integration_test_generator.rb
...tors/rails/integration_test/integration_test_generator.rb
+1
-3
railties/lib/generators/rails/mailer/mailer_generator.rb
railties/lib/generators/rails/mailer/mailer_generator.rb
+1
-4
railties/lib/generators/rails/metal/metal_generator.rb
railties/lib/generators/rails/metal/metal_generator.rb
+1
-3
railties/lib/generators/rails/observer/observer_generator.rb
railties/lib/generators/rails/observer/observer_generator.rb
+1
-3
railties/lib/generators/rails/performance_test/performance_test_generator.rb
...tors/rails/performance_test/performance_test_generator.rb
+7
-8
railties/lib/generators/rails/plugin/plugin_generator.rb
railties/lib/generators/rails/plugin/plugin_generator.rb
+1
-3
railties/lib/generators/test_unit.rb
railties/lib/generators/test_unit.rb
+1
-0
railties/lib/generators/test_unit/helper/helper_generator.rb
railties/lib/generators/test_unit/helper/helper_generator.rb
+1
-3
railties/lib/generators/test_unit/mailer/mailer_generator.rb
railties/lib/generators/test_unit/mailer/mailer_generator.rb
+0
-4
railties/lib/generators/test_unit/observer/observer_generator.rb
...s/lib/generators/test_unit/observer/observer_generator.rb
+0
-4
railties/test/generators/helper_generator_test.rb
railties/test/generators/helper_generator_test.rb
+6
-0
未找到文件。
railties/lib/generators/base.rb
浏览文件 @
2f3681df
...
...
@@ -45,9 +45,8 @@ def self.namespace(name=nil)
protected
# Check whether the given class names are already taken by Ruby or Rails.
# In the future, expand to check other namespaces such as the rest of
# the user's app.
# Check whether the given class names are already taken by user
# application or Ruby on Rails.
#
def
class_collisions
(
*
class_names
)
return
unless
behavior
==
:invoke
...
...
railties/lib/generators/named_base.rb
浏览文件 @
2f3681df
...
...
@@ -11,15 +11,43 @@ class NamedBase < Base
alias
:file_name
:singular_name
def
initialize
(
*
args
)
class
<<
self
# Add a class collisions name to be checked on class initialization. You
# can supply a hash with a :prefix or :suffix to be tested.
#
# ==== Examples
#
# check_class_collision :suffix => "Observer"
#
# If the generator is invoked with class name Admin, it will check for
# the presence of "AdminObserver".
#
def
check_class_collision
(
options
=
{})
@class_collisions
=
options
end
# Returns the class collisions for this class and retreives one from
# superclass. The from_superclass method used below is from Thor.
#
def
class_collisions
#:nodoc:
@class_collisions
||=
from_superclass
(
:class_collisions
,
nil
)
end
end
def
initialize
(
*
args
)
#:nodoc:
super
assign_names!
(
self
.
name
)
parse_attributes!
if
respond_to?
(
:attributes
)
if
self
.
class
.
class_collisions
value
=
add_prefix_and_suffix
(
class_name
,
self
.
class
.
class_collisions
)
class_collisions
(
value
)
end
end
protected
def
assign_names!
(
given_name
)
def
assign_names!
(
given_name
)
#:nodoc:
self
.
name
,
@class_path
,
@file_path
,
@class_nesting
,
@class_nesting_depth
=
extract_modules
(
given_name
)
@class_name_without_nesting
,
@singular_name
,
@plural_name
=
inflect_names
(
self
.
name
)
...
...
@@ -40,7 +68,7 @@ def assign_names!(given_name)
# Convert attributes hash into an array with GeneratedAttribute objects.
#
def
parse_attributes!
def
parse_attributes!
#:nodoc:
attributes
.
map!
do
|
name
,
type
|
Rails
::
Generator
::
GeneratedAttribute
.
new
(
name
,
type
)
end
...
...
@@ -49,7 +77,7 @@ def parse_attributes!
# Extract modules from filesystem-style or ruby-style path. Both
# good/fun/stuff and Good::Fun::Stuff produce the same results.
#
def
extract_modules
(
name
)
def
extract_modules
(
name
)
#:nodoc:
modules
=
name
.
include?
(
'/'
)
?
name
.
split
(
'/'
)
:
name
.
split
(
'::'
)
name
=
modules
.
pop
path
=
modules
.
map
{
|
m
|
m
.
underscore
}
...
...
@@ -62,13 +90,19 @@ def extract_modules(name)
# Receives name and return camelized, underscored and pluralized names.
#
def
inflect_names
(
name
)
def
inflect_names
(
name
)
#:nodoc:
camel
=
name
.
camelize
under
=
camel
.
underscore
plural
=
under
.
pluralize
[
camel
,
under
,
plural
]
end
# Receives a name and add suffix and prefix values frrm hash.
#
def
add_prefix_and_suffix
(
name
,
hash
)
#:nodoc:
"
#{
hash
[
:prefix
]
}#{
name
}#{
hash
[
:suffix
]
}
"
end
end
end
end
railties/lib/generators/rails/helper/helper_generator.rb
浏览文件 @
2f3681df
module
Rails
module
Generators
class
HelperGenerator
<
NamedBase
def
check_class_collisions
class_collisions
"
#{
class_name
}
Helper"
end
check_class_collision
:suffix
=>
"Helper"
def
create_helper_files
template
'helper.rb'
,
File
.
join
(
'app/helpers'
,
class_path
,
"
#{
file_name
}
_helper.rb"
)
...
...
railties/lib/generators/rails/integration_test/integration_test_generator.rb
浏览文件 @
2f3681df
module
Rails
module
Generators
class
IntegrationTestGenerator
<
NamedBase
def
check_class_collisions
class_collisions
"
#{
class_name
}
Test"
end
check_class_collisions
:suffix
=>
"Test"
def
create_test_files
template
'integration_test.rb'
,
File
.
join
(
'test/integration'
,
class_path
,
"
#{
file_name
}
_test.rb"
)
...
...
railties/lib/generators/rails/mailer/mailer_generator.rb
浏览文件 @
2f3681df
...
...
@@ -2,10 +2,7 @@ module Rails
module
Generators
class
MailerGenerator
<
NamedBase
argument
:actions
,
:type
=>
:array
,
:default
=>
[],
:banner
=>
"method method"
def
check_class_collision
class_collisions
class_name
end
check_class_collision
def
create_mailer_file
template
"mailer.rb"
,
File
.
join
(
'app/models'
,
class_path
,
"
#{
file_name
}
.rb"
)
...
...
railties/lib/generators/rails/metal/metal_generator.rb
浏览文件 @
2f3681df
module
Rails
module
Generators
class
MetalGenerator
<
NamedBase
def
check_class_collision
class_collisions
class_name
end
check_class_collision
def
create_file
template
"metal.rb"
,
"app/metal/
#{
file_name
}
.rb"
...
...
railties/lib/generators/rails/observer/observer_generator.rb
浏览文件 @
2f3681df
module
Rails
module
Generators
class
ObserverGenerator
<
NamedBase
def
check_class_collision
class_collisions
"
#{
class_name
}
Observer"
end
check_class_collision
:suffix
=>
"Observer"
def
create_observer_file
template
'observer.rb'
,
File
.
join
(
'app/models'
,
class_path
,
"
#{
file_name
}
_observer.rb"
)
...
...
railties/lib/generators/rails/performance_test/performance_test_generator.rb
浏览文件 @
2f3681df
module
Rails
module
Generators
class
PerformanceTestGenerator
<
NamedBase
def
check_class_collisions
class_collisions
"
#{
class_name
}
Test"
end
class
PerformanceTestGenerator
<
NamedBase
check_class_collision
:suffix
=>
"Test"
def
create_test_files
template
'performance_test.rb'
,
File
.
join
(
'test/performance'
,
class_path
,
"
#{
file_name
}
_test.rb"
)
end
end
end
def
create_test_files
template
'performance_test.rb'
,
File
.
join
(
'test/performance'
,
class_path
,
"
#{
file_name
}
_test.rb"
)
end
end
end
end
railties/lib/generators/rails/plugin/plugin_generator.rb
浏览文件 @
2f3681df
...
...
@@ -7,9 +7,7 @@ class PluginGenerator < NamedBase
class_option
:with_generator
,
:type
=>
:boolean
,
:aliases
=>
"-g"
,
:default
=>
false
,
:desc
=>
"When supplied creates generator base files."
def
check_class_collision
class_collisions
class_name
end
check_class_collision
def
create_root
self
.
root
=
File
.
expand_path
(
"vendor/plugins/
#{
file_name
}
"
,
root
)
...
...
railties/lib/generators/test_unit.rb
浏览文件 @
2f3681df
...
...
@@ -3,6 +3,7 @@
module
TestUnit
module
Generators
class
Base
<
Rails
::
Generators
::
NamedBase
check_class_collision
:suffix
=>
"Test"
end
end
end
railties/lib/generators/test_unit/helper/helper_generator.rb
浏览文件 @
2f3681df
module
TestUnit
module
Generators
class
HelperGenerator
<
Base
def
check_class_collisions
class_collisions
"
#{
class_name
}
HelperTest"
end
check_class_collision
:suffix
=>
"HelperTest"
def
create_helper_files
template
'helper_test.rb'
,
File
.
join
(
'test/unit/helpers'
,
class_path
,
"
#{
file_name
}
_helper_test.rb"
)
...
...
railties/lib/generators/test_unit/mailer/mailer_generator.rb
浏览文件 @
2f3681df
...
...
@@ -3,10 +3,6 @@ module Generators
class
MailerGenerator
<
Base
argument
:actions
,
:type
=>
:array
,
:default
=>
[],
:banner
=>
"method method"
def
check_class_collisions
class_collisions
"
#{
class_name
}
Test"
end
def
create_test_files
template
"unit_test.rb"
,
File
.
join
(
'test'
,
'unit'
,
class_path
,
"
#{
file_name
}
_test.rb"
)
end
...
...
railties/lib/generators/test_unit/observer/observer_generator.rb
浏览文件 @
2f3681df
module
TestUnit
module
Generators
class
ObserverGenerator
<
Base
def
check_class_collisions
class_collisions
class_name
,
"
#{
class_name
}
Test"
end
def
create_test_files
template
'unit_test.rb'
,
File
.
join
(
'test'
,
'unit'
,
class_path
,
"
#{
file_name
}
_observer_test.rb"
)
end
...
...
railties/test/generators/helper_generator_test.rb
浏览文件 @
2f3681df
...
...
@@ -4,6 +4,7 @@
require
'generators/test_unit/helper/helper_generator'
ObjectHelper
=
Class
.
new
AnotherObjectHelperTest
=
Class
.
new
class
HelperGeneratorTest
<
GeneratorsTestCase
...
...
@@ -27,6 +28,11 @@ def test_check_class_collision
assert_match
/The name 'ObjectHelper' is either already used in your application or reserved/
,
content
end
def
test_check_class_collision_on_tests
content
=
capture
(
:stderr
){
run_generator
[
"another_object"
]
}
assert_match
/The name 'AnotherObjectHelperTest' is either already used in your application or reserved/
,
content
end
protected
def
run_generator
(
args
=
[
"admin"
])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录