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

X
Xavier Noria 已提交
3
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 已提交
4 5 6

In this guide you will:

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

endprologue.

X
Xavier Noria 已提交
17
NOTE: This guide is about generators in Rails 3, previous versions are not covered.
P
Pratik Naik 已提交
18

19
h3. 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 23

<shell>
24
$ rails new myapp
P
Pratik Naik 已提交
25
$ cd myapp
26
$ rails generate
P
Pratik Naik 已提交
27 28
</shell>

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 31

<shell>
32
$ rails generate helper --help
P
Pratik Naik 已提交
33 34
</shell>

35
h3. Creating Your First Generator
P
Pratik Naik 已提交
36

37
Since Rails 3.0, generators are built on top of "Thor":https://github.com/wycats/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 已提交
38

X
Xavier Noria 已提交
39
The first step is to create a file at +lib/generators/initializer_generator.rb+ with the following content:
P
Pratik Naik 已提交
40 41 42 43 44 45 46 47 48

<ruby>
class InitializerGenerator < Rails::Generators::Base
  def create_initializer_file
    create_file "config/initializers/initializer.rb", "# Add initialization content here"
  end
end
</ruby>

49
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/wycats/thor/master/Thor/Actions.html
50

51
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 已提交
52 53 54 55

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

<shell>
56
$ rails generate initializer
P
Pratik Naik 已提交
57 58 59 60 61
</shell>

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

<shell>
62
$ rails generate initializer --help
P
Pratik Naik 已提交
63 64
</shell>

65
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 已提交
66 67 68 69 70 71 72 73 74 75

<ruby>
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
</ruby>

76
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 已提交
77

78
h3. Creating Generators with Generators
P
Pratik Naik 已提交
79

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

<shell>
83
$ rails generate generator initializer
P
Pratik Naik 已提交
84 85 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
</shell>

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

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

98
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 已提交
99 100 101 102

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

<shell>
103
$ rails generate initializer --help
P
Pratik Naik 已提交
104
Usage:
105
  rails generate initializer NAME [options]
P
Pratik Naik 已提交
106 107
</shell>

X
Xavier Noria 已提交
108 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+.

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 已提交
111 112 113 114 115 116 117 118 119

<ruby>
# Add initialization content here
</ruby>

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

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

  def copy_initializer_file
    copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
  end
end
</ruby>

And let's execute our generator:

<shell>
X
Xavier Noria 已提交
131
$ rails generate initializer core_extensions
P
Pratik Naik 已提交
132 133
</shell>

A
s/a/an/  
Akira Matsuda 已提交
134
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 已提交
135

L
Lucas Catón 已提交
136
The methods that are available for generators are covered in the "final section":#generator-methods of this guide.
137

138
h3. Generators Lookup
P
Pratik Naik 已提交
139

X
Xavier Noria 已提交
140
When you run +rails generate initializer core_extensions+ Rails requires these files in turn until one is found:
P
Pratik Naik 已提交
141 142

<shell>
J
José Valim 已提交
143 144 145 146
rails/generators/initializer/initializer_generator.rb
generators/initializer/initializer_generator.rb
rails/generators/initializer_generator.rb
generators/initializer_generator.rb
P
Pratik Naik 已提交
147 148
</shell>

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

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

153
h3. Customizing Your Workflow
P
Pratik Naik 已提交
154

X
Xavier Noria 已提交
155
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 已提交
156 157 158 159 160 161 162 163 164

<ruby>
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  g.test_framework  :test_unit, :fixture => true
end
</ruby>

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

<shell>
168
$ rails generate scaffold User name:string
P
Pratik Naik 已提交
169 170 171 172 173 174
      invoke  active_record
      create    db/migrate/20091120125558_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/unit/user_test.rb
      create      test/fixtures/users.yml
X
Xavier Noria 已提交
175
       route  resources :users
P
Pratik Naik 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      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
      create      test/functional/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      create        test/unit/helpers/users_helper_test.rb
      invoke  stylesheets
192
      create    app/assets/stylesheets/scaffold.css
P
Pratik Naik 已提交
193 194
</shell>

X
Xavier Noria 已提交
195
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 已提交
196

X
Xavier Noria 已提交
197
Our first customization on the workflow will be to stop generating stylesheets and test fixtures for scaffolds. We can achieve that by changing our configuration to the following:
P
Pratik Naik 已提交
198 199 200 201 202 203 204 205 206 207

<ruby>
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  g.test_framework  :test_unit, :fixture => false
  g.stylesheets     false
end
</ruby>

208
If we generate another resource with the scaffold generator, we can see that neither stylesheets nor fixtures are 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 已提交
209

210
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 已提交
211 212

