生成和定制Rails生成器和模板 Creating and Customizing Rails Generators & Templates2

简介: 生成和定制Rails生成器和模板 Creating and Customizing Rails Generators & Templates




7 加生成器撤退


生成器的最后一个特性是对插件生成器非常有用的,就是回滚。比如,想象在TestUnit上加一个特性,像shoulda(http://github.com/thoughtbot/shoulda)一样。


因为TestUnit实现了Rails需要的所有生成器,shoulda只是想重写其中的一部分,不需全部覆盖,它就可以告诉Rails如果在Shoulda命名空间里找不到生成器,就用TestUnit生成器。


再次修改config/application.rb,我们可以简单的模仿这个行为:

config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :shoulda, :fixture => false
g.stylesheets false
# Add a fallback!
g.fallbacks[:shoulda] = :test_unit
end

现在,如果你建立了一个注释脚手架,可以看到shoulda生成器被调用,在最后,它们都退到TestUnit的生成器了。


$ rails generate scaffold Comment body:text
invoke active_record
create db/migrate/20091120151323_create_comments.rb
create app/models/comment.rb
invoke shoulda
create test/unit/comment_test.rb
create test/fixtures/comments.yml
route map.resources :comments
invoke scaffold_controller
create app/controllers/comments_controller.rb
invoke erb
create app/views/comments
create app/views/comments/index.html.erb
create app/views/comments/edit.html.erb
create app/views/comments/show.html.erb
create app/views/comments/new.html.erb
create app/views/comments/_form.html.erb
create app/views/layouts/comments.html.erb
invoke shoulda
create test/functional/comments_controller_test.rb
invoke my_helper
create app/helpers/comments_helper.rb
invoke shoulda
create test/unit/helpers/comments_helper_test.rb

回退使你的生成器有单独的职责,越来越多的代码重用并减少代码重写。


8应用模板

现在你已经看见了生成器在应用程序里可以被调用,你知道它也可生成应用吗?这种生成器叫做“模板”。

gem("rspec-rails", :group => "test")
gem("cucumber-rails", :group => "test")
if yes?("Would you like to install Devise?")
gem("devise")
generate("devise:install")
model_name = ask("What would you like the user model to be called? [user]")
model_name = "user" if model_name.blank?
generate("devise", model_name)
end

在上面的模板中,我们指定应用程序依赖于rspec-rails和cucumber-rails的gem,这两个东西将被加到Gemfile的测试组里。然后,我们提出一个问题给用户,他们是否想安装设备,如果用户回答“Y”或“yes”,模板将在所有Gemfile组的外面添加设备,运行devise:install生成器。然后这个模板读入用户输入,运行设备生成器,用户对最后一个问题的回答被传给生成器。

modify the outcome of the rails new command by using the -m option and passing in the filename:

想象这个模板在文件template.rb里,使用rails的new命令的时候,使用-m选项,提交文件名,我们可以用它修改new的结果。


$ rails new thud -m template.rb

这个命令将生成Thud应用,执行模板,并输出。

模板不一定要存在本地系统,-m选项支持在线模板。

$ rails new thud -mhttps://gist.github.com/722911.txt

同时最后一个章节不包括生成怎样生成对人类来说最可怕的模板,它将根据你的处理,使你的方法可用,这样你就可以自己开发。这些方法对生成器也同样可用。

9生成器方法


以下对Rails生成器和模板同样适用:


注:本指南不包括Thor提供的方法,在Thor的文档里可以找到它。


9.1 plugin


plugin将安装一个插件到现有应用里。

plugin("dynamic-form", :git => "git://github.com/rails/dynamic-form.git")
Available options are:
? :git – Takes the path to the git repository where this plugin can be found.
? :branch – The name of the branch of the git repository where the plugin is found.
? :submodule – Set to true for the plugin to be installed as a submodule. Defaults to false.
? :svn – Takes the path to the svn repository where this plugin can be found.
? :revision – The revision of the plugin in an SVN repository.


9.2 gem

为应用程序指定一个gem依赖。


gem("rspec", :group => "test", :version => "2.1.0")
gem("devise", "1.1.5")
Available options are:
? :group – The group in the Gemfile where this gem should go.
? :version – The version string of the gem you want to use. Can also be specified as the second argument to the method.
? :git – The URL to the git repository for this gem.
Any additional options passed to this method are put on the end of the line:
gem("devise", :git => "git://github.com/plataformatec/devise", :branch => "master")
The above code will put the following line into Gemfile:
gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"


9.3 add_source

加一个特定的源到Gemfile:

add_source "http://gems.github.com"

9.4 application

在应用的类定义之后,直接加入一行到config/application.rb。

application "config.asset_host = 'http://example.com'"
This method can also take a block:
application do
"config.asset_host = 'http://example.com'"
end
Available options are:
? :env – Specify an environment for this configuration option. If you wish to use this option with the block syntax the recommended syntax is as follows:
application(nil, :env => "development") do
"config.asset_host = 'http://localhost:3000'"
end


9.5 git


运行指定的git命令:


git :init
git :add => "."
git :commit => "-m First commit!"
git :add => "onefile.rb", :rm => "badfile.cxx"
The values of the hash here being the arguments or options passed to the specific git command. As per the final example shown here, multiple git commands can be specified at a time, but the order of their running is not guaranteed to be the same as the order that they were specified in.


9.6 vendor


将一个文件放入vendor,包含特定的代码。


vendor("sekrit.rb", '#top secret stuff')
This method also takes a block:
vendor("seeds.rb") do
"puts 'in ur app, seeding ur database'"
end


9.7 lib


将一个文件放入lib,包含特定的代码。


lib("special.rb", 'p Rails.root')
This method also takes a block:
lib("super_special.rb") do
puts "Super special!"
end


9.8 rakefile


在应用的lib/tasks创建一个Rake文件。


rakefile("test.rake", 'hello there')
This method also takes a block:
rakefile("test.rake") do
%Q{
task :rock => :environment do
puts "Rockin'"
end
}
end


9.9 initializer

在应用的config/initializers创建一个initializer文件:

initializer("begin.rb", "puts 'this is the beginning'")
This method also takes a block:
initializer("begin.rb") do
puts "Almost done!"
end


9.10 generate

运行指定的生成器,参数是生成器名,后面的参数将赋给生成器。


generate("scaffold", "forums title:string description:text")

9.11 rake

Runs the specified Rake task.
rake("db:migrate")
Available options are:
? :env – Specifies the environment in which to run this rake task.
? :sudo – Whether or not to run this task using sudo. Defaults to false.

9.12 capify!


在Capistrano运行capify命令,在应用程序的根目录,生成Capistrano配置。


capify!


9.13 route


给文件config/routes.rb加内容:


route("resources :people")


9.14 readme


输出参数中模板源路径的文件内容, 通常是一个README.


readme("README")


10 更改列表


December 1, 2010:加入模板和生成器的可用方法和选项文档,Ryan Bigg


December 1, 2010: 补充Rails应用模板,Ryan Bigg


August 23, 2010: 编辑通过, Xavier Noria


April 30, 2010: José Valim复核


November 20, 2009:第一稿,José Valim  

目录
相关文章
|
机器学习/深度学习 自然语言处理 达摩院
Modelscope 工程介绍及实战演示| 学习笔记
快速学习 Modelscope 工程介绍及实战演示
Modelscope 工程介绍及实战演示| 学习笔记
|
JavaScript PHP UED
【PHP开发专栏】PHP与WebSocket实时通信
【4月更文挑战第30天】本文介绍了PHP实现WebSocket实时通信的原理、使用方法和实际案例。WebSocket是基于HTTP的全双工通信协议,PHP 5.4以上版本支持WebSocket,可通过内置函数或第三方库如Socket.io、PHP-WebSocket来实现。文章详细展示了创建WebSocket服务器和客户端的PHP代码示例,并提及在PHP中使用Socket.io库进行实时通信。
869 2
|
存储 JavaScript 缓存
|
关系型数据库 MySQL PHP
XAMPP下pear安装
我们使用XAMPP作为本地开发环境,主要的原因还是非常方便地安装了我们所需要的开发环境,几乎是不需要什么设置,目前支持windows,linux,macOS和solaris,几乎涵盖了绝大多数主流开发者使用的操作系统。
993 0
|
7天前
|
人工智能 自然语言处理 JavaScript
2026年Windows+Ollama本地部署OpenClaw保姆级教程:本地AI Agent+阿里云上快速搭建
2026年OpenClaw凭借本地部署、私有化运行的特性,成为打造个人智能体的核心工具,而Ollama作为轻量级本地大模型管理工具,能让OpenClaw摆脱对云端大模型的依赖,实现**本地推理、数据不泄露、全流程私有化**的智能体验。本文基于Windows 11系统,从硬件环境准备、Ollama安装与模型定制、OpenClaw部署配置、技能扩展到常见问题排查,打造保姆级本地部署教程,同时补充阿里云OpenClaw(Clawdbot)快速部署步骤,兼顾本地私有化需求与云端7×24小时运行需求,文中所有代码命令均可直接复制执行,确保零基础用户也能快速搭建属于自己的本地智能体。
8657 71
|
4天前
|
人工智能 安全 API
CoPaw:5分钟部署你的 AI助理
源自阿里巴巴开源生态的个人 AI 助理——CoPaw。作为阿里倾力打造的开源力作,CoPaw 完美打通钉钉、飞书、Discord 等多平台对话通道,支持定时任务自动化。内置 PDF/Office 深度处理、新闻摘要等强大技能,更开放自定义扩展接口。坚持数据全程私有化部署,绝不上传云端,让每一位用户都能在大厂技术加持下,拥有安全、专属的智能助手。
|
6天前
|
人工智能 自然语言处理 机器人
保姆级教程:Mac本地搭建OpenClaw及阿里云上1分钟部署OpenClaw+飞书集成实战指南
OpenClaw(曾用名Clawdbot、Moltbot)作为2026年最热门的开源个人AI助手平台,以“自然语言驱动自动化”为核心,支持对接飞书、Telegram等主流通讯工具,可替代人工完成文件操作、日历管理、邮件处理等重复性工作。其模块化架构适配多系统环境,既可以在Mac上本地化部署打造私人助手,也能通过阿里云实现7×24小时稳定运行,完美兼顾隐私性与便捷性。
4069 8
|
5天前
|
人工智能 安全 JavaScript
阿里云上+本地部署OpenClaw(小龙虾)新手攻略:解锁10大必备Skills,零基础也能玩转AI助手
2026年,开源AI代理工具OpenClaw(昵称“小龙虾”)凭借“能实际做事”的核心优势,在GitHub斩获25万+星标,成为现象级AI工具。它最强大的魅力在于可扩展的Skills(技能包)系统——通过ClawHub插件市场的数百个技能,能让AI助手从简单聊天升级为处理办公、学习、日常事务的全能帮手。
3844 8
|
8天前
|
人工智能 JSON JavaScript
手把手教你用 OpenClaw + 飞书,打造专属 AI 机器人
手把手教你用 OpenClaw(v2026.2.22-2)+ 飞书,10分钟零代码搭建专属AI机器人!内置飞书插件,无需额外安装;支持Claude等主流模型,命令行一键配置。告别复杂开发,像聊同事一样自然对话。
4487 13
手把手教你用 OpenClaw + 飞书,打造专属 AI 机器人
|
7天前
|
人工智能 监控 机器人
2026年零门槛部署 OpenClaw(Clawdbot)接入A股数据,实现24小时股票分析保姆级教程
在AI赋能金融分析的浪潮中,OpenClaw(原Clawdbot/Moltbot)凭借开源灵活的架构,成为个人投资者打造专属智能分析助手的首选。通过接入A股实时数据,它能实现24小时市场监控、涨跌预警、潜力股推荐等核心功能,彻底解放人工盯盘的繁琐。而阿里云的稳定部署环境,更让这套系统实现全天候不间断运行,成为真正的“金融AI助手”。 本文基于OpenClaw v2026.1.25稳定版与QVeris免费A股数据接口,详细拆解阿里云OpenClaw部署步骤、A股数据接入流程、高级分析功能配置及多平台联动技巧,所有代码命令均可直接复制复用,即使无技术基础也能在1小时内完成从部署到实战的全流程。
3234 11