2018-07-23 22:29:31 -04:00
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.**
2014-12-23 17:32:50 -05:00
2012-09-01 17:25:58 -04:00
Rails Application Templates
===========================
2009-04-17 09:28:46 -04:00
2012-01-02 15:49:18 -05:00
Application templates are simple Ruby files containing DSL for adding gems/initializers etc. to your freshly created Rails project or an existing Rails project.
2009-07-25 11:03:58 -04:00
2012-11-29 17:25:02 -05:00
After reading this guide, you will know:
2009-04-17 09:28:46 -04:00
2012-12-07 12:50:09 -05:00
* How to use templates to generate/customize Rails applications.
* How to write your own reusable application templates using the Rails template API.
2009-04-17 09:28:46 -04:00
2012-09-01 17:25:58 -04:00
--------------------------------------------------------------------------------
2009-04-17 09:28:46 -04:00
2012-09-01 17:25:58 -04:00
Usage
-----
2009-04-17 09:28:46 -04:00
2016-05-17 22:46:07 -04:00
To apply a template, you need to provide the Rails generator with the location of the template you wish to apply using the `-m` option. This can either be a path to a file or a URL.
2009-07-25 11:03:58 -04:00
2012-09-01 20:45:26 -04:00
```bash
2010-06-03 10:03:42 -04:00
$ rails new blog -m ~/template.rb
2011-07-22 16:51:05 -04:00
$ rails new blog -m http://example.com/template.rb
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2018-06-29 12:49:05 -04:00
You can use the `app:template` rails command to apply templates to an existing Rails application. The location of the template needs to be passed in via the LOCATION environment variable. Again, this can either be path to a file or a URL.
2009-07-25 11:03:58 -04:00
2012-09-01 20:45:26 -04:00
```bash
2018-06-26 16:02:51 -04:00
$ rails app:template LOCATION=~/template.rb
$ rails app:template LOCATION=http://example.com/template.rb
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
Template API
------------
2009-07-25 11:03:58 -04:00
2013-04-05 20:18:07 -04:00
The Rails templates API is easy to understand. Here's an example of a typical Rails template:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
# template.rb
generate(:scaffold, "person name:string")
2012-11-16 05:30:12 -05:00
route "root to: 'people#index'"
2016-02-20 12:56:35 -05:00
rails_command("db:migrate")
2009-07-25 11:03:58 -04:00
2014-07-31 06:05:07 -04:00
after_bundle do
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2013-04-05 20:18:07 -04:00
The following sections outline the primary methods provided by the API:
2009-07-25 11:03:58 -04:00
2012-11-09 16:09:50 -05:00
### gem(*args)
2009-07-25 11:03:58 -04:00
2013-08-23 11:59:11 -04:00
Adds a `gem` entry for the supplied gem to the generated application's `Gemfile` .
2009-07-25 11:03:58 -04:00
2012-09-01 21:37:59 -04:00
For example, if your application depends on the gems `bj` and `nokogiri` :
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
gem "bj"
2010-06-23 08:52:02 -04:00
gem "nokogiri"
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 21:37:59 -04:00
Please note that this will NOT install the gems for you and you will have to run `bundle install` to do that.
2009-07-25 11:03:58 -04:00
2012-09-03 21:21:24 -04:00
```bash
2011-06-11 12:01:45 -04:00
bundle install
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
### gem_group(*names, &block)
2011-09-04 04:14:53 -04:00
Wraps gem entries inside a group.
2013-04-05 20:18:07 -04:00
For example, if you want to load `rspec-rails` only in the `development` and `test` groups:
2011-09-04 04:14:53 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-09-04 04:14:53 -04:00
gem_group :development, :test do
gem "rspec-rails"
end
2012-09-01 17:08:06 -04:00
```
2011-09-04 04:14:53 -04:00
2015-09-10 08:12:11 -04:00
### add_source(source, options={}, &block)
2010-12-26 12:27:45 -05:00
2012-09-01 21:37:59 -04:00
Adds the given source to the generated application's `Gemfile` .
2010-12-26 12:27:45 -05:00
2019-07-08 14:30:31 -04:00
For example, if you need to source a gem from `"http://gems.github.com"` :
2010-12-26 12:27:45 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2019-07-08 14:30:31 -04:00
add_source "http://gems.github.com"
2012-09-01 17:08:06 -04:00
```
2010-12-26 12:27:45 -05:00
2015-09-10 08:12:11 -04:00
If block is given, gem entries in block are wrapped into the source group.
```ruby
add_source "http://gems.github.com/" do
gem "rspec-rails"
end
```
2012-11-09 16:10:39 -05:00
### environment/application(data=nil, options={}, &block)
Adds a line inside the `Application` class for `config/application.rb` .
2012-11-16 14:43:52 -05:00
If `options[:env]` is specified, the line is appended to the corresponding file in `config/environments` .
2012-11-09 16:10:39 -05:00
```ruby
2013-05-27 15:52:04 -04:00
environment 'config.action_mailer.default_url_options = {host: "http://yourwebsite.example.com"}', env: 'production'
2012-11-09 16:10:39 -05:00
```
A block can be used in place of the `data` argument.
2012-09-01 17:25:58 -04:00
### vendor/lib/file/initializer(filename, data = nil, &block)
2009-07-25 11:03:58 -04:00
2013-08-23 11:59:11 -04:00
Adds an initializer to the generated application's `config/initializers` directory.
2009-07-25 11:03:58 -04:00
2013-04-05 20:18:07 -04:00
Let's say you like using `Object#not_nil?` and `Object#not_blank?` :
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
initializer 'bloatlol.rb', < < -CODE
2012-09-03 21:21:24 -04:00
class Object
def not_nil?
!nil?
end
2010-08-14 01:13:00 -04:00
2012-09-03 21:21:24 -04:00
def not_blank?
!blank?
end
2009-07-25 11:03:58 -04:00
end
CODE
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2013-04-05 20:18:07 -04:00
Similarly, `lib()` creates a file in the `lib/` directory and `vendor()` creates a file in the `vendor/` directory.
2009-07-25 11:03:58 -04:00
2013-04-05 20:18:07 -04:00
There is even `file()` , which accepts a relative path from `Rails.root` and creates all the directories/files needed:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
file 'app/components/foo.rb', < < -CODE
2012-09-03 21:21:24 -04:00
class Foo
end
2009-07-25 11:03:58 -04:00
CODE
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2013-08-23 11:59:11 -04:00
That'll create the `app/components` directory and put `foo.rb` in there.
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
### rakefile(filename, data = nil, &block)
2009-07-25 11:03:58 -04:00
2012-09-01 21:37:59 -04:00
Creates a new rake file under `lib/tasks` with the supplied tasks:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
rakefile("bootstrap.rake") do
< < -TASK
namespace :boot do
task :strap do
puts "i like boots!"
end
end
TASK
end
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 21:37:59 -04:00
The above creates `lib/tasks/bootstrap.rake` with a `boot:strap` rake task.
2009-07-25 11:03:58 -04:00
2012-11-09 16:09:50 -05:00
### generate(what, *args)
2009-07-25 11:03:58 -04:00
2011-08-15 11:06:32 -04:00
Runs the supplied rails generator with given arguments.
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
generate(:scaffold, "person", "name:string", "address:text", "age:number")
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
### run(command)
2009-07-25 11:03:58 -04:00
2013-01-03 15:28:21 -05:00
Executes an arbitrary command. Just like the backticks. Let's say you want to remove the `README.rdoc` file:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2013-01-03 15:28:21 -05:00
run "rm README.rdoc"
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2016-02-20 12:56:35 -05:00
### rails_command(command, options = {})
2009-07-25 11:03:58 -04:00
2018-06-29 12:49:05 -04:00
Runs the supplied command in the Rails application. Let's say you want to migrate the database:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2016-02-20 12:56:35 -05:00
rails_command "db:migrate"
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2018-06-29 12:49:05 -04:00
You can also run commands with a different Rails environment:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2016-02-20 12:56:35 -05:00
rails_command "db:migrate", env: 'production'
```
2018-06-29 12:49:05 -04:00
You can also run commands as a super-user:
2016-02-20 12:56:35 -05:00
```ruby
rails_command "log:clear", sudo: true
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2018-12-07 01:01:32 -05:00
You can also run commands that should abort application generation if they fail:
```ruby
rails_command "db:migrate", abort_on_failure: true
```
2012-09-01 17:25:58 -04:00
### route(routing_code)
2009-07-25 11:03:58 -04:00
2013-04-05 20:18:07 -04:00
Adds a routing entry to the `config/routes.rb` file. In the steps above, we generated a person scaffold and also removed `README.rdoc` . Now, to make `PeopleController#index` the default page for the application:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-11-16 05:30:12 -05:00
route "root to: 'person#index'"
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
### inside(dir)
2009-07-25 11:03:58 -04:00
2011-02-20 11:01:12 -05:00
Enables you to run a command from the given directory. For example, if you have a copy of edge rails that you wish to symlink from your new apps, you can do this:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
inside('vendor') do
run "ln -s ~/commit-rails/rails rails"
end
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
### ask(question)
2009-07-25 11:03:58 -04:00
2013-08-23 11:59:11 -04:00
`ask()` gives you a chance to get some feedback from the user and use it in your templates. Let's say you want your user to name the new shiny library you're adding:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
lib_name = ask("What do you want to call the shiny library ?")
lib_name < < ".rb" unless lib_name.index(".rb")
lib lib_name, < < -CODE
2012-09-03 21:21:24 -04:00
class Shiny
end
2009-07-25 11:03:58 -04:00
CODE
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
### yes?(question) or no?(question)
2009-07-25 11:03:58 -04:00
2019-10-14 20:20:01 -04:00
These methods let you ask questions from templates and decide the flow based on the user's answer. Let's say you want to prompt the user to run migrations:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2019-10-14 20:20:01 -04:00
rails_command("db:migrate") if yes?("Run database migrations?")
2012-09-03 21:21:24 -04:00
# no?(question) acts just the opposite.
2012-09-01 17:08:06 -04:00
```
2009-07-25 11:03:58 -04:00
2012-09-01 17:25:58 -04:00
### git(:command)
2009-07-25 11:03:58 -04:00
2011-04-13 20:58:26 -04:00
Rails templates let you run any git command:
2009-07-25 11:03:58 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-07-25 11:03:58 -04:00
git :init
2012-11-16 05:30:12 -05:00
git add: "."
git commit: "-a -m 'Initial commit'"
2012-09-01 17:08:06 -04:00
```
2013-07-05 07:21:01 -04:00
2014-07-31 06:05:07 -04:00
### after_bundle(&block)
2014-08-06 02:49:00 -04:00
Registers a callback to be executed after the gems are bundled and binstubs
are generated. Useful for all generated files to version control:
2014-07-31 06:05:07 -04:00
```ruby
after_bundle do
git :init
git add: '.'
git commit: "-a -m 'Initial commit'"
end
```
The callbacks gets executed even if `--skip-bundle` and/or `--skip-spring` has
been passed.
2013-07-05 07:21:01 -04:00
Advanced Usage
--------------
The application template is evaluated in the context of a
`Rails::Generators::AppGenerator` instance. It uses the `apply` action
provided by
[Thor ](https://github.com/erikhuda/thor/blob/master/lib/thor/actions.rb#L207 ).
This means you can extend and change the instance to match your needs.
For example by overwriting the `source_paths` method to contain the
location of your template. Now methods like `copy_file` will accept
relative paths to your template's location.
```ruby
def source_paths
2017-05-15 10:17:28 -04:00
[__dir__]
2013-07-05 07:21:01 -04:00
end
```