我正在尝试自定义 Railsscaffold 命令,以便当我运行时
rails generate scaffold api/v1/model_name
Run Code Online (Sandbox Code Playgroud)
它仍然生成文件夹结构,但不会将 api_v1_ 添加到视图、测试等中。
我了解如何从这篇文章中覆盖和添加新的部分: 覆盖 Rails 脚手架生成器,但我想覆盖测试、工厂和视图。
我正在制作一个自定义生成器,生成一个新的rails应用程序,我这样做
require 'thor'
require 'rails/generators/rails/app/app_generator'
class AppBuilder < Rails::AppBuilder
include Thor::Actions
include Thor::Shell
...
end
Run Code Online (Sandbox Code Playgroud)
问题是,我如何添加一个新的源目录(然后使用Thor::Actions#copy_file,Thor::Actions#template,和其他人)?我在Thor的文档中看到了Thor::Actions#source_paths保存源代码(它是一个路径数组),所以我尝试在我的课程中覆盖它(因为我已经包括在内Thor::Actions):
def source_paths
[File.join(File.expand_path(File.dirname(__FILE__)), "templates")] + super
end
Run Code Online (Sandbox Code Playgroud)
有了这个,我想./templates在源代码中添加目录,同时仍然保持Rails的一个(这就是+ super最后的原因).但它不起作用,它仍然将Rails的源路径列为唯一的路径.
我尝试浏览Rails的源代码,但我找不到Rails如何将他的目录放在源路径中.我真的想知道:)