如何从自定义生成器调用 Rails 模型生成器
我正在开始编写自己的生成器。我终于看到,当我创建 Rails 应用程序时,我做了很多相同的事情。
这是我的发电机到目前为止的样子。
class FbScaffoldGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :model_name, :type => :string, :default => "fb_item"
def create_models
end
private
def mod_name
model_name.underscore
end
end
正如你所知,我还没有走得太远。在 create_models 方法中,我想获取传入的 model_name 并调用 Rails 模型生成器,向其传递名称并在此过程中定义一些字段。我还将在执行此操作的同时创建几个其他模型,因此了解如何调用任何预定义的生成器将非常有帮助。
I am getting started writing my own generators. I am finally to the place where I am seeing that I do a lot of the same things when I create rails apps.
Here is what my generator looks like so far.
class FbScaffoldGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :model_name, :type => :string, :default => "fb_item"
def create_models
end
private
def mod_name
model_name.underscore
end
end
as you can tell I haven't gotten far. In the create_models method I would like to take the model_name passed in and invoke the rails model generator, pass it the name and also define some fields along the way. I will also create a couple other models at the same time I am doing this so knowing how to invoke any of the pre defined generators will be really helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
如果您想跳过某些部分(例如固定装置),则可以
use
if you want to skip some parts (say, fixtures) you can do