From f44a0b1d524064a2e919cd10d3013db680af9b17 Mon Sep 17 00:00:00 2001 From: RomD Date: Sat, 6 Feb 2010 17:18:10 +0100 Subject: fix usage examples and more to use new invocations Signed-off-by: Carl Lerche --- railties/guides/source/generators.textile | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'railties/guides/source/generators.textile') 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+: $ rails myapp $ cd myapp -$ ruby script/generate +$ rails generate 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: -$ ruby script/generate helper --help +$ rails generate helper --help 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: -$ ruby script/generate initializer +$ rails generate initializer Before we go on, let's see our brand new generator description: -$ ruby script/generate initializer --help +$ rails generate initializer --help 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: -$ 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): -$ ruby script/generate initializer --help +$ rails generate initializer --help Usage: - script/generate initializer NAME [options] + rails generate initializer NAME [options] 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: -$ ruby script/generate initializer foo +$ rails generate initializer foo 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: -$ 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: -$ ruby script/generate generator my_helper +$ rails generate generator my_helper 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: -$ ruby script/generate my_helper users +$ rails generate my_helper users 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: -$ 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: -$ 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 -- cgit v1.2.3