aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/generators.md
diff options
context:
space:
mode:
Diffstat (limited to 'guides/source/generators.md')
-rw-r--r--guides/source/generators.md120
1 files changed, 60 insertions, 60 deletions
diff --git a/guides/source/generators.md b/guides/source/generators.md
index 0581af2c85..cb99d1f60a 100644
--- a/guides/source/generators.md
+++ b/guides/source/generators.md
@@ -20,7 +20,7 @@ NOTE: This guide is about generators in Rails 3, previous versions are not cover
First Contact
-------------
-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+:
+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`:
```bash
$ rails new myapp
@@ -37,9 +37,9 @@ $ rails generate helper --help
Creating Your First Generator
-----------------------------
-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+.
+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`.
-The first step is to create a file at +lib/generators/initializer_generator.rb+ with the following content:
+The first step is to create a file at `lib/generators/initializer_generator.rb` with the following content:
```ruby
class InitializerGenerator < Rails::Generators::Base
@@ -49,9 +49,9 @@ class InitializerGenerator < Rails::Generators::Base
end
```
-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
+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
-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.
+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.
To invoke our new generator, we just need to do:
@@ -65,7 +65,7 @@ Before we go on, let's see our brand new generator description:
$ rails generate initializer --help
```
-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:
+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:
```ruby
class InitializerGenerator < Rails::Generators::Base
@@ -76,7 +76,7 @@ class InitializerGenerator < Rails::Generators::Base
end
```
-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.
+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.
Creating Generators with Generators
-----------------------------------
@@ -99,7 +99,7 @@ class InitializerGenerator < Rails::Generators::NamedBase
end
```
-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+.
+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`.
We can see that by invoking the description of this new generator (don't forget to delete the old generator file):
@@ -109,9 +109,9 @@ Usage:
rails generate initializer NAME [options]
```
-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+.
+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:
+In order to understand what a generator template means, let's create the file `lib/generators/initializer/templates/initializer.rb` with the following content:
```ruby
# Add initialization content here
@@ -135,14 +135,14 @@ And let's execute our generator:
$ rails generate initializer core_extensions
```
-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+.
+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`.
The methods that are available for generators are covered in the "final section":#generator-methods of this guide.
Generators Lookup
-----------------
-When you run +rails generate initializer core_extensions+ Rails requires these files in turn until one is found:
+When you run `rails generate initializer core_extensions` Rails requires these files in turn until one is found:
```bash
rails/generators/initializer/initializer_generator.rb
@@ -153,12 +153,12 @@ generators/initializer_generator.rb
If none is found you get an error message.
-INFO: The examples above put files under the application's +lib+ because said directory belongs to +$LOAD_PATH+.
+INFO: The examples above put files under the application's `lib` because said directory belongs to `$LOAD_PATH`.
Customizing Your Workflow
-------------------------
-Rails own generators are flexible enough to let you customize scaffolding. They can be configured in +config/application.rb+, these are some defaults:
+Rails own generators are flexible enough to let you customize scaffolding. They can be configured in `config/application.rb`, these are some defaults:
```ruby
config.generators do |g|
@@ -219,7 +219,7 @@ To demonstrate this, we are going to create a new helper generator that simply a
$ rails generate generator rails/my_helper
```
-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:
+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:
```ruby
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
@@ -239,7 +239,7 @@ We can try out our new generator by creating a helper for users:
$ rails generate my_helper products
```
-And it will generate the following helper file in +app/helpers+:
+And it will generate the following helper file in `app/helpers`:
```ruby
module ProductsHelper
@@ -247,7 +247,7 @@ module ProductsHelper
end
```
-Which is what we expected. We can now tell scaffold to use our new helper generator by editing +config/application.rb+ once again:
+Which is what we expected. We can now tell scaffold to use our new helper generator by editing `config/application.rb` once again:
```ruby
config.generators do |g|
@@ -288,7 +288,7 @@ end
end
```
-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:
+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:
```ruby
# Search for :helper instead of :my_helper
@@ -300,9 +300,9 @@ And now you can re-run scaffold for another resource and see it generating tests
Customizing Your Workflow by Changing Generators Templates
----------------------------------------------------------
-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+.
+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`.
-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:
+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:
```erb
module <%= class_name %>Helper
@@ -310,7 +310,7 @@ module <%= class_name %>Helper
end
```
-and revert the last change in +config/application.rb+:
+and revert the last change in `config/application.rb`:
```ruby
config.generators do |g|
@@ -321,14 +321,14 @@ config.generators do |g|
end
```
-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+.
+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`.
Adding Generators Fallbacks
---------------------------
-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.
+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.
-We can easily simulate this behavior by changing our +config/application.rb+ once again:
+We can easily simulate this behavior by changing our `config/application.rb` once again:
```ruby
config.generators do |g|
@@ -391,17 +391,17 @@ if yes?("Would you like to install Devise?")
end
```
-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.
+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.
-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:
+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:
```bash
$ rails new thud -m template.rb
```
-This command will generate the +Thud+ application, and then apply the template to the generated output.
+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:
+Templates don't have to be stored on the local system, the `-m` option also supports online templates:
```bash
$ rails new thud -m https://gist.github.com/722911.txt
@@ -416,7 +416,7 @@ The following are methods available for both generators and templates for Rails.
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
-### +gem+
+### `gem`
Specifies a gem dependency of the application.
@@ -427,9 +427,9 @@ 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.
+* `: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:
@@ -437,13 +437,13 @@ 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+:
+The above code will put the following line into `Gemfile`:
```ruby
gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"
```
-### +gem_group+
+### `gem_group`
Wraps gem entries inside a group:
@@ -453,15 +453,15 @@ gem_group :development, :test do
end
```
-### +add_source+
+### `add_source`
-Adds a specified source to +Gemfile+:
+Adds a specified source to `Gemfile`:
```ruby
add_source "http://gems.github.com"
```
-### +inject_into_file+
+### `inject_into_file`
Injects a block of code into a defined position in your file.
@@ -472,7 +472,7 @@ RUBY
end
```
-### +gsub_file+
+### `gsub_file`
Replaces text inside a file.
@@ -482,9 +482,9 @@ gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code
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.
-### +application+
+### `application`
-Adds a line to +config/application.rb+ directly after the application class definition.
+Adds a line to `config/application.rb` directly after the application class definition.
```ruby
application "config.asset_host = 'http://example.com'"
@@ -500,7 +500,7 @@ 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:
+* `: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
application(nil, :env => "development") do
@@ -508,7 +508,7 @@ application(nil, :env => "development") do
end
```
-### +git+
+### `git`
Runs the specified git command:
@@ -521,9 +521,9 @@ 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.
-### +vendor+
+### `vendor`
-Places a file into +vendor+ which contains the specified code.
+Places a file into `vendor` which contains the specified code.
```ruby
vendor("sekrit.rb", '#top secret stuff')
@@ -537,9 +537,9 @@ vendor("seeds.rb") do
end
```
-### +lib+
+### `lib`
-Places a file into +lib+ which contains the specified code.
+Places a file into `lib` which contains the specified code.
```ruby
lib("special.rb", 'p Rails.root')
@@ -553,9 +553,9 @@ lib("super_special.rb") do
end
```
-### +rakefile+
+### `rakefile`
-Creates a Rake file in the +lib/tasks+ directory of the application.
+Creates a Rake file in the `lib/tasks` directory of the application.
```ruby
rakefile("test.rake", 'hello there')
@@ -573,9 +573,9 @@ rakefile("test.rake") do
end
```
-### +initializer+
+### `initializer`
-Creates an initializer in the +config/initializers+ directory of the application:
+Creates an initializer in the `config/initializers` directory of the application:
```ruby
initializer("begin.rb", "puts 'this is the beginning'")
@@ -589,7 +589,7 @@ initializer("begin.rb") do
end
```
-### +generate+
+### `generate`
Runs the specified generator where the first argument is the generator name and the remaining arguments are passed directly to the generator.
@@ -598,7 +598,7 @@ generate("scaffold", "forums title:string description:text")
```
-### +rake+
+### `rake`
Runs the specified Rake task.
@@ -608,28 +608,28 @@ 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+.
+* `:env` - Specifies the environment in which to run this rake task.
+* `:sudo` - Whether or not to run this task using `sudo`. Defaults to `false`.
-### +capify!+
+### `capify!`
-Runs the +capify+ command from Capistrano at the root of the application which generates Capistrano configuration.
+Runs the `capify` command from Capistrano at the root of the application which generates Capistrano configuration.
```ruby
capify!
```
-### +route+
+### `route`
-Adds text to the +config/routes.rb+ file:
+Adds text to the `config/routes.rb` file:
```ruby
route("resources :people")
```
-### +readme+
+### `readme`
-Output the contents of a file in the template's +source_path+, usually a README.
+Output the contents of a file in the template's `source_path`, usually a README.
```ruby
readme("README")