Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
0efedf2a
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,发现更多精彩内容 >>
提交
0efedf2a
编写于
8月 30, 2009
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ensure scaffold works properly even if plural name is given. [#3062]
上级
5096ba96
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
137 addition
and
81 deletion
+137
-81
railties/lib/generators/base.rb
railties/lib/generators/base.rb
+49
-3
railties/lib/generators/erb/scaffold/scaffold_generator.rb
railties/lib/generators/erb/scaffold/scaffold_generator.rb
+2
-1
railties/lib/generators/named_base.rb
railties/lib/generators/named_base.rb
+0
-62
railties/lib/generators/rails/resource/resource_generator.rb
railties/lib/generators/rails/resource/resource_generator.rb
+5
-11
railties/lib/generators/rails/scaffold/scaffold_generator.rb
railties/lib/generators/rails/scaffold/scaffold_generator.rb
+2
-1
railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
...ails/scaffold_controller/scaffold_controller_generator.rb
+3
-2
railties/lib/generators/resource_helpers.rb
railties/lib/generators/resource_helpers.rb
+74
-0
railties/lib/generators/test_unit/scaffold/scaffold_generator.rb
...s/lib/generators/test_unit/scaffold/scaffold_generator.rb
+2
-1
未找到文件。
railties/lib/generators/base.rb
浏览文件 @
0efedf2a
...
...
@@ -9,6 +9,8 @@ class Base < Thor::Group
include
Thor
::
Actions
include
Rails
::
Generators
::
Actions
add_runtime_options!
# Automatically sets the source root based on the class name.
#
def
self
.
source_root
...
...
@@ -45,8 +47,10 @@ def self.namespace(name=nil)
#
# ==== Examples
#
# class ControllerGenerator < Rails::Generators::Base
# hook_for :test_framework, :aliases => "-t"
# module Rails::Generators
# class ControllerGenerator < Base
# hook_for :test_framework, :aliases => "-t"
# end
# end
#
# The example above will create a test framework option and will invoke
...
...
@@ -64,7 +68,49 @@ def self.namespace(name=nil)
# invoked. This allows any test framework to hook into Rails as long as it
# provides any of the hooks above.
#
# Finally, if the user don't want to use any test framework, he can do:
# ==== Options
#
# This lookup can be customized with two options: :base and :as. The first
# is the root module value and in the example above defaults to "rails".
# The later defaults to the generator name, without the "Generator" ending.
#
# Let's suppose you are creating a generator that needs to invoke the
# controller generator from test unit. Your first attempt is:
#
# class AwesomeGenerator < Rails::Generators::Base
# hook_for :test_framework
# end
#
# The lookup in this case for test_unit as input is:
#
# "test_unit:generators:awesome", "test_unit"
#
# Which is not the desired the lookup. You can change it by providing the
# :as option:
#
# class AwesomeGenerator < Rails::Generators::Base
# hook_for :test_framework, :as => :controller
# end
#
# And now it will lookup at:
#
# "test_unit:generators:awesome", "test_unit"
#
# Similarly, if you want it to also lookup in the rails namespace, you just
# need to provide the :base value:
#
# class AwesomeGenerator < Rails::Generators::Base
# hook_for :test_framework, :base => :rails, :as => :controller
# end
#
# And the lookup is exactly the same as previously:
#
# "rails:generators:test_unit", "test_unit:generators:controller", "test_unit"
#
# ==== Switches
#
# All hooks come with switches for user interface. If the user don't want
# to use any test framework, he can do:
#
# ruby script/generate controller Account --skip-test-framework
#
...
...
railties/lib/generators/erb/scaffold/scaffold_generator.rb
浏览文件 @
0efedf2a
require
'generators/erb'
require
'generators/resource_helpers'
module
Erb
module
Generators
class
ScaffoldGenerator
<
Base
include
Rails
::
Generators
::
ScaffoldBase
include
Rails
::
Generators
::
ResourceHelpers
argument
:attributes
,
:type
=>
:array
,
:default
=>
[],
:banner
=>
"field:type field:type"
...
...
railties/lib/generators/named_base.rb
浏览文件 @
0efedf2a
...
...
@@ -97,67 +97,5 @@ def self.check_class_collision(options={})
end
end
end
# Deal with controller names on scaffold. Also provide helpers to deal with
# ActionORM.
#
module
ScaffoldBase
def
self
.
included
(
base
)
#:nodoc:
base
.
send
:attr_reader
,
:controller_name
,
:controller_class_name
,
:controller_file_name
,
:controller_class_path
,
:controller_file_path
end
# Set controller variables on initialization.
#
def
initialize
(
*
args
)
#:nodoc:
super
@controller_name
=
name
.
pluralize
base_name
,
@controller_class_path
,
@controller_file_path
,
class_nesting
,
class_nesting_depth
=
extract_modules
(
@controller_name
)
class_name_without_nesting
,
@controller_file_name
,
controller_plural_name
=
inflect_names
(
base_name
)
@controller_class_name
=
if
class_nesting
.
empty?
class_name_without_nesting
else
"
#{
class_nesting
}
::
#{
class_name_without_nesting
}
"
end
end
protected
# Loads the ORM::Generators::ActiveModel class. This class is responsable
# to tell scaffold entities how to generate an specific method for the
# ORM. Check Rails::Generators::ActiveModel for more information.
#
def
orm_class
@orm_class
||=
begin
# Raise an error if the class_option :orm was not defined.
unless
self
.
class
.
class_options
[
:orm
]
raise
"You need to have :orm as class option to invoke orm_class and orm_instance"
end
action_orm
=
"
#{
options
[
:orm
].
to_s
.
classify
}
::Generators::ActiveModel"
# If the orm was not loaded, try to load it at "generators/orm",
# for example "generators/active_record" or "generators/sequel".
begin
klass
=
action_orm
.
constantize
rescue
NameError
require
"generators/
#{
options
[
:orm
]
}
"
end
# Try once again after loading the file with success.
klass
||=
action_orm
.
constantize
rescue
Exception
=>
e
raise
Error
,
"Could not load
#{
action_orm
}
, skipping controller. Error:
#{
e
.
message
}
."
end
end
# Initialize ORM::Generators::ActiveModel to access instance methods.
#
def
orm_instance
(
name
=
file_name
)
@orm_instance
||=
@orm_class
.
new
(
name
)
end
end
end
end
railties/lib/generators/rails/resource/resource_generator.rb
浏览文件 @
0efedf2a
require
'generators/rails/model/model_generator'
require
'generators/resource_helpers'
module
Rails
module
Generators
class
ResourceGenerator
<
ModelGenerator
#metagenerator
include
ResourceHelpers
hook_for
:resource_controller
,
:required
=>
true
do
|
base
,
controller
|
base
.
invoke
controller
,
[
base
.
name
.
pluraliz
e
,
base
.
options
[
:actions
]
]
base
.
invoke
controller
,
[
base
.
controller_nam
e
,
base
.
options
[
:actions
]
]
end
class_option
:actions
,
:type
=>
:array
,
:banner
=>
"ACTION ACTION"
,
:default
=>
[],
:desc
=>
"Actions for the resource controller"
class_option
:singleton
,
:type
=>
:boolean
,
:desc
=>
"Supply to create a singleton controller"
class_option
:force_plural
,
:type
=>
:boolean
,
:desc
=>
"Forces the use of a plural ModelName"
def
initialize
(
*
args
)
super
if
name
==
name
.
pluralize
&&
!
options
[
:force_plural
]
say
"Plural version of the model detected, using singularized version. Override with --force-plural."
name
.
replace
name
.
singularize
end
end
class_option
:singleton
,
:type
=>
:boolean
,
:desc
=>
"Supply to create a singleton controller"
def
add_resource_route
route
"map.resource
#{
:s
unless
options
[
:singleton
]
}
:
#{
pluralize?
(
file_name
)
}
"
...
...
railties/lib/generators/rails/scaffold/scaffold_generator.rb
浏览文件 @
0efedf2a
...
...
@@ -3,7 +3,8 @@
module
Rails
module
Generators
class
ScaffoldGenerator
<
ResourceGenerator
#metagenerator
remove_hook_for
:actions
,
:resource_controller
remove_hook_for
:resource_controller
remove_class_option
:actions
hook_for
:scaffold_controller
,
:required
=>
true
hook_for
:stylesheets
...
...
railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
浏览文件 @
0efedf2a
require
'generators/resource_helpers'
module
Rails
module
Generators
class
ScaffoldControllerGenerator
<
NamedBase
# Add controller methods and ActionORM settings.
include
ScaffoldBase
include
ResourceHelpers
check_class_collision
:suffix
=>
"Controller"
...
...
railties/lib/generators/resource_helpers.rb
0 → 100644
浏览文件 @
0efedf2a
module
Rails
module
Generators
# Deal with controller names on scaffold and add some helpers to deal with
# ActiveModel.
#
module
ResourceHelpers
def
self
.
included
(
base
)
#:nodoc:
base
.
send
:attr_reader
,
:controller_name
,
:controller_class_name
,
:controller_file_name
,
:controller_class_path
,
:controller_file_path
base
.
send
:class_option
,
:force_plural
,
:type
=>
:boolean
,
:desc
=>
"Forces the use of a plural ModelName"
end
# Set controller variables on initialization.
#
def
initialize
(
*
args
)
#:nodoc:
super
if
name
==
name
.
pluralize
&&
!
options
[
:force_plural
]
say
"Plural version of the model detected, using singularized version. Override with --force-plural."
name
.
replace
name
.
singularize
assign_names!
(
self
.
name
)
end
@controller_name
=
name
.
pluralize
base_name
,
@controller_class_path
,
@controller_file_path
,
class_nesting
,
class_nesting_depth
=
extract_modules
(
@controller_name
)
class_name_without_nesting
,
@controller_file_name
,
controller_plural_name
=
inflect_names
(
base_name
)
@controller_class_name
=
if
class_nesting
.
empty?
class_name_without_nesting
else
"
#{
class_nesting
}
::
#{
class_name_without_nesting
}
"
end
end
protected
# Loads the ORM::Generators::ActiveModel class. This class is responsable
# to tell scaffold entities how to generate an specific method for the
# ORM. Check Rails::Generators::ActiveModel for more information.
#
def
orm_class
@orm_class
||=
begin
# Raise an error if the class_option :orm was not defined.
unless
self
.
class
.
class_options
[
:orm
]
raise
"You need to have :orm as class option to invoke orm_class and orm_instance"
end
active_model
=
"
#{
options
[
:orm
].
to_s
.
classify
}
::Generators::ActiveModel"
# If the orm was not loaded, try to load it at "generators/orm",
# for example "generators/active_record" or "generators/sequel".
begin
klass
=
active_model
.
constantize
rescue
NameError
require
"generators/
#{
options
[
:orm
]
}
"
end
# Try once again after loading the file with success.
klass
||=
active_model
.
constantize
rescue
Exception
=>
e
raise
Error
,
"Could not load
#{
active_model
}
, skipping controller. Error:
#{
e
.
message
}
."
end
end
# Initialize ORM::Generators::ActiveModel to access instance methods.
#
def
orm_instance
(
name
=
file_name
)
@orm_instance
||=
@orm_class
.
new
(
name
)
end
end
end
end
railties/lib/generators/test_unit/scaffold/scaffold_generator.rb
浏览文件 @
0efedf2a
require
'generators/test_unit'
require
'generators/resource_helpers'
module
TestUnit
module
Generators
class
ScaffoldGenerator
<
Base
include
Rails
::
Generators
::
ScaffoldBase
include
Rails
::
Generators
::
ResourceHelpers
class_option
:singleton
,
:type
=>
:boolean
,
:desc
=>
"Supply to create a singleton controller"
check_class_collision
:suffix
=>
"ControllerTest"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录