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".
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>
<ruby>
gem("rspec-rails", :group => "test")
gem("rspec-rails", :group => "test")
gem("cucumber-rails", :group => "test")
gem("cucumber-rails", :group => "test")
if yes?("Would you like to install Devise?")
if yes?("Would you like to install Devise?")
gem("devise")
gem("devise")
generate("devise:install")
generate("devise:install")
model_name = ask("What would you like the user model to be called? [user]")
model_name = ask("What would you like the user model to be called? [user]")
model_name = "user" if model_name.blank?
model_name = "user" if model_name.blank?
generate("devise", model_name)
generate("devise", model_name)
end
end
</ruby>
</ruby>
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.
@@ -470,10 +470,9 @@ Adds a line to +config/application.rb+ directly after the application class defi
...
@@ -470,10 +470,9 @@ Adds a line to +config/application.rb+ directly after the application class defi
This method can also take a block:
This method can also take a block:
<ruby>
<ruby>
application do
application do
"config.asset_host = 'http://example.com'"
"config.asset_host = 'http://example.com'"
end
end
end
</ruby>
</ruby>
Available options are:
Available options are:
...
@@ -481,9 +480,9 @@ Available options are:
...
@@ -481,9 +480,9 @@ 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>
<ruby>
application(nil, :env => "development") do
application(nil, :env => "development") do
"config.asset_host = 'http://localhost:3000'"
"config.asset_host = 'http://localhost:3000'"
end
end
</ruby>
</ruby>
h4. +git+
h4. +git+
...
@@ -526,9 +525,9 @@ Places a file into +lib+ which contains the specified code.
...
@@ -526,9 +525,9 @@ Places a file into +lib+ which contains the specified code.
This method also takes a block:
This method also takes a block:
<ruby>
<ruby>
lib("super_special.rb") do
lib("super_special.rb") do
puts "Super special!"
puts "Super special!"
end
end
</ruby>
</ruby>
h4. +rakefile+
h4. +rakefile+
...
@@ -542,13 +541,13 @@ Creates a Rake file in the +lib/tasks+ directory of the application.
...
@@ -542,13 +541,13 @@ Creates a Rake file in the +lib/tasks+ directory of the application.
This method also takes a block:
This method also takes a block:
<ruby>
<ruby>
rakefile("test.rake") do
rakefile("test.rake") do
%Q{
%Q{
task :rock => :environment do
task :rock => :environment do
puts "Rockin'"
puts "Rockin'"
end
end
}
}
end
end
</ruby>
</ruby>
h4. +initializer+
h4. +initializer+
...
@@ -562,9 +561,9 @@ Creates an initializer in the +config/initializers+ directory of the application
...
@@ -562,9 +561,9 @@ Creates an initializer in the +config/initializers+ directory of the application