generators.md 23.0 KB
Newer Older
1 2
Creating and Customizing Rails Generators & Templates
=====================================================
P
Pratik Naik 已提交
3

X
Xavier Noria 已提交
4
Rails generators are an essential tool if you plan to improve your workflow. With this guide you will learn how to create generators and customize existing ones.
P
Pratik Naik 已提交
5

6
After reading this guide, you will know:
P
Pratik Naik 已提交
7

8 9 10 11 12 13 14
* How to see which generators are available in your application.
* How to create a generator using templates.
* How Rails searches for generators before invoking them.
* How to customize your scaffold by creating new generators.
* How to customize your scaffold by changing generator templates.
* How to use fallbacks to avoid overwriting a huge set of generators.
* How to create an application template.
P
Pratik Naik 已提交
15

16
--------------------------------------------------------------------------------
P
Pratik Naik 已提交
17

18 19
First Contact
-------------
P
Pratik Naik 已提交
20

21
When you create an application using the `rails` command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking `rails generate`:
P
Pratik Naik 已提交
22

P
Prem Sichanugrist 已提交
23
```bash
24
$ rails new myapp
P
Pratik Naik 已提交
25
$ cd myapp
26
$ rails generate
27
```
P
Pratik Naik 已提交
28

29
You will get a list of all generators that comes with Rails. If you need a detailed description of the helper generator, for example, you can simply do:
P
Pratik Naik 已提交
30

P
Prem Sichanugrist 已提交
31
```bash
32
$ rails generate helper --help
33
```
P
Pratik Naik 已提交
34

35 36
Creating Your First Generator
-----------------------------
P
Pratik Naik 已提交
37

