mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
remove generators section from command line guide in favor of the separate generator guide
This commit is contained in:
parent
5c53754f33
commit
def4a538cc
1 changed files with 0 additions and 94 deletions
|
@ -504,97 +504,3 @@ $ rails server mongrel
|
|||
=> Rails 3.1.0 application starting on http://0.0.0.0:3000
|
||||
...
|
||||
</shell>
|
||||
|
||||
h4. The Rails Generation: Generators
|
||||
|
||||
INFO: For a good rundown on generators, see "Understanding Generators":generators.html. A lot of its material is presented here.
|
||||
|
||||
Generators are code that generates code. Let's experiment by building one. Our generator will generate a text file.
|
||||
|
||||
The Rails generator by default looks in these places for available generators, where Rails.root is the root of your Rails application, like /home/foobar/commandsapp:
|
||||
|
||||
* Rails.root/lib/generators
|
||||
* Rails.root/vendor/generators
|
||||
* Inside any plugin with a directory like "generators" or "rails_generators"
|
||||
* ~/.rails/generators
|
||||
* Inside any Gem you have installed with a name ending in "_generator"
|
||||
* Inside any Gem installed with a "rails_generators" path, and a file ending in "_generator.rb"
|
||||
* Finally, the builtin Rails generators (controller, model, mailer, etc.)
|
||||
|
||||
Let's try the fourth option (in our home directory), which will be easy to clean up later:
|
||||
|
||||
<shell>
|
||||
$ mkdir -p ~/.rails/generators/tutorial_test/templates
|
||||
$ touch ~/.rails/generators/tutorial_test/templates/tutorial.erb
|
||||
$ touch ~/.rails/generators/tutorial_test/tutorial_test_generator.rb
|
||||
</shell>
|
||||
|
||||
We'll fill +tutorial_test_generator.rb+ out with:
|
||||
|
||||
<ruby>
|
||||
class TutorialTestGenerator < Rails::Generator::Base
|
||||
def initialize(*runtime_args)
|
||||
super(*runtime_args)
|
||||
@tut_args = runtime_args
|
||||
end
|
||||
|
||||
def manifest
|
||||
record do |m|
|
||||
m.directory "public"
|
||||
m.template "tutorial.erb", File.join("public", "tutorial.txt"),
|
||||
:assigns => { :args => @tut_args }
|
||||
end
|
||||
end
|
||||
end
|
||||
</ruby>
|
||||
|
||||
We take whatever args are supplied, save them to an instance variable, and literally copying from the Rails source, implement a +manifest+ method, which calls +record+ with a block, and we:
|
||||
|
||||
* Check there's a *public* directory. You bet there is.
|
||||
* Run the ERB template called "tutorial.erb".
|
||||
* Save it into "Rails.root/public/tutorial.txt".
|
||||
* Pass in the arguments we saved through the +:assigns+ parameter.
|
||||
|
||||
Next we'll build the template:
|
||||
|
||||
<shell>
|
||||
$ cat ~/.rails/generators/tutorial_test/templates/tutorial.erb
|
||||
I'm a template!
|
||||
|
||||
I got assigned some args:
|
||||
<%= require 'pp'; PP.pp(args, "") %>
|
||||
</shell>
|
||||
|
||||
Then we'll make sure it got included in the list of available generators:
|
||||
|
||||
<shell>
|
||||
$ rails generate
|
||||
...
|
||||
...
|
||||
Installed Generators
|
||||
User: tutorial_test
|
||||
</shell>
|
||||
|
||||
SWEET! Now let's generate some text, yeah!
|
||||
|
||||
<shell>
|
||||
$ rails generate tutorial_test arg1 arg2 arg3
|
||||
exists public
|
||||
create public/tutorial.txt
|
||||
</shell>
|
||||
|
||||
And the result:
|
||||
|
||||
<shell>
|
||||
$ cat public/tutorial.txt
|
||||
I'm a template!
|
||||
|
||||
I got assigned some args:
|
||||
[["arg1", "arg2", "arg3"],
|
||||
{:collision=>:ask,
|
||||
:quiet=>false,
|
||||
:generator=>"tutorial_test",
|
||||
:command=>:create}]
|
||||
</shell>
|
||||
|
||||
Tada!
|
||||
|
|
Loading…
Reference in a new issue