Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
95ef9bd6
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,发现更多精彩内容 >>
提交
95ef9bd6
编写于
6月 29, 2009
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplifying resource generator.
上级
ff4c6004
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
52 addition
and
32 deletion
+52
-32
railties/lib/generators/base.rb
railties/lib/generators/base.rb
+44
-4
railties/lib/generators/rails/resource/resource_generator.rb
railties/lib/generators/rails/resource/resource_generator.rb
+4
-19
railties/test/generators/app_generator_test.rb
railties/test/generators/app_generator_test.rb
+2
-2
railties/test/generators/plugin_generator_test.rb
railties/test/generators/plugin_generator_test.rb
+2
-1
railties/test/generators/resource_generator_test.rb
railties/test/generators/resource_generator_test.rb
+0
-6
未找到文件。
railties/lib/generators/base.rb
浏览文件 @
95ef9bd6
...
...
@@ -91,13 +91,29 @@ def self.namespace(name=nil)
#
# ruby script/generate controller Account --no-test-framework
#
def
self
.
hook_for
(
*
names
)
# ==== Custom invocations
#
# You can also supply a block to hook for to customize how the hook is
# going to be invoked. The block receives two parameters, an instance
# of the current class and the klass to be invoked.
#
# For example, in the resource generator, the controller should be invoked
# with a pluralized class name. By default, it is invoked with the same
# name as the resource generator, which is singular. To change this, we
# can give a block to customize how the controller can be invoked.
#
# hook_for :resource_controller do |instance, controller|
# instance.invoke controller, [ instance.name.pluralize ]
# end
#
def
self
.
hook_for
(
*
names
,
&
block
)
default_class_options
(
*
names
)
options
=
names
.
extract_options!
verbose
=
options
.
fetch
(
:verbose
,
:blue
)
names
.
each
do
|
name
|
invocations
<<
[
name
,
base_name
,
generator_name
]
invocation_blocks
[
name
]
=
block
if
block_given?
class_eval
<<-
METHOD
,
__FILE__
,
__LINE__
def invoke_for_
#{
name
}
...
...
@@ -108,7 +124,7 @@ def invoke_for_#{name}
if klass
say_status :invoke, options[
#{
name
.
inspect
}
],
#{
verbose
.
inspect
}
invoke klass
invoke
_class_with_block
#{
name
.
inspect
}
,
klass
else
say "Could not find and invoke '
\#
{options[
#{
name
.
inspect
}
]}'."
end
...
...
@@ -140,13 +156,19 @@ def invoke_for_#{name}
#
# "rails:generators:webrat", "webrat:generators:controller", "webrat"
#
def
self
.
invoke_if
(
*
names
)
# ==== Custom invocations
#
# This method accepts custom invocations as in hook_for. Check hook_for
# for usage and examples.
#
def
self
.
invoke_if
(
*
names
,
&
block
)
conditional_class_options
(
*
names
)
options
=
names
.
extract_options!
verbose
=
options
.
fetch
(
:verbose
,
:blue
)
names
.
each
do
|
name
|
invocations
<<
[
name
,
base_name
,
generator_name
]
invocation_blocks
[
name
]
=
block
if
block_given?
class_eval
<<-
METHOD
,
__FILE__
,
__LINE__
def invoke_if_
#{
name
}
...
...
@@ -157,7 +179,7 @@ def invoke_if_#{name}
if klass
say_status :invoke,
#{
name
.
inspect
}
,
#{
verbose
.
inspect
}
invoke klass
invoke
_class_with_block
#{
name
.
inspect
}
,
klass
else
say "Could not find and invoke '
#{
name
}
'."
end
...
...
@@ -168,6 +190,18 @@ def invoke_if_#{name}
protected
# This is the common method that both hook_for and invoke_if use to
# invoke a class. It searches for a block in the invocation blocks
# in case the user wants to customize how the class is invoked.
#
def
invoke_class_with_block
(
name
,
klass
)
#:nodoc:
if
block
=
self
.
class
.
invocation_blocks
[
name
]
block
.
call
(
self
,
klass
)
else
invoke
klass
end
end
# Check whether the given class names are already taken by user
# application or Ruby on Rails.
#
...
...
@@ -229,6 +263,12 @@ def self.invocations #:nodoc:
@invocations
||=
from_superclass
(
:invocations
,
[])
end
# Stores invocation blocks used on hook_for and invoke_if.
#
def
self
.
invocation_blocks
#:nodoc:
@invocation_blocks
||=
from_superclass
(
:invocation_blocks
,
{})
end
# Creates a conditional class option with type boolean, default value
# lookup and default description.
#
...
...
railties/lib/generators/rails/resource/resource_generator.rb
浏览文件 @
95ef9bd6
...
...
@@ -3,7 +3,9 @@
module
Rails
module
Generators
class
ResourceGenerator
<
ModelGenerator
hook_for
:resource_controller
hook_for
:resource_controller
do
|
base
,
controller
|
base
.
invoke
controller
,
[
base
.
name
.
pluralize
,
base
.
options
[
:actions
]
]
end
class_option
:actions
,
:type
=>
:array
,
:default
=>
[],
:banner
=>
"ACTION ACTION"
,
:desc
=>
"Actions for the resource controller"
,
:aliases
=>
"-a"
...
...
@@ -11,25 +13,8 @@ class ResourceGenerator < ModelGenerator
class_option
:singleton
,
:type
=>
:boolean
,
:default
=>
false
,
:aliases
=>
"-i"
,
:desc
=>
"Supply to create a singleton controller"
def
invoke_for_resource_controller
return
unless
options
[
:resource_controller
]
klass
=
Rails
::
Generators
.
find_by_namespace
(
options
[
:resource_controller
],
:rails
,
:controller
)
if
klass
args
=
[]
args
<<
pluralize?
(
class_name
)
args
<<
options
[
:actions
]
say_status
:invoke
,
options
[
:resource_controller
],
:blue
klass
.
new
(
args
,
options
.
dup
,
_overrides_config
).
invoke
(
:all
)
else
say
"Could not find and invoke '
#{
options
[
:resource_controller
]
}
'."
end
end
def
add_resource_route
route
"map.resource
#{
"s"
unless
options
[
:singleton
]
}
:
#{
pluralize?
(
file_name
)
}
"
route
"map.resource
#{
:s
unless
options
[
:singleton
]
}
:
#{
pluralize?
(
file_name
)
}
"
end
protected
...
...
railties/test/generators/app_generator_test.rb
浏览文件 @
95ef9bd6
...
...
@@ -111,7 +111,7 @@ def test_shebang_is_added_to_files
def
test_rails_is_frozen
generator
(
:freeze
=>
true
,
:database
=>
"sqlite3"
).
expects
(
:run
).
with
(
"rake rails:freeze:edge"
,
false
)
silence
(
:stdout
){
generator
.
invoke
(
:all
)
}
silence
(
:stdout
){
generator
.
invoke
}
assert_file
'config/environment.rb'
,
/# RAILS_GEM_VERSION/
end
...
...
@@ -127,7 +127,7 @@ def test_template_is_executed_when_supplied
template
.
instance_eval
"def read; self; end"
# Make the string respond to read
generator
(
:template
=>
path
,
:database
=>
"sqlite3"
).
expects
(
:open
).
with
(
path
).
returns
(
template
)
assert_match
/It works!/
,
silence
(
:stdout
){
generator
.
invoke
(
:all
)
}
assert_match
/It works!/
,
silence
(
:stdout
){
generator
.
invoke
}
end
def
test_usage_read_from_file
...
...
railties/test/generators/plugin_generator_test.rb
浏览文件 @
95ef9bd6
...
...
@@ -22,7 +22,8 @@ def test_check_class_collision
def
test_invokes_default_test_framework
run_generator
assert_file
"vendor/plugins/plugin_fu/test/plugin_fu_test.rb"
assert_file
"vendor/plugins/plugin_fu/test/plugin_fu_test.rb"
,
/class PluginFuTest < ActiveSupport::TestCase/
assert_file
"vendor/plugins/plugin_fu/test/test_helper.rb"
end
def
test_logs_if_the_test_framework_cannot_be_found
...
...
railties/test/generators/resource_generator_test.rb
浏览文件 @
95ef9bd6
...
...
@@ -81,12 +81,6 @@ def test_resource_routes_are_added
def
test_singleton_resource
run_generator
[
"account"
,
"--singleton"
]
assert_file
"app/controllers/account_controller.rb"
,
/class AccountController < ApplicationController/
assert_file
"test/functional/account_controller_test.rb"
,
/class AccountControllerTest < ActionController::TestCase/
assert_file
"app/helpers/account_helper.rb"
,
/module AccountHelper/
assert_file
"test/unit/helpers/account_helper_test.rb"
,
/class AccountHelperTest < ActionView::TestCase/
assert_file
"config/routes.rb"
do
|
route
|
assert_match
/map\.resource :account$/
,
route
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录