aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/generators.textile
diff options
context:
space:
mode:
authorRomD <romd86@gmail.com>2010-02-06 17:18:10 +0100
committerCarl Lerche <carllerche@mac.com>2010-02-06 09:51:53 -0800
commitf44a0b1d524064a2e919cd10d3013db680af9b17 (patch)
tree43011f4c151d45dbecdf0eeb78806e9ac3e8f391 /railties/guides/source/generators.textile
parent6958eac1a00a4ab33e3facc70c80a07492288196 (diff)
downloadrails-f44a0b1d524064a2e919cd10d3013db680af9b17.tar.gz
rails-f44a0b1d524064a2e919cd10d3013db680af9b17.tar.bz2
rails-f44a0b1d524064a2e919cd10d3013db680af9b17.zip
fix usage examples and more to use new invocations
Signed-off-by: Carl Lerche <carllerche@mac.com>
Diffstat (limited to 'railties/guides/source/generators.textile')
-rw-r--r--railties/guides/source/generators.textile28
1 files changed, 14 insertions, 14 deletions
diff --git a/railties/guides/source/generators.textile b/railties/guides/source/generators.textile
index fcd91f8956..4387fe3bd5 100644
--- a/railties/guides/source/generators.textile
+++ b/railties/guides/source/generators.textile
@@ -17,18 +17,18 @@ NOTE: This guide is about Rails generators for versions >= 3.0. Rails generators
h3. 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 +script/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+:
<shell>
$ rails myapp
$ cd myapp
-$ ruby script/generate
+$ rails generate
</shell>
You will get a list of all generators that comes with Rails. If you need a detailed description, for instance about the helper generator, you can simply do:
<shell>
-$ ruby script/generate helper --help
+$ rails generate helper --help
</shell>
h3. Creating your first generator
@@ -50,13 +50,13 @@ Our new generator is quite simple: it inherits from +Rails::Generators::Base+ an
To invoke our new generator, we just need to do:
<shell>
-$ ruby script/generate initializer
+$ rails generate initializer
</shell>
Before we go on, let's see our brand new generator description:
<shell>
-$ ruby script/generate initializer --help
+$ rails generate initializer --help
</shell>
Rails usually is 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:
@@ -77,7 +77,7 @@ h3. Creating generators with generators
A faster way to create a generator is using the generator's generator:
<shell>
-$ ruby script/generate generator initializer
+$ rails generate generator initializer
create lib/generators/initializer
create lib/generators/initializer/initializer_generator.rb
create lib/generators/initializer/USAGE
@@ -99,9 +99,9 @@ At first, we can notice that we are inheriting from +Rails::Generators::NamedBas
We can see that by invoking the description of this new generator (don't forget to delete the old generator file):
<shell>
-$ ruby script/generate initializer --help
+$ rails generate initializer --help
Usage:
- script/generate initializer NAME [options]
+ rails generate initializer NAME [options]
</shell>
We can also see in our new generator that it has a class method called +source_root+. This method points to where our generator templates will be placed and by default it points to the created directory under +RAILS_APP/lib/generators/initializer/templates+. In order to understand what a generator template means, let's create a file at +RAILS_APP/lib/generators/initializer/templates/initializer.rb+ with the following content:
@@ -128,7 +128,7 @@ end
And let's execute our generator:
<shell>
-$ ruby script/generate initializer foo
+$ rails generate initializer foo
</shell>
We can see that now a initializer named foo was created at +config/initializers/foo.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+.
@@ -166,7 +166,7 @@ end
Before we customize our workflow, let's first see how our scaffold looks like:
<shell>
-$ ruby script/generate scaffold User name:string
+$ rails generate scaffold User name:string
invoke active_record
create db/migrate/20091120125558_create_users.rb
create app/models/user.rb
@@ -212,7 +212,7 @@ If we generate another resource on scaffold, we can notice that neither styleshe
To show that, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator:
<shell>
-$ ruby script/generate generator my_helper
+$ rails generate generator my_helper
</shell>
After that, we can delete both 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:
@@ -232,7 +232,7 @@ end
We can try out our new generator by creating a helper for users:
<shell>
-$ ruby script/generate my_helper users
+$ rails generate my_helper users
</shell>
And it will generate the following helper file in app/helpers:
@@ -258,7 +258,7 @@ end
And see it in action when invoking generator once again:
<shell>
-$ ruby script/generate scaffold Post body:text
+$ rails generate scaffold Post body:text
[...]
invoke my_helper
create app/helpers/posts_helper.rb
@@ -343,7 +343,7 @@ Rails::Generators.fallbacks[:shoulda] = :test_unit
Now, if create a Comment scaffold, you will see that shoulda generators are being invoked, and at the end, they are just falling back to test unit generators:
<shell>
-$ ruby script/generate scaffold Comment body:text
+$ rails generate scaffold Comment body:text
invoke active_record
create db/migrate/20091120151323_create_comments.rb
create app/models/comment.rb