38
Since Rails 3.0, generators are built on top of [Thor](https://github.com/erikhuda/thor). Thor provides powerful options parsing and a great API for manipulating files. For instance, let's build a generator that creates an initializer file named `initializer.rb` inside `config/initializers`.
P
Pratik Naik 已提交
39

40
The first step is to create a file at `lib/generators/initializer_generator.rb` with the following content:
P
Pratik Naik 已提交
41

42
```ruby
P
Pratik Naik 已提交
43 44 45 46 47
class InitializerGenerator < Rails::Generators::Base
  def create_initializer_file
    create_file "config/initializers/initializer.rb", "# Add initialization content here"
  end
end
48
```
P
Pratik Naik 已提交
49

50
NOTE: `create_file` is a method provided by `Thor::Actions`. Documentation for `create_file` and other Thor methods can be found in [Thor's documentation](http://rdoc.info/github/erikhuda/thor/master/Thor/Actions.html)
51

52
Our new generator is quite simple: it inherits from `Rails::Generators::Base` and has one method definition. When a generator is invoked, each public method in the generator is executed sequentially in the order that it is defined. Finally, we invoke the `create_file` method that will create a file at the given destination with the given content. If you are familiar with the Rails Application Templates API, you'll feel right at home with the new generators API.
P
Pratik Naik 已提交
53 54 55

To invoke our new generator, we just need to do:

P
Prem Sichanugrist 已提交
56
```bash
57
$ rails generate initializer
58
```
P
Pratik Naik 已提交
59 60 61

Before we go on, let's see our brand new generator description:

P
Prem Sichanugrist 已提交
62
```bash
63
$ rails generate initializer --help
64
```
P
Pratik Naik 已提交
65

66
Rails is usually able to generate good descriptions if a generator is namespaced, as `ActiveRecord::Generators::ModelGenerator`, but not in this particular case. We can solve this problem in two ways. The first one is calling `desc` inside our generator:
P
Pratik Naik 已提交
67

68
```ruby
P
Pratik Naik 已提交
69 70 71 72 73 74
class InitializerGenerator < Rails::Generators::Base
  desc "This generator creates an initializer file at config/initializers"
  def create_initializer_file
    create_file "config/initializers/initializer.rb", "# Add initialization content here"
  end
end
75
```
P
Pratik Naik 已提交
76

77
Now we can see the new description by invoking `--help` on the new generator. The second way to add a description is by creating a file named `USAGE` in the same directory as our generator. We are going to do that in the next step.
P
Pratik Naik 已提交
78

79 80
Creating Generators with Generators
-----------------------------------
P
Pratik Naik 已提交
81

X
Xavier Noria 已提交
82
Generators themselves have a generator:
P
Pratik Naik 已提交
83

P
Prem Sichanugrist 已提交
84
```bash
85
$ rails generate generator initializer
P
Pratik Naik 已提交
86 87 88 89
      create  lib/generators/initializer
      create  lib/generators/initializer/initializer_generator.rb
      create  lib/generators/initializer/USAGE
      create  lib/generators/initializer/templates
90
```
P
Pratik Naik 已提交
91

X
Xavier Noria 已提交
92
This is the generator just created:
P
Pratik Naik 已提交
93

94
```ruby
P
Pratik Naik 已提交
95
class InitializerGenerator < Rails::Generators::NamedBase
J
José Valim 已提交
96
  source_root File.expand_path("../templates", __FILE__)
P
Pratik Naik 已提交
97
end
98
```
P
Pratik Naik 已提交
99

100
First, notice that we are inheriting from `Rails::Generators::NamedBase` instead of `Rails::Generators::Base`. This means that our generator expects at least one argument, which will be the name of the initializer, and will be available in our code in the variable `name`.
P
Pratik Naik 已提交
101 102 103

We can see that by invoking the description of this new generator (don't forget to delete the old generator file):

P
Prem Sichanugrist 已提交
104
```bash
105
$ rails generate initializer --help
P
Pratik Naik 已提交
106
Usage:
107
  rails generate initializer NAME [options]
108
```
P
Pratik Naik 已提交
109

110
We can also see that our new generator has a class method called `source_root`. This method points to where our generator templates will be placed, if any, and by default it points to the created directory `lib/generators/initializer/templates`.
X
Xavier Noria 已提交
111

112
In order to understand what a generator template means, let's create the file `lib/generators/initializer/templates/initializer.rb` with the following content:
P
Pratik Naik 已提交
113

114
```ruby
P
Pratik Naik 已提交
115
# Add initialization content here
116
```
P
Pratik Naik 已提交
117 118 119

And now let's change the generator to copy this template when invoked:

120
```ruby
P
Pratik Naik 已提交
121
class InitializerGenerator < Rails::Generators::NamedBase
J
José Valim 已提交
122
  source_root File.expand_path("../templates", __FILE__)
P
Pratik Naik 已提交
123 124 125 126 127

  def copy_initializer_file
    copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
  end
end
128
```
P
Pratik Naik 已提交
129 130 131

And let's execute our generator:

P
Prem Sichanugrist 已提交
132
```bash
X
Xavier Noria 已提交
133
$ rails generate initializer core_extensions
134
```
P
Pratik Naik 已提交
135

136
We can see that now an initializer named core_extensions was created at `config/initializers/core_extensions.rb` with the contents of our template. That means that `copy_file` copied a file in our source root to the destination path we gave. The method `file_name` is automatically created when we inherit from `Rails::Generators::NamedBase`.
P
Pratik Naik 已提交
137

138
The methods that are available for generators are covered in the [final section](#generator-methods) of this guide.
139

140 141
Generators Lookup
-----------------
P
Pratik Naik 已提交
142

143
When you run `rails generate initializer core_extensions` Rails requires these files in turn until one is found:
P
Pratik Naik 已提交
144

P
Prem Sichanugrist 已提交
145
```bash
J
José Valim 已提交
146 147 148 149
rails/generators/initializer/initializer_generator.rb
generators/initializer/initializer_generator.rb
rails/generators/initializer_generator.rb
generators/initializer_generator.rb
150
```
P
Pratik Naik 已提交
151

X
Xavier Noria 已提交
152 153
If none is found you get an error message.

154
INFO: The examples above put files under the application's `lib` because said directory belongs to `$LOAD_PATH`.
P
Pratik Naik 已提交
155

156 157
Customizing Your Workflow
-------------------------
P
Pratik Naik 已提交
158

159
Rails own generators are flexible enough to let you customize scaffolding. They can be configured in `config/application.rb`, these are some defaults:
P
Pratik Naik 已提交
160

161
```ruby
P
Pratik Naik 已提交
162 163 164
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
165
  g.test_framework  :test_unit, fixture: true
P
Pratik Naik 已提交
166
end
167
```
P
Pratik Naik 已提交
168

169
Before we customize our workflow, let's first see what our scaffold looks like:
P
Pratik Naik 已提交
170

P
Prem Sichanugrist 已提交
171
```bash
172
$ rails generate scaffold User name:string
P
Pratik Naik 已提交
173
      invoke  active_record
174
      create    db/migrate/20130924151154_create_users.rb
P
Pratik Naik 已提交
175 176
      create    app/models/user.rb
      invoke    test_unit
M
Mike Moore 已提交
177
      create      test/models/user_test.rb
P
Pratik Naik 已提交
178
      create      test/fixtures/users.yml
179 180
      invoke  resource_route
       route    resources :users
P
Pratik Naik 已提交
181 182 183 184 185 186 187 188 189 190
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
M
Mike Moore 已提交
191
      create      test/controllers/users_controller_test.rb
P
Pratik Naik 已提交
192 193 194
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
M
Mike Moore 已提交
195
      create        test/helpers/users_helper_test.rb
196 197 198
      invoke    jbuilder
      create      app/views/users/index.json.jbuilder
      create      app/views/users/show.json.jbuilder
199 200 201 202 203 204 205
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/users.js.coffee
      invoke    scss
      create      app/assets/stylesheets/users.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss
206
```
P
Pratik Naik 已提交
207

X
Xavier Noria 已提交
208
Looking at this output, it's easy to understand how generators work in Rails 3.0 and above. The scaffold generator doesn't actually generate anything, it just invokes others to do the work. This allows us to add/replace/remove any of those invocations. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication.
P
Pratik Naik 已提交
209

210
Our first customization on the workflow will be to stop generating stylesheets, javascripts and test fixtures for scaffolds. We can achieve that by changing our configuration to the following:
P
Pratik Naik 已提交
211

212
```ruby
P
Pratik Naik 已提交
213 214 215
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
216
  g.test_framework  :test_unit, fixture: false
P
Pratik Naik 已提交
217
  g.stylesheets     false
218
  g.javascripts     false
P
Pratik Naik 已提交
219
end
220
```
P
Pratik Naik 已提交
221

222
If we generate another resource with the scaffold generator, we can see that stylesheets, javascripts and fixtures are not created anymore. If you want to customize it further, for example to use DataMapper and RSpec instead of Active Record and TestUnit, it's just a matter of adding their gems to your application and configuring your generators.
P
Pratik Naik 已提交
223

224
To demonstrate this, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator within the rails namespace, as this is where rails searches for generators used as hooks:
P
Pratik Naik 已提交
225

P
Prem Sichanugrist 已提交
226
```bash
227
$ rails generate generator rails/my_helper
228 229 230 231
      create  lib/generators/rails/my_helper
      create  lib/generators/rails/my_helper/my_helper_generator.rb
      create  lib/generators/rails/my_helper/USAGE
      create  lib/generators/rails/my_helper/templates
232
```
P
Pratik Naik 已提交
233

234 235 236
After that, we can delete both the `templates` directory and the `source_root`
class method call from our new generator, because we are not going to need them.
Add the method below, so our generator looks like the following:
P
Pratik Naik 已提交
237

238
```ruby
239
# lib/generators/rails/my_helper/my_helper_generator.rb
240
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
P
Pratik Naik 已提交
241 242 243 244 245 246 247 248
  def create_helper_file
    create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
module #{class_name}Helper
  attr_reader :#{plural_name}, :#{plural_name.singularize}
end
    FILE
  end
end
249
```
P
Pratik Naik 已提交
250 251 252

We can try out our new generator by creating a helper for users:

P
Prem Sichanugrist 已提交
253
```bash
X
Xavier Noria 已提交
254
$ rails generate my_helper products
255
      create  app/helpers/products_helper.rb
256
```
P
Pratik Naik 已提交
257

258
And it will generate the following helper file in `app/helpers`:
P
Pratik Naik 已提交
259

260
```ruby
X
Xavier Noria 已提交
261 262
module ProductsHelper
  attr_reader :products, :product
P
Pratik Naik 已提交
263
end
264
```
P
Pratik Naik 已提交
265

266
Which is what we expected. We can now tell scaffold to use our new helper generator by editing `config/application.rb` once again:
P
Pratik Naik 已提交
267

268
```ruby
P
Pratik Naik 已提交
269 270 271
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
272
  g.test_framework  :test_unit, fixture: false
P
Pratik Naik 已提交
273
  g.stylesheets     false
274
  g.javascripts     false
P
Pratik Naik 已提交
275 276
  g.helper          :my_helper
end
277
```
P
Pratik Naik 已提交
278

X
Xavier Noria 已提交
279
and see it in action when invoking the generator:
P
Pratik Naik 已提交
280

P
Prem Sichanugrist 已提交
281
```bash
282
$ rails generate scaffold Post body:text
P
Pratik Naik 已提交
283 284 285
      [...]
      invoke    my_helper
      create      app/helpers/posts_helper.rb
286
```
P
Pratik Naik 已提交
287 288 289

We can notice on the output that our new helper was invoked instead of the Rails default. However one thing is missing, which is tests for our new generator and to do that, we are going to reuse old helpers test generators.

290
Since Rails 3.0, this is easy to do due to the hooks concept. Our new helper does not need to be focused in one specific test framework, it can simply provide a hook and a test framework just needs to implement this hook in order to be compatible.
P
Pratik Naik 已提交
291

X
Xavier Noria 已提交
292
To do that, we can change the generator this way:
P
Pratik Naik 已提交
293

294
```ruby
295
# lib/generators/rails/my_helper/my_helper_generator.rb
296
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
P
Pratik Naik 已提交
297 298 299 300 301 302 303 304 305 306
  def create_helper_file
    create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
module #{class_name}Helper
  attr_reader :#{plural_name}, :#{plural_name.singularize}
end
    FILE
  end

  hook_for :test_framework
end
307
```
P
Pratik Naik 已提交
308

309
Now, when the helper generator is invoked and TestUnit is configured as the test framework, it will try to invoke both `Rails::TestUnitGenerator` and `TestUnit::MyHelperGenerator`. Since none of those are defined, we can tell our generator to invoke `TestUnit::Generators::HelperGenerator` instead, which is defined since it's a Rails generator. To do that, we just need to add:
P
Pratik Naik 已提交
310

311
```ruby
V
Vijay Dev 已提交
312
# Search for :helper instead of :my_helper
313
hook_for :test_framework, as: :helper
314
```
P
Pratik Naik 已提交
315 316 317

And now you can re-run scaffold for another resource and see it generating tests as well!

318 319
Customizing Your Workflow by Changing Generators Templates
----------------------------------------------------------
P
Pratik Naik 已提交
320

321
In the step above we simply wanted to add a line to the generated helper, without adding any extra functionality. There is a simpler way to do that, and it's by replacing the templates of already existing generators, in that case `Rails::Generators::HelperGenerator`.
P
Pratik Naik 已提交
322

323
In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths. And one of them is `lib/templates`. Since we want to customize `Rails::Generators::HelperGenerator`, we can do that by simply making a template copy inside `lib/templates/rails/helper` with the name `helper.rb`. So let's create that file with the following content:
P
Pratik Naik 已提交
324

325
```erb
P
Pratik Naik 已提交
326
module <%= class_name %>Helper
M
manishval 已提交
327
  attr_reader :<%= plural_name %>, :<%= plural_name.singularize %>
P
Pratik Naik 已提交
328
end
329
```
P
Pratik Naik 已提交
330

331
and revert the last change in `config/application.rb`:
P
Pratik Naik 已提交
332

333
```ruby
P
Pratik Naik 已提交
334 335 336
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
337
  g.test_framework  :test_unit, fixture: false
P
Pratik Naik 已提交
338
  g.stylesheets     false
339
  g.javascripts     false
P
Pratik Naik 已提交
340
end
341
```
P
Pratik Naik 已提交
342

343
If you generate another resource, you can see that we get exactly the same result! This is useful if you want to customize your scaffold templates and/or layout by just creating `edit.html.erb`, `index.html.erb` and so on inside `lib/templates/erb/scaffold`.
P
Pratik Naik 已提交
344

345 346
Adding Generators Fallbacks
---------------------------
P
Pratik Naik 已提交
347

348
One last feature about generators which is quite useful for plugin generators is fallbacks. For example, imagine that you want to add a feature on top of TestUnit like [shoulda](https://github.com/thoughtbot/shoulda) does. Since TestUnit already implements all generators required by Rails and shoulda just wants to overwrite part of it, there is no need for shoulda to reimplement some generators again, it can simply tell Rails to use a `TestUnit` generator if none was found under the `Shoulda` namespace.
P
Pratik Naik 已提交
349

350
We can easily simulate this behavior by changing our `config/application.rb` once again:
P
Pratik Naik 已提交
351

352
```ruby
P
Pratik Naik 已提交
353 354 355
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
356
  g.test_framework  :shoulda, fixture: false
P
Pratik Naik 已提交
357
  g.stylesheets     false
358
  g.javascripts     false
P
Pratik Naik 已提交
359

360
  # Add a fallback!
361
  g.fallbacks[:shoulda] = :test_unit
362
end
363
```
P
Pratik Naik 已提交
364

X
Xavier Noria 已提交
365
Now, if you create a Comment scaffold, you will see that the shoulda generators are being invoked, and at the end, they are just falling back to TestUnit generators:
P
Pratik Naik 已提交
366

P
Prem Sichanugrist 已提交
367
```bash
368
$ rails generate scaffold Comment body:text
P
Pratik Naik 已提交
369
      invoke  active_record
370
      create    db/migrate/20130924143118_create_comments.rb
P
Pratik Naik 已提交
371 372
      create    app/models/comment.rb
      invoke    shoulda
M
Mike Moore 已提交
373
      create      test/models/comment_test.rb
P
Pratik Naik 已提交
374
      create      test/fixtures/comments.yml
375
      invoke  resource_route
376
       route    resources :comments
P
Pratik Naik 已提交
377 378 379 380 381 382 383 384 385 386
      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
      invoke    shoulda
M
Mike Moore 已提交
387
      create      test/controllers/comments_controller_test.rb
P
Pratik Naik 已提交
388 389 390
      invoke    my_helper
      create      app/helpers/comments_helper.rb
      invoke      shoulda
M
Mike Moore 已提交
391
      create        test/helpers/comments_helper_test.rb
392 393 394
      invoke    jbuilder
      create      app/views/comments/index.json.jbuilder
      create      app/views/comments/show.json.jbuilder
395 396 397 398
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/comments.js.coffee
      invoke    scss
399
```
P
Pratik Naik 已提交
400

401
Fallbacks allow your generators to have a single responsibility, increasing code reuse and reducing the amount of duplication.
P
Pratik Naik 已提交
402

403 404
Application Templates
---------------------
405

V
Vijay Dev 已提交
406
Now that you've seen how generators can be used _inside_ an application, did you know they can also be used to _generate_ applications too? This kind of generator is referred as a "template". This is a brief overview of the Templates API. For detailed documentation see the [Rails Application Templates guide](rails_application_templates.html).
407

408
```ruby
409 410
gem "rspec-rails", group: "test"
gem "cucumber-rails", group: "test"
V
Vijay Dev 已提交
411 412

if yes?("Would you like to install Devise?")
413 414
  gem "devise"
  generate "devise:install"
V
Vijay Dev 已提交
415 416
  model_name = ask("What would you like the user model to be called? [user]")
  model_name = "user" if model_name.blank?
417
  generate "devise", model_name
V
Vijay Dev 已提交
418
end
419
```
420

421
In the above template we specify that the application relies on the `rspec-rails` and `cucumber-rails` gem so these two will be added to the `test` group in the `Gemfile`. Then we pose a question to the user about whether or not they would like to install Devise. If the user replies "y" or "yes" to this question, then the template will add Devise to the `Gemfile` outside of any group and then runs the `devise:install` generator. This template then takes the users input and runs the `devise` generator, with the user's answer from the last question being passed to this generator.
422

423
Imagine that this template was in a file called `template.rb`. We can use it to modify the outcome of the `rails new` command by using the `-m` option and passing in the filename:
424

P
Prem Sichanugrist 已提交
425
```bash
426
$ rails new thud -m template.rb
427
```
428

429
This command will generate the `Thud` application, and then apply the template to the generated output.
430

431
Templates don't have to be stored on the local system, the `-m` option also supports online templates:
432

P
Prem Sichanugrist 已提交
433
```bash
A
Akira Matsuda 已提交
434
$ rails new thud -m https://gist.github.com/radar/722911/raw/
435
```
436

437 438
Whilst the final section of this guide doesn't cover how to generate the most awesome template known to man, it will take you through the methods available at your disposal so that you can develop it yourself. These same methods are also available for generators.

439 440
Generator methods
-----------------
441

V
Vijay Dev 已提交
442
The following are methods available for both generators and templates for Rails.
443

444
NOTE: Methods provided by Thor are not covered this guide and can be found in [Thor's documentation](http://rdoc.info/github/erikhuda/thor/master/Thor/Actions.html)
445

446
### `gem`
447 448 449

Specifies a gem dependency of the application.

450
```ruby
451 452
gem "rspec", group: "test", version: "2.1.0"
gem "devise", "1.1.5"
453
```
454 455 456

Available options are:

457 458 459
* `: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.
460 461 462

Any additional options passed to this method are put on the end of the line:

463
```ruby
464
gem "devise", git: "git://github.com/plataformatec/devise", branch: "master"
465
```
466

467
The above code will put the following line into `Gemfile`:
468

469
```ruby
470
gem "devise", git: "git://github.com/plataformatec/devise", branch: "master"
471
```
472

473
### `gem_group`
474 475 476

Wraps gem entries inside a group:

477
```ruby
478 479 480
gem_group :development, :test do
  gem "rspec-rails"
end
481
```
482

483
### `add_source`
484

485
Adds a specified source to `Gemfile`:
486

487
```ruby
V
Vijay Dev 已提交
488
add_source "http://gems.github.com"
489
```
490

491
### `inject_into_file`
492 493 494

Injects a block of code into a defined position in your file.

495
```ruby
496
inject_into_file 'name_of_file.rb', after: "#The code goes below this line. Don't forget the Line break at the end\n" do <<-'RUBY'
497 498 499
  puts "Hello World"
RUBY
end
500
```
501

502
### `gsub_file`
503 504 505

Replaces text inside a file.

506
```ruby
V
Vijay Dev 已提交
507
gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code'
508
```
509 510 511

Regular Expressions can be used to make this method more precise. You can also use append_file and prepend_file in the same way to place code at the beginning and end of a file respectively.

512
### `application`
513

514
Adds a line to `config/application.rb` directly after the application class definition.
515

516
```ruby
V
Vijay Dev 已提交
517
application "config.asset_host = 'http://example.com'"
518
```
519 520 521

This method can also take a block:

522
```ruby
V
Vijay Dev 已提交
523 524 525
application do
  "config.asset_host = 'http://example.com'"
end
526
```
527 528 529

Available options are:

530
* `: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:
531

532
```ruby
533
application(nil, env: "development") do
V
Vijay Dev 已提交
534 535
  "config.asset_host = 'http://localhost:3000'"
end
536
```
537

538
### `git`
539 540 541

Runs the specified git command:

542
```ruby
V
Vijay Dev 已提交
543
git :init
544 545 546
git add: "."
git commit: "-m First commit!"
git add: "onefile.rb", rm: "badfile.cxx"
547
```
548 549 550

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.

551
### `vendor`
552

553
Places a file into `vendor` which contains the specified code.
554

555
```ruby
556
vendor "sekrit.rb", '#top secret stuff'
557
```
558 559 560

This method also takes a block:

561
```ruby
562
vendor "seeds.rb" do
V
Vijay Dev 已提交
563 564
  "puts 'in ur app, seeding ur database'"
end
565
```
566

567
### `lib`
568

569
Places a file into `lib` which contains the specified code.
570

571
```ruby
572
lib "special.rb", "p Rails.root"
573
```
574 575 576

This method also takes a block:

577
```ruby
578
lib "super_special.rb" do
V
Vijay Dev 已提交
579 580
  puts "Super special!"
end
581
```
582

583
### `rakefile`
584

585
Creates a Rake file in the `lib/tasks` directory of the application.
586

587
```ruby
588
rakefile "test.rake", "hello there"
589
```
590 591 592

This method also takes a block:

593
```ruby
594
rakefile "test.rake" do
V
Vijay Dev 已提交
595
  %Q{
596
    task rock: :environment do
V
Vijay Dev 已提交
597 598 599 600
      puts "Rockin'"
    end
  }
end
601
```
602

603
### `initializer`
604

605
Creates an initializer in the `config/initializers` directory of the application:
606

607
```ruby
608
initializer "begin.rb", "puts 'this is the beginning'"
609
```
610

611
This method also takes a block, expected to return a string:
612

613
```ruby
614
initializer "begin.rb" do
615
  "puts 'this is the beginning'"
V
Vijay Dev 已提交
616
end
617
```
618

619
### `generate`
620 621 622

Runs the specified generator where the first argument is the generator name and the remaining arguments are passed directly to the generator.

623
```ruby
624
generate "scaffold", "forums title:string description:text"
625
```
626 627


628
### `rake`
629 630 631

Runs the specified Rake task.

632
```ruby
633
rake "db:migrate"
634
```
635 636 637

Available options are:

638 639
* `:env` - Specifies the environment in which to run this rake task.
* `:sudo` - Whether or not to run this task using `sudo`. Defaults to `false`.
640

641
### `capify!`
642

643
Runs the `capify` command from Capistrano at the root of the application which generates Capistrano configuration.
644

645
```ruby
V
Vijay Dev 已提交
646
capify!
647
```
648

649
### `route`
650

651
Adds text to the `config/routes.rb` file:
652

653
```ruby
654
route "resources :people"
655
```
656

657
### `readme`
658

659
Output the contents of a file in the template's `source_path`, usually a README.
660

661
```ruby
662
readme "README"
663
```