<shell>
213
$ rails generate generator rails/my_helper
P
Pratik Naik 已提交
214 215
</shell>

216
After that, we can delete both the +templates+ directory and the +source_root+ class method from our new generators, because we are not going to need them. So our new generator looks like the following:
P
Pratik Naik 已提交
217 218

<ruby>
219
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
P
Pratik Naik 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
  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
</ruby>

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

<shell>
X
Xavier Noria 已提交
233
$ rails generate my_helper products
P
Pratik Naik 已提交
234 235
</shell>

236
And it will generate the following helper file in +app/helpers+:
P
Pratik Naik 已提交
237 238

<ruby>
X
Xavier Noria 已提交
239 240
module ProductsHelper
  attr_reader :products, :product
P
Pratik Naik 已提交
241 242 243
end
</ruby>

X
Xavier Noria 已提交
244
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 已提交
245 246 247 248 249 250 251 252 253 254 255

<ruby>
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  g.test_framework  :test_unit, :fixture => false
  g.stylesheets     false
  g.helper          :my_helper
end
</ruby>

X
Xavier Noria 已提交
256
and see it in action when invoking the generator:
P
Pratik Naik 已提交
257 258

<shell>
259
$ rails generate scaffold Post body:text
P
Pratik Naik 已提交
260 261 262 263 264 265 266
      [...]
      invoke    my_helper
      create      app/helpers/posts_helper.rb
</shell>

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.

267
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 已提交
268

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

<ruby>
272
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
P
Pratik Naik 已提交
273 274 275 276 277 278 279 280 281 282 283 284
  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
</ruby>

285
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 已提交
286 287

<ruby>
V
Vijay Dev 已提交
288 289
# Search for :helper instead of :my_helper
hook_for :test_framework, :as => :helper
P
Pratik Naik 已提交
290 291 292 293
</ruby>

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

294
h3. Customizing Your Workflow by Changing Generators Templates
P
Pratik Naik 已提交
295

X
Xavier Noria 已提交
296
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 已提交
297

X
Xavier Noria 已提交
298
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 已提交
299 300 301 302 303 304 305

<erb>
module <%= class_name %>Helper
  attr_reader :<%= plural_name %>, <%= plural_name.singularize %>
end
</erb>

X
Xavier Noria 已提交
306
and revert the last change in +config/application.rb+:
P
Pratik Naik 已提交
307 308 309 310 311 312 313 314 315 316

<ruby>
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  g.test_framework  :test_unit, :fixture => false
  g.stylesheets     false
end
</ruby>

X
Xavier Noria 已提交
317
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 已提交
318

319
h3. Adding Generators Fallbacks
P
Pratik Naik 已提交
320

321
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 已提交
322 323 324 325 326 327 328 329 330 331

We can easily simulate this behavior by changing our +config/application.rb+ once again:

<ruby>
config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  g.test_framework  :shoulda, :fixture => false
  g.stylesheets     false

332
  # Add a fallback!
333
  g.fallbacks[:shoulda] = :test_unit
334
end
P
Pratik Naik 已提交
335 336
</ruby>

X
Xavier Noria 已提交
337
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 已提交
338 339

<shell>
340
$ rails generate scaffold Comment body:text
P
Pratik Naik 已提交
341 342 343 344 345 346
      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
347
       route    resources :comments
P
Pratik Naik 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
      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
</shell>

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

368
h3. Application Templates
369 370 371 372

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".

<ruby>
V
Vijay Dev 已提交
373 374 375 376 377 378 379 380 381 382
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
383 384
</ruby>

385
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.
386 387 388 389

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:

<shell>
390
$ rails new thud -m template.rb
391 392 393 394 395 396 397
</shell>

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

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

<shell>
398
$ rails new thud -m https://gist.github.com/722911.txt
399 400
</shell>

401 402 403 404
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.

h3. Generator methods

V
Vijay Dev 已提交
405
The following are methods available for both generators and templates for Rails.
406 407 408

NOTE: Methods provided by Thor are not covered this guide and can be found in "Thor's documentation":http://rdoc.info/github/wycats/thor/master/Thor/Actions.html

409 410 411 412 413
h4. +gem+

Specifies a gem dependency of the application.

<ruby>
V
Vijay Dev 已提交
414 415
gem("rspec", :group => "test", :version => "2.1.0")
gem("devise", "1.1.5")
416 417 418 419 420 421 422 423 424 425 426
</ruby>

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:

<ruby>
V
Vijay Dev 已提交
427
gem("devise", :git => "git://github.com/plataformatec/devise", :branch => "master")
428 429 430 431 432
</ruby>

The above code will put the following line into +Gemfile+:

<ruby>
V
Vijay Dev 已提交
433
gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"
434 435
</ruby>

436 437 438 439 440 441 442 443 444
h4. +gem_group+

Wraps gem entries inside a group:

<ruby>
gem_group :development, :test do
  gem "rspec-rails"
end
</ruby>
445

446 447 448 449 450
h4. +add_source+

Adds a specified source to +Gemfile+:

<ruby>
V
Vijay Dev 已提交
451
add_source "http://gems.github.com"
452 453
</ruby>

454 455 456 457 458
h4. +inject_into_file+

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

<ruby>
U
Uģis Ozols 已提交
459
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'
460 461 462 463 464 465 466 467 468 469
  puts "Hello World"
RUBY
end
</ruby>

h4. +gsub_file+

Replaces text inside a file.

<ruby>
V
Vijay Dev 已提交
470
gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code'
J
James Strocel 已提交
471
</ruby>
472 473 474

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.

475 476 477 478 479
h4. +application+

Adds a line to +config/application.rb+ directly after the application class definition.

<ruby>
V
Vijay Dev 已提交
480
application "config.asset_host = 'http://example.com'"
481 482 483 484 485
</ruby>

This method can also take a block:

<ruby>
V
Vijay Dev 已提交
486 487 488
application do
  "config.asset_host = 'http://example.com'"
end
489 490 491 492 493 494 495
</ruby>

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:

<ruby>
V
Vijay Dev 已提交
496 497 498
application(nil, :env => "development") do
  "config.asset_host = 'http://localhost:3000'"
end
499 500 501 502 503 504 505
</ruby>

h4. +git+

Runs the specified git command:

<ruby>
V
Vijay Dev 已提交
506 507 508 509
git :init
git :add => "."
git :commit => "-m First commit!"
git :add => "onefile.rb", :rm => "badfile.cxx"
510 511 512 513 514 515 516 517 518
</ruby>

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.

h4. +vendor+

Places a file into +vendor+ which contains the specified code.

<ruby>
V
Vijay Dev 已提交
519
vendor("sekrit.rb", '#top secret stuff')
520 521 522 523 524
</ruby>

This method also takes a block:

<ruby>
V
Vijay Dev 已提交
525 526 527
vendor("seeds.rb") do
  "puts 'in ur app, seeding ur database'"
end
528 529 530 531 532 533 534
</ruby>

h4. +lib+

Places a file into +lib+ which contains the specified code.

<ruby>
V
Vijay Dev 已提交
535
lib("special.rb", 'p Rails.root')
536 537 538 539 540
</ruby>

This method also takes a block:

<ruby>
V
Vijay Dev 已提交
541 542 543
lib("super_special.rb") do
  puts "Super special!"
end
544 545 546 547 548 549 550
</ruby>

h4. +rakefile+

Creates a Rake file in the +lib/tasks+ directory of the application.

<ruby>
V
Vijay Dev 已提交
551
rakefile("test.rake", 'hello there')
552 553 554 555 556
</ruby>

This method also takes a block:

<ruby>
V
Vijay Dev 已提交
557 558 559 560 561 562 563
rakefile("test.rake") do
  %Q{
    task :rock => :environment do
      puts "Rockin'"
    end
  }
end
564 565 566 567 568 569 570
</ruby>

h4. +initializer+

Creates an initializer in the +config/initializers+ directory of the application:

<ruby>
V
Vijay Dev 已提交
571
initializer("begin.rb", "puts 'this is the beginning'")
572 573 574 575 576
</ruby>

This method also takes a block:

<ruby>
V
Vijay Dev 已提交
577 578 579
initializer("begin.rb") do
  puts "Almost done!"
end
580 581 582 583 584 585 586
</ruby>

h4. +generate+

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

<ruby>
V
Vijay Dev 已提交
587
generate("scaffold", "forums title:string description:text")
588 589 590 591 592 593 594 595
</ruby>


h4. +rake+

Runs the specified Rake task.

<ruby>
V
Vijay Dev 已提交
596
rake("db:migrate")
597 598 599 600 601 602 603 604 605 606 607 608
</ruby>

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+.

h4. +capify!+

Runs the +capify+ command from Capistrano at the root of the application which generates Capistrano configuration.

<ruby>
V
Vijay Dev 已提交
609
capify!
610 611 612 613 614 615 616
</ruby>

h4. +route+

Adds text to the +config/routes.rb+ file:

<ruby>
V
Vijay Dev 已提交
617
route("resources :people")
618 619 620 621 622 623 624
</ruby>

h4. +readme+

Output the contents of a file in the template's +source_path+, usually a README.

<ruby>
V
Vijay Dev 已提交
625
readme("README")
626
</ruby>