在一个生成器内运行其他生成器
我创建了一个脚手架 gem,它使用另一个 gem 来生成演示者。我使用 Rails 3 生成器创建了这个脚手架生成器。它的工作原理如下:
rails g stager:scaffold User
我创建了另一个 gem 来生成演示者。我想使用这个 gem 为每个脚手架生成一个演示者。目前我正在执行以下操作来完成此操作:
run "rails g exhibit:presenter #{scaffold_name}"
问题是,当调用此生成器时,它似乎会重新加载 Rails 环境,这使得它有点慢。我想知道是否有更好的方法在生成器中调用另一个生成器。那么:有没有更好的方法来创建 Rails 生成器 inception? ;)
I created a scaffolding gem that uses another gem to generate presenters. I created this scaffolding generator using the Rails 3 generators. It works like this:
rails g stager:scaffold User
I created another gem to generate presenters. I want to use this gem to generate a presenter with each scaffold. Currently I'm doing the following to accomplish this:
run "rails g exhibit:presenter #{scaffold_name}"
The problem is that it seems to reload the Rails environment when this generator is called, making it a bit slow. I'm wondering if there's a better way to call another generator within a generator. So: is there a better way to create a Rails generator inception? ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在生成器类中为其他生成器定义一个
hook_for
。 ScaffoldGenerator 可能是最好的例子,因为它 有一个用于scaffold_controller
生成器的钩子。如果你想通过在生成器的参数中,最好使用类似于此文件中的
hook_for
示例的资源和样式表引擎。You must define a
hook_for
the other generator in your generator class. TheScaffoldGenerator
is probably the best example of this, as it has a hook for thescaffold_controller
generator.If you want to pass in arguments to your generator, it's probably better to use something like the
hook_for
examples further down in this file for assets and stylesheet engine.