railties 3.0.11 → 3.0.12.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rails/version.rb +2 -2
- metadata +305 -461
- data/guides/output/2_2_release_notes.html +0 -565
- data/guides/output/2_3_release_notes.html +0 -713
- data/guides/output/3_0_release_notes.html +0 -652
- data/guides/output/3_1_release_notes.html +0 -670
- data/guides/output/action_controller_overview.html +0 -925
- data/guides/output/action_mailer_basics.html +0 -658
- data/guides/output/action_view_overview.html +0 -1471
- data/guides/output/active_model_basics.html +0 -349
- data/guides/output/active_record_basics.html +0 -364
- data/guides/output/active_record_querying.html +0 -1272
- data/guides/output/active_record_validations_callbacks.html +0 -1292
- data/guides/output/active_resource_basics.html +0 -252
- data/guides/output/active_support_core_extensions.html +0 -3374
- data/guides/output/ajax_on_rails.html +0 -412
- data/guides/output/api_documentation_guidelines.html +0 -317
- data/guides/output/asset_pipeline.html +0 -691
- data/guides/output/association_basics.html +0 -1742
- data/guides/output/caching_with_rails.html +0 -533
- data/guides/output/command_line.html +0 -662
- data/guides/output/configuring.html +0 -811
- data/guides/output/contribute.html +0 -216
- data/guides/output/contributing_to_ruby_on_rails.html +0 -465
- data/guides/output/credits.html +0 -210
- data/guides/output/debugging_rails_applications.html +0 -791
- data/guides/output/engines.html +0 -673
- data/guides/output/form_helpers.html +0 -850
- data/guides/output/generators.html +0 -725
- data/guides/output/getting_started.html +0 -1980
- data/guides/output/i18n.html +0 -1054
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/challenge.png +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/customized_error_messages.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/error_messages.png +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +0 -5
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/jaimeiniesta.jpg +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/posts_index.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/rails_welcome.png +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/validation_error_messages.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +0 -300
- data/guides/output/initialization.html +0 -1087
- data/guides/output/javascripts/guides.js +0 -7
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
- data/guides/output/layout.html +0 -312
- data/guides/output/layouts_and_rendering.html +0 -1257
- data/guides/output/migrations.html +0 -751
- data/guides/output/nested_model_forms.html +0 -350
- data/guides/output/performance_testing.html +0 -858
- data/guides/output/plugins.html +0 -590
- data/guides/output/rails_application_templates.html +0 -368
- data/guides/output/rails_on_rack.html +0 -408
- data/guides/output/routing.html +0 -1246
- data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
- data/guides/output/security.html +0 -968
- data/guides/output/stylesheets/fixes.css +0 -16
- data/guides/output/stylesheets/main.css +0 -445
- data/guides/output/stylesheets/print.css +0 -52
- data/guides/output/stylesheets/reset.css +0 -43
- data/guides/output/stylesheets/style.css +0 -13
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
- data/guides/output/testing.html +0 -1182
@@ -1,725 +0,0 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
-
|
4
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
-
<head>
|
6
|
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
-
|
8
|
-
<title>Ruby on Rails Guides: Creating and Customizing Rails Generators & Templates</title>
|
9
|
-
|
10
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
11
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
12
|
-
|
13
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
14
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
15
|
-
|
16
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
17
|
-
</head>
|
18
|
-
<body class="guide">
|
19
|
-
<div id="topNav">
|
20
|
-
<div class="wrapper">
|
21
|
-
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
22
|
-
<a href="http://rubyonrails.org/">Overview</a> |
|
23
|
-
<a href="http://rubyonrails.org/download">Download</a> |
|
24
|
-
<a href="http://rubyonrails.org/deploy">Deploy</a> |
|
25
|
-
<a href="https://github.com/rails/rails">Code</a> |
|
26
|
-
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
|
27
|
-
<a href="http://rubyonrails.org/documentation">Documentation</a> |
|
28
|
-
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
|
29
|
-
<a href="http://rubyonrails.org/community">Community</a> |
|
30
|
-
<a href="http://weblog.rubyonrails.org/">Blog</a>
|
31
|
-
</div>
|
32
|
-
</div>
|
33
|
-
<div id="header">
|
34
|
-
<div class="wrapper clearfix">
|
35
|
-
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
36
|
-
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
|
37
|
-
<ul class="nav">
|
38
|
-
<li><a href="index.html">Home</a></li>
|
39
|
-
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
|
40
|
-
<div id="guides" class="clearfix" style="display: none;">
|
41
|
-
<hr />
|
42
|
-
<dl class="L">
|
43
|
-
<dt>Start Here</dt>
|
44
|
-
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
45
|
-
<dt>Models</dt>
|
46
|
-
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
|
47
|
-
<dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
|
48
|
-
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
49
|
-
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
50
|
-
<dt>Views</dt>
|
51
|
-
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
52
|
-
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
53
|
-
<dt>Controllers</dt>
|
54
|
-
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
55
|
-
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
56
|
-
</dl>
|
57
|
-
<dl class="R">
|
58
|
-
<dt>Digging Deeper</dt>
|
59
|
-
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
60
|
-
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
61
|
-
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
62
|
-
<dd><a href="testing.html">Testing Rails Applications</a></dd>
|
63
|
-
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
64
|
-
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
65
|
-
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
|
66
|
-
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
67
|
-
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
68
|
-
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
69
|
-
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
70
|
-
|
71
|
-
<dt>Extending Rails</dt>
|
72
|
-
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
73
|
-
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
74
|
-
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
75
|
-
|
76
|
-
<dt>Contributing to Ruby on Rails</dt>
|
77
|
-
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
78
|
-
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
79
|
-
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
80
|
-
|
81
|
-
<dt>Release Notes</dt>
|
82
|
-
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
83
|
-
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
84
|
-
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
85
|
-
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
86
|
-
</dl>
|
87
|
-
</div>
|
88
|
-
</li>
|
89
|
-
<li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
90
|
-
<li><a href="credits.html">Credits</a></li>
|
91
|
-
</ul>
|
92
|
-
</div>
|
93
|
-
</div>
|
94
|
-
<hr class="hide" />
|
95
|
-
|
96
|
-
<div id="feature">
|
97
|
-
<div class="wrapper">
|
98
|
-
<h2>Creating and Customizing Rails Generators & Templates</h2>
|
99
|
-
<p>Rails generators are an essential tool if you plan to improve your workflow. With this guide you will learn how to create generators and customize existing ones.</p>
|
100
|
-
<p>In this guide you will:</p>
|
101
|
-
<ul>
|
102
|
-
<li>Learn how to see which generators are available in your application</li>
|
103
|
-
<li>Create a generator using templates</li>
|
104
|
-
<li>Learn how Rails searches for generators before invoking them</li>
|
105
|
-
<li>Customize your scaffold by creating new generators</li>
|
106
|
-
<li>Customize your scaffold by changing generator templates</li>
|
107
|
-
<li>Learn how to use fallbacks to avoid overwriting a huge set of generators</li>
|
108
|
-
<li>Learn how to create an application template</li>
|
109
|
-
</ul>
|
110
|
-
|
111
|
-
<div id="subCol">
|
112
|
-
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
113
|
-
<ol class="chapters">
|
114
|
-
<li><a href="#first-contact">First Contact</a></li><li><a href="#creating-your-first-generator">Creating Your First Generator</a></li><li><a href="#creating-generators-with-generators">Creating Generators with Generators</a></li><li><a href="#generators-lookup">Generators Lookup</a></li><li><a href="#customizing-your-workflow">Customizing Your Workflow</a></li><li><a href="#customizing-your-workflow-by-changing-generators-templates">Customizing Your Workflow by Changing Generators Templates</a></li><li><a href="#adding-generators-fallbacks">Adding Generators Fallbacks</a></li><li><a href="#application-templates">Application Templates</a></li><li><a href="#generator-methods">Generator methods</a><ul><li><a href="#plugin"><tt>plugin</tt></a></li> <li><a href="#gem"><tt>gem</tt></a></li> <li><a href="#gem_group"><tt>gem_group</tt></a></li> <li><a href="#add_source"><tt>add_source</tt></a></li> <li><a href="#application"><tt>application</tt></a></li> <li><a href="#git"><tt>git</tt></a></li> <li><a href="#vendor"><tt>vendor</tt></a></li> <li><a href="#lib"><tt>lib</tt></a></li> <li><a href="#rakefile"><tt>rakefile</tt></a></li> <li><a href="#initializer"><tt>initializer</tt></a></li> <li><a href="#generate"><tt>generate</tt></a></li> <li><a href="#rake"><tt>rake</tt></a></li> <li><a href="#capify"><tt>capify!</tt></a></li> <li><a href="#route"><tt>route</tt></a></li> <li><a href="#readme"><tt>readme</tt></a></li></ul></li></ol></div>
|
115
|
-
</div>
|
116
|
-
</div>
|
117
|
-
|
118
|
-
<div id="container">
|
119
|
-
<div class="wrapper">
|
120
|
-
<div id="mainCol">
|
121
|
-
<div class="note"><p>This guide is about generators in Rails 3, previous versions are not covered.</p></div>
|
122
|
-
<h3 id="first-contact">1 First Contact</h3>
|
123
|
-
<p>When you create an application using the <tt>rails</tt> command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking <tt>rails generate</tt>:</p>
|
124
|
-
<div class="code_container">
|
125
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
126
|
-
$ rails new myapp
|
127
|
-
$ cd myapp
|
128
|
-
$ rails generate
|
129
|
-
</pre>
|
130
|
-
</div>
|
131
|
-
<p>You will get a list of all generators that comes with Rails. If you need a detailed description of the helper generator, for example, you can simply do:</p>
|
132
|
-
<div class="code_container">
|
133
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
134
|
-
$ rails generate helper --help
|
135
|
-
</pre>
|
136
|
-
</div>
|
137
|
-
<h3 id="creating-your-first-generator">2 Creating Your First Generator</h3>
|
138
|
-
<p>Since Rails 3.0, generators are built on top of <a href="https://github.com/wycats/thor">Thor</a>. Thor provides powerful options parsing and a great <span class="caps">API</span> for manipulating files. For instance, let’s build a generator that creates an initializer file named <tt>initializer.rb</tt> inside <tt>config/initializers</tt>.</p>
|
139
|
-
<p>The first step is to create a file at <tt>lib/generators/initializer_generator.rb</tt> with the following content:</p>
|
140
|
-
<div class="code_container">
|
141
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
142
|
-
class InitializerGenerator < Rails::Generators::Base
|
143
|
-
def create_initializer_file
|
144
|
-
create_file "config/initializers/initializer.rb", "# Add initialization content here"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
</pre>
|
148
|
-
</div>
|
149
|
-
<div class="note"><p><tt>create_file</tt> is a method provided by <tt>Thor::Actions</tt>. Documentation for <tt>create_file</tt> and other Thor methods can be found in <a href="http://rdoc.info/github/wycats/thor/master/Thor/Actions.html">Thor’s documentation</a></p></div>
|
150
|
-
<p>Our new generator is quite simple: it inherits from <tt>Rails::Generators::Base</tt> and has one method definition. Each public method in the generator is executed when a generator is invoked. Finally, we invoke the <tt>create_file</tt> method that will create a file at the given destination with the given content. If you are familiar with the Rails Application Templates <span class="caps">API</span>, you’ll feel right at home with the new generators <span class="caps">API</span>.</p>
|
151
|
-
<p>To invoke our new generator, we just need to do:</p>
|
152
|
-
<div class="code_container">
|
153
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
154
|
-
$ rails generate initializer
|
155
|
-
</pre>
|
156
|
-
</div>
|
157
|
-
<p>Before we go on, let’s see our brand new generator description:</p>
|
158
|
-
<div class="code_container">
|
159
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
160
|
-
$ rails generate initializer --help
|
161
|
-
</pre>
|
162
|
-
</div>
|
163
|
-
<p>Rails is usually able to generate good descriptions if a generator is namespaced, as <tt>ActiveRecord::Generators::ModelGenerator</tt>, but not in this particular case. We can solve this problem in two ways. The first one is calling <tt>desc</tt> inside our generator:</p>
|
164
|
-
<div class="code_container">
|
165
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
166
|
-
class InitializerGenerator < Rails::Generators::Base
|
167
|
-
desc "This generator creates an initializer file at config/initializers"
|
168
|
-
def create_initializer_file
|
169
|
-
create_file "config/initializers/initializer.rb", "# Add initialization content here"
|
170
|
-
end
|
171
|
-
end
|
172
|
-
</pre>
|
173
|
-
</div>
|
174
|
-
<p>Now we can see the new description by invoking <tt>--help</tt> on the new generator. The second way to add a description is by creating a file named <tt>USAGE</tt> in the same directory as our generator. We are going to do that in the next step.</p>
|
175
|
-
<h3 id="creating-generators-with-generators">3 Creating Generators with Generators</h3>
|
176
|
-
<p>Generators themselves have a generator:</p>
|
177
|
-
<div class="code_container">
|
178
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
179
|
-
$ rails generate generator initializer
|
180
|
-
create lib/generators/initializer
|
181
|
-
create lib/generators/initializer/initializer_generator.rb
|
182
|
-
create lib/generators/initializer/USAGE
|
183
|
-
create lib/generators/initializer/templates
|
184
|
-
</pre>
|
185
|
-
</div>
|
186
|
-
<p>This is the generator just created:</p>
|
187
|
-
<div class="code_container">
|
188
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
189
|
-
class InitializerGenerator < Rails::Generators::NamedBase
|
190
|
-
source_root File.expand_path("../templates", __FILE__)
|
191
|
-
end
|
192
|
-
</pre>
|
193
|
-
</div>
|
194
|
-
<p>First, notice that we are inheriting from <tt>Rails::Generators::NamedBase</tt> instead of <tt>Rails::Generators::Base</tt>. 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 <tt>name</tt>.</p>
|
195
|
-
<p>We can see that by invoking the description of this new generator (don’t forget to delete the old generator file):</p>
|
196
|
-
<div class="code_container">
|
197
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
198
|
-
$ rails generate initializer --help
|
199
|
-
Usage:
|
200
|
-
rails generate initializer NAME [options]
|
201
|
-
</pre>
|
202
|
-
</div>
|
203
|
-
<p>We can also see that our new generator has a class method called <tt>source_root</tt>. This method points to where our generator templates will be placed, if any, and by default it points to the created directory <tt>lib/generators/initializer/templates</tt>.</p>
|
204
|
-
<p>In order to understand what a generator template means, let’s create the file <tt>lib/generators/initializer/templates/initializer.rb</tt> with the following content:</p>
|
205
|
-
<div class="code_container">
|
206
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
207
|
-
# Add initialization content here
|
208
|
-
</pre>
|
209
|
-
</div>
|
210
|
-
<p>And now let’s change the generator to copy this template when invoked:</p>
|
211
|
-
<div class="code_container">
|
212
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
213
|
-
class InitializerGenerator < Rails::Generators::NamedBase
|
214
|
-
source_root File.expand_path("../templates", __FILE__)
|
215
|
-
|
216
|
-
def copy_initializer_file
|
217
|
-
copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
|
218
|
-
end
|
219
|
-
end
|
220
|
-
</pre>
|
221
|
-
</div>
|
222
|
-
<p>And let’s execute our generator:</p>
|
223
|
-
<div class="code_container">
|
224
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
225
|
-
$ rails generate initializer core_extensions
|
226
|
-
</pre>
|
227
|
-
</div>
|
228
|
-
<p>We can see that now an initializer named core_extensions was created at <tt>config/initializers/core_extensions.rb</tt> with the contents of our template. That means that <tt>copy_file</tt> copied a file in our source root to the destination path we gave. The method <tt>file_name</tt> is automatically created when we inherit from <tt>Rails::Generators::NamedBase</tt>.</p>
|
229
|
-
<p>The methods that are available for generators are covered in the <a href="#generator-methods">final section</a> of this guide.</p>
|
230
|
-
<h3 id="generators-lookup">4 Generators Lookup</h3>
|
231
|
-
<p>When you run <tt>rails generate initializer core_extensions</tt> Rails requires these files in turn until one is found:</p>
|
232
|
-
<div class="code_container">
|
233
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
234
|
-
rails/generators/initializer/initializer_generator.rb
|
235
|
-
generators/initializer/initializer_generator.rb
|
236
|
-
rails/generators/initializer_generator.rb
|
237
|
-
generators/initializer_generator.rb
|
238
|
-
</pre>
|
239
|
-
</div>
|
240
|
-
<p>If none is found you get an error message.</p>
|
241
|
-
<div class="info"><p>The examples above put files under the application’s <tt>lib</tt> because said directory belongs to <tt>$LOAD_PATH</tt>.</p></div>
|
242
|
-
<h3 id="customizing-your-workflow">5 Customizing Your Workflow</h3>
|
243
|
-
<p>Rails own generators are flexible enough to let you customize scaffolding. They can be configured in <tt>config/application.rb</tt>, these are some defaults:</p>
|
244
|
-
<div class="code_container">
|
245
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
246
|
-
config.generators do |g|
|
247
|
-
g.orm :active_record
|
248
|
-
g.template_engine :erb
|
249
|
-
g.test_framework :test_unit, :fixture => true
|
250
|
-
end
|
251
|
-
</pre>
|
252
|
-
</div>
|
253
|
-
<p>Before we customize our workflow, let’s first see what our scaffold looks like:</p>
|
254
|
-
<div class="code_container">
|
255
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
256
|
-
$ rails generate scaffold User name:string
|
257
|
-
invoke active_record
|
258
|
-
create db/migrate/20091120125558_create_users.rb
|
259
|
-
create app/models/user.rb
|
260
|
-
invoke test_unit
|
261
|
-
create test/unit/user_test.rb
|
262
|
-
create test/fixtures/users.yml
|
263
|
-
route resources :users
|
264
|
-
invoke scaffold_controller
|
265
|
-
create app/controllers/users_controller.rb
|
266
|
-
invoke erb
|
267
|
-
create app/views/users
|
268
|
-
create app/views/users/index.html.erb
|
269
|
-
create app/views/users/edit.html.erb
|
270
|
-
create app/views/users/show.html.erb
|
271
|
-
create app/views/users/new.html.erb
|
272
|
-
create app/views/users/_form.html.erb
|
273
|
-
invoke test_unit
|
274
|
-
create test/functional/users_controller_test.rb
|
275
|
-
invoke helper
|
276
|
-
create app/helpers/users_helper.rb
|
277
|
-
invoke test_unit
|
278
|
-
create test/unit/helpers/users_helper_test.rb
|
279
|
-
invoke stylesheets
|
280
|
-
create app/assets/stylesheets/scaffold.css
|
281
|
-
</pre>
|
282
|
-
</div>
|
283
|
-
<p>Looking at this output, it’s easy to understand how generators work in Rails 3.0 and above. The scaffold generator doesn’t actually generate anything, it just invokes others to do the work. This allows us to add/replace/remove any of those invocations. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication.</p>
|
284
|
-
<p>Our first customization on the workflow will be to stop generating stylesheets and test fixtures for scaffolds. We can achieve that by changing our configuration to the following:</p>
|
285
|
-
<div class="code_container">
|
286
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
287
|
-
config.generators do |g|
|
288
|
-
g.orm :active_record
|
289
|
-
g.template_engine :erb
|
290
|
-
g.test_framework :test_unit, :fixture => false
|
291
|
-
g.stylesheets false
|
292
|
-
end
|
293
|
-
</pre>
|
294
|
-
</div>
|
295
|
-
<p>If we generate another resource with the scaffold generator, we can see that neither stylesheets nor fixtures are created anymore. If you want to customize it further, for example to use DataMapper and RSpec instead of Active Record and TestUnit, it’s just a matter of adding their gems to your application and configuring your generators.</p>
|
296
|
-
<p>To demonstrate this, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator within the rails namespace, as this is where rails searches for generators used as hooks:</p>
|
297
|
-
<div class="code_container">
|
298
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
299
|
-
$ rails generate generator rails/my_helper
|
300
|
-
</pre>
|
301
|
-
</div>
|
302
|
-
<p>After that, we can delete both the <tt>templates</tt> directory and the <tt>source_root</tt> class method from our new generators, because we are not going to need them. So our new generator looks like the following:</p>
|
303
|
-
<div class="code_container">
|
304
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
305
|
-
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
|
306
|
-
def create_helper_file
|
307
|
-
create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
|
308
|
-
module #{class_name}Helper
|
309
|
-
attr_reader :#{plural_name}, :#{plural_name.singularize}
|
310
|
-
end
|
311
|
-
FILE
|
312
|
-
end
|
313
|
-
end
|
314
|
-
</pre>
|
315
|
-
</div>
|
316
|
-
<p>We can try out our new generator by creating a helper for users:</p>
|
317
|
-
<div class="code_container">
|
318
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
319
|
-
$ rails generate my_helper products
|
320
|
-
</pre>
|
321
|
-
</div>
|
322
|
-
<p>And it will generate the following helper file in <tt>app/helpers</tt>:</p>
|
323
|
-
<div class="code_container">
|
324
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
325
|
-
module ProductsHelper
|
326
|
-
attr_reader :products, :product
|
327
|
-
end
|
328
|
-
</pre>
|
329
|
-
</div>
|
330
|
-
<p>Which is what we expected. We can now tell scaffold to use our new helper generator by editing <tt>config/application.rb</tt> once again:</p>
|
331
|
-
<div class="code_container">
|
332
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
333
|
-
config.generators do |g|
|
334
|
-
g.orm :active_record
|
335
|
-
g.template_engine :erb
|
336
|
-
g.test_framework :test_unit, :fixture => false
|
337
|
-
g.stylesheets false
|
338
|
-
g.helper :my_helper
|
339
|
-
end
|
340
|
-
</pre>
|
341
|
-
</div>
|
342
|
-
<p>and see it in action when invoking the generator:</p>
|
343
|
-
<div class="code_container">
|
344
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
345
|
-
$ rails generate scaffold Post body:text
|
346
|
-
[...]
|
347
|
-
invoke my_helper
|
348
|
-
create app/helpers/posts_helper.rb
|
349
|
-
</pre>
|
350
|
-
</div>
|
351
|
-
<p>We can notice on the output that our new helper was invoked instead of the Rails default. However one thing is missing, which is tests for our new generator and to do that, we are going to reuse old helpers test generators.</p>
|
352
|
-
<p>Since Rails 3.0, this is easy to do due to the hooks concept. Our new helper does not need to be focused in one specific test framework, it can simply provide a hook and a test framework just needs to implement this hook in order to be compatible.</p>
|
353
|
-
<p>To do that, we can change the generator this way:</p>
|
354
|
-
<div class="code_container">
|
355
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
356
|
-
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
|
357
|
-
def create_helper_file
|
358
|
-
create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
|
359
|
-
module #{class_name}Helper
|
360
|
-
attr_reader :#{plural_name}, :#{plural_name.singularize}
|
361
|
-
end
|
362
|
-
FILE
|
363
|
-
end
|
364
|
-
|
365
|
-
hook_for :test_framework
|
366
|
-
end
|
367
|
-
</pre>
|
368
|
-
</div>
|
369
|
-
<p>Now, when the helper generator is invoked and TestUnit is configured as the test framework, it will try to invoke both <tt>Rails::TestUnitGenerator</tt> and <tt>TestUnit::MyHelperGenerator</tt>. Since none of those are defined, we can tell our generator to invoke <tt>TestUnit::Generators::HelperGenerator</tt> instead, which is defined since it’s a Rails generator. To do that, we just need to add:</p>
|
370
|
-
<div class="code_container">
|
371
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
372
|
-
# Search for :helper instead of :my_helper
|
373
|
-
hook_for :test_framework, :as => :helper
|
374
|
-
</pre>
|
375
|
-
</div>
|
376
|
-
<p>And now you can re-run scaffold for another resource and see it generating tests as well!</p>
|
377
|
-
<h3 id="customizing-your-workflow-by-changing-generators-templates">6 Customizing Your Workflow by Changing Generators Templates</h3>
|
378
|
-
<p>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 <tt>Rails::Generators::HelperGenerator</tt>.</p>
|
379
|
-
<p>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 <tt>lib/templates</tt>. Since we want to customize <tt>Rails::Generators::HelperGenerator</tt>, we can do that by simply making a template copy inside <tt>lib/templates/rails/helper</tt> with the name <tt>helper.rb</tt>. So let’s create that file with the following content:</p>
|
380
|
-
<div class="code_container">
|
381
|
-
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
382
|
-
module <%= class_name %>Helper
|
383
|
-
attr_reader :<%= plural_name %>, <%= plural_name.singularize %>
|
384
|
-
end
|
385
|
-
</pre>
|
386
|
-
</div>
|
387
|
-
<p>and revert the last change in <tt>config/application.rb</tt>:</p>
|
388
|
-
<div class="code_container">
|
389
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
390
|
-
config.generators do |g|
|
391
|
-
g.orm :active_record
|
392
|
-
g.template_engine :erb
|
393
|
-
g.test_framework :test_unit, :fixture => false
|
394
|
-
g.stylesheets false
|
395
|
-
end
|
396
|
-
</pre>
|
397
|
-
</div>
|
398
|
-
<p>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 <tt>edit.html.erb</tt>, <tt>index.html.erb</tt> and so on inside <tt>lib/templates/erb/scaffold</tt>.</p>
|
399
|
-
<h3 id="adding-generators-fallbacks">7 Adding Generators Fallbacks</h3>
|
400
|
-
<p>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 <a href="https://github.com/thoughtbot/shoulda">shoulda</a> 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 <tt>TestUnit</tt> generator if none was found under the <tt>Shoulda</tt> namespace.</p>
|
401
|
-
<p>We can easily simulate this behavior by changing our <tt>config/application.rb</tt> once again:</p>
|
402
|
-
<div class="code_container">
|
403
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
404
|
-
config.generators do |g|
|
405
|
-
g.orm :active_record
|
406
|
-
g.template_engine :erb
|
407
|
-
g.test_framework :shoulda, :fixture => false
|
408
|
-
g.stylesheets false
|
409
|
-
|
410
|
-
# Add a fallback!
|
411
|
-
g.fallbacks[:shoulda] = :test_unit
|
412
|
-
end
|
413
|
-
</pre>
|
414
|
-
</div>
|
415
|
-
<p>Now, if you create a Comment scaffold, you will see that the shoulda generators are being invoked, and at the end, they are just falling back to TestUnit generators:</p>
|
416
|
-
<div class="code_container">
|
417
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
418
|
-
$ rails generate scaffold Comment body:text
|
419
|
-
invoke active_record
|
420
|
-
create db/migrate/20091120151323_create_comments.rb
|
421
|
-
create app/models/comment.rb
|
422
|
-
invoke shoulda
|
423
|
-
create test/unit/comment_test.rb
|
424
|
-
create test/fixtures/comments.yml
|
425
|
-
route resources :comments
|
426
|
-
invoke scaffold_controller
|
427
|
-
create app/controllers/comments_controller.rb
|
428
|
-
invoke erb
|
429
|
-
create app/views/comments
|
430
|
-
create app/views/comments/index.html.erb
|
431
|
-
create app/views/comments/edit.html.erb
|
432
|
-
create app/views/comments/show.html.erb
|
433
|
-
create app/views/comments/new.html.erb
|
434
|
-
create app/views/comments/_form.html.erb
|
435
|
-
create app/views/layouts/comments.html.erb
|
436
|
-
invoke shoulda
|
437
|
-
create test/functional/comments_controller_test.rb
|
438
|
-
invoke my_helper
|
439
|
-
create app/helpers/comments_helper.rb
|
440
|
-
invoke shoulda
|
441
|
-
create test/unit/helpers/comments_helper_test.rb
|
442
|
-
</pre>
|
443
|
-
</div>
|
444
|
-
<p>Fallbacks allow your generators to have a single responsibility, increasing code reuse and reducing the amount of duplication.</p>
|
445
|
-
<h3 id="application-templates">8 Application Templates</h3>
|
446
|
-
<p>Now that you’ve seen how generators can be used <em>inside</em> an application, did you know they can also be used to <em>generate</em> applications too? This kind of generator is referred as a “template”.</p>
|
447
|
-
<div class="code_container">
|
448
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
449
|
-
gem("rspec-rails", :group => "test")
|
450
|
-
gem("cucumber-rails", :group => "test")
|
451
|
-
|
452
|
-
if yes?("Would you like to install Devise?")
|
453
|
-
gem("devise")
|
454
|
-
generate("devise:install")
|
455
|
-
model_name = ask("What would you like the user model to be called? [user]")
|
456
|
-
model_name = "user" if model_name.blank?
|
457
|
-
generate("devise", model_name)
|
458
|
-
end
|
459
|
-
</pre>
|
460
|
-
</div>
|
461
|
-
<p>In the above template we specify that the application relies on the <tt>rspec-rails</tt> and <tt>cucumber-rails</tt> gem so these two will be added to the <tt>test</tt> group in the <tt>Gemfile</tt>. 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 <tt>Gemfile</tt> outside of any group and then runs the <tt>devise:install</tt> generator. This template then takes the users input and runs the <tt>devise</tt> generator, with the user’s answer from the last question being passed to this generator.</p>
|
462
|
-
<p>Imagine that this template was in a file called <tt>template.rb</tt>. We can use it to modify the outcome of the <tt>rails new</tt> command by using the <tt>-m</tt> option and passing in the filename:</p>
|
463
|
-
<div class="code_container">
|
464
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
465
|
-
$ rails new thud -m template.rb
|
466
|
-
</pre>
|
467
|
-
</div>
|
468
|
-
<p>This command will generate the <tt>Thud</tt> application, and then apply the template to the generated output.</p>
|
469
|
-
<p>Templates don’t have to be stored on the local system, the <tt>-m</tt> option also supports online templates:</p>
|
470
|
-
<div class="code_container">
|
471
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
472
|
-
$ rails new thud -m https://gist.github.com/722911.txt
|
473
|
-
</pre>
|
474
|
-
</div>
|
475
|
-
<p>Whilst the final section of this guide doesn’t cover how to generate the most awesome template known to man, it will take you through the methods available at your disposal so that you can develop it yourself. These same methods are also available for generators.</p>
|
476
|
-
<h3 id="generator-methods">9 Generator methods</h3>
|
477
|
-
<p>The following are methods available for both generators and templates for Rails.</p>
|
478
|
-
<div class="note"><p>Methods provided by Thor are not covered this guide and can be found in <a href="http://rdoc.info/github/wycats/thor/master/Thor/Actions.html">Thor’s documentation</a></p></div>
|
479
|
-
<h4 id="plugin">9.1 <tt>plugin</tt></h4>
|
480
|
-
<p><tt>plugin</tt> will install a plugin into the current application.</p>
|
481
|
-
<div class="code_container">
|
482
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
483
|
-
plugin("dynamic-form", :git => "git://github.com/rails/dynamic-form.git")
|
484
|
-
</pre>
|
485
|
-
</div>
|
486
|
-
<p>Available options are:</p>
|
487
|
-
<ul>
|
488
|
-
<li><tt>:git</tt> – Takes the path to the git repository where this plugin can be found.</li>
|
489
|
-
<li><tt>:branch</tt> – The name of the branch of the git repository where the plugin is found.</li>
|
490
|
-
<li><tt>:submodule</tt> – Set to <tt>true</tt> for the plugin to be installed as a submodule. Defaults to <tt>false</tt>.</li>
|
491
|
-
<li><tt>:svn</tt> – Takes the path to the svn repository where this plugin can be found.</li>
|
492
|
-
<li><tt>:revision</tt> – The revision of the plugin in an <span class="caps">SVN</span> repository.</li>
|
493
|
-
</ul>
|
494
|
-
<h4 id="gem">9.2 <tt>gem</tt></h4>
|
495
|
-
<p>Specifies a gem dependency of the application.</p>
|
496
|
-
<div class="code_container">
|
497
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
498
|
-
gem("rspec", :group => "test", :version => "2.1.0")
|
499
|
-
gem("devise", "1.1.5")
|
500
|
-
</pre>
|
501
|
-
</div>
|
502
|
-
<p>Available options are:</p>
|
503
|
-
<ul>
|
504
|
-
<li><tt>:group</tt> – The group in the <tt>Gemfile</tt> where this gem should go.</li>
|
505
|
-
<li><tt>:version</tt> – The version string of the gem you want to use. Can also be specified as the second argument to the method.</li>
|
506
|
-
<li><tt>:git</tt> – The <span class="caps">URL</span> to the git repository for this gem.</li>
|
507
|
-
</ul>
|
508
|
-
<p>Any additional options passed to this method are put on the end of the line:</p>
|
509
|
-
<div class="code_container">
|
510
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
511
|
-
gem("devise", :git => "git://github.com/plataformatec/devise", :branch => "master")
|
512
|
-
</pre>
|
513
|
-
</div>
|
514
|
-
<p>The above code will put the following line into <tt>Gemfile</tt>:</p>
|
515
|
-
<div class="code_container">
|
516
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
517
|
-
gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"
|
518
|
-
</pre>
|
519
|
-
</div>
|
520
|
-
<h4 id="gem_group">9.3 <tt>gem_group</tt></h4>
|
521
|
-
<p>Wraps gem entries inside a group:</p>
|
522
|
-
<div class="code_container">
|
523
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
524
|
-
gem_group :development, :test do
|
525
|
-
gem "rspec-rails"
|
526
|
-
end
|
527
|
-
</pre>
|
528
|
-
</div>
|
529
|
-
<h4 id="add_source">9.4 <tt>add_source</tt></h4>
|
530
|
-
<p>Adds a specified source to <tt>Gemfile</tt>:</p>
|
531
|
-
<div class="code_container">
|
532
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
533
|
-
add_source "http://gems.github.com"
|
534
|
-
</pre>
|
535
|
-
</div>
|
536
|
-
<h4 id="application">9.5 <tt>application</tt></h4>
|
537
|
-
<p>Adds a line to <tt>config/application.rb</tt> directly after the application class definition.</p>
|
538
|
-
<div class="code_container">
|
539
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
540
|
-
application "config.asset_host = 'http://example.com'"
|
541
|
-
</pre>
|
542
|
-
</div>
|
543
|
-
<p>This method can also take a block:</p>
|
544
|
-
<div class="code_container">
|
545
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
546
|
-
application do
|
547
|
-
"config.asset_host = 'http://example.com'"
|
548
|
-
end
|
549
|
-
</pre>
|
550
|
-
</div>
|
551
|
-
<p>Available options are:</p>
|
552
|
-
<ul>
|
553
|
-
<li><tt>:env</tt> – Specify an environment for this configuration option. If you wish to use this option with the block syntax the recommended syntax is as follows:</li>
|
554
|
-
</ul>
|
555
|
-
<div class="code_container">
|
556
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
557
|
-
application(nil, :env => "development") do
|
558
|
-
"config.asset_host = 'http://localhost:3000'"
|
559
|
-
end
|
560
|
-
</pre>
|
561
|
-
</div>
|
562
|
-
<h4 id="git">9.6 <tt>git</tt></h4>
|
563
|
-
<p>Runs the specified git command:</p>
|
564
|
-
<div class="code_container">
|
565
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
566
|
-
git :init
|
567
|
-
git :add => "."
|
568
|
-
git :commit => "-m First commit!"
|
569
|
-
git :add => "onefile.rb", :rm => "badfile.cxx"
|
570
|
-
</pre>
|
571
|
-
</div>
|
572
|
-
<p>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.</p>
|
573
|
-
<h4 id="vendor">9.7 <tt>vendor</tt></h4>
|
574
|
-
<p>Places a file into <tt>vendor</tt> which contains the specified code.</p>
|
575
|
-
<div class="code_container">
|
576
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
577
|
-
vendor("sekrit.rb", '#top secret stuff')
|
578
|
-
</pre>
|
579
|
-
</div>
|
580
|
-
<p>This method also takes a block:</p>
|
581
|
-
<div class="code_container">
|
582
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
583
|
-
vendor("seeds.rb") do
|
584
|
-
"puts 'in ur app, seeding ur database'"
|
585
|
-
end
|
586
|
-
</pre>
|
587
|
-
</div>
|
588
|
-
<h4 id="lib">9.8 <tt>lib</tt></h4>
|
589
|
-
<p>Places a file into <tt>lib</tt> which contains the specified code.</p>
|
590
|
-
<div class="code_container">
|
591
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
592
|
-
lib("special.rb", 'p Rails.root')
|
593
|
-
</pre>
|
594
|
-
</div>
|
595
|
-
<p>This method also takes a block:</p>
|
596
|
-
<div class="code_container">
|
597
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
598
|
-
lib("super_special.rb") do
|
599
|
-
puts "Super special!"
|
600
|
-
end
|
601
|
-
</pre>
|
602
|
-
</div>
|
603
|
-
<h4 id="rakefile">9.9 <tt>rakefile</tt></h4>
|
604
|
-
<p>Creates a Rake file in the <tt>lib/tasks</tt> directory of the application.</p>
|
605
|
-
<div class="code_container">
|
606
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
607
|
-
rakefile("test.rake", 'hello there')
|
608
|
-
</pre>
|
609
|
-
</div>
|
610
|
-
<p>This method also takes a block:</p>
|
611
|
-
<div class="code_container">
|
612
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
613
|
-
rakefile("test.rake") do
|
614
|
-
%Q{
|
615
|
-
task :rock => :environment do
|
616
|
-
puts "Rockin'"
|
617
|
-
end
|
618
|
-
}
|
619
|
-
end
|
620
|
-
</pre>
|
621
|
-
</div>
|
622
|
-
<h4 id="initializer">9.10 <tt>initializer</tt></h4>
|
623
|
-
<p>Creates an initializer in the <tt>config/initializers</tt> directory of the application:</p>
|
624
|
-
<div class="code_container">
|
625
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
626
|
-
initializer("begin.rb", "puts 'this is the beginning'")
|
627
|
-
</pre>
|
628
|
-
</div>
|
629
|
-
<p>This method also takes a block:</p>
|
630
|
-
<div class="code_container">
|
631
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
632
|
-
initializer("begin.rb") do
|
633
|
-
puts "Almost done!"
|
634
|
-
end
|
635
|
-
</pre>
|
636
|
-
</div>
|
637
|
-
<h4 id="generate">9.11 <tt>generate</tt></h4>
|
638
|
-
<p>Runs the specified generator where the first argument is the generator name and the remaining arguments are passed directly to the generator.</p>
|
639
|
-
<div class="code_container">
|
640
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
641
|
-
generate("scaffold", "forums title:string description:text")
|
642
|
-
</pre>
|
643
|
-
</div>
|
644
|
-
<h4 id="rake">9.12 <tt>rake</tt></h4>
|
645
|
-
<p>Runs the specified Rake task.</p>
|
646
|
-
<div class="code_container">
|
647
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
648
|
-
rake("db:migrate")
|
649
|
-
</pre>
|
650
|
-
</div>
|
651
|
-
<p>Available options are:</p>
|
652
|
-
<ul>
|
653
|
-
<li><tt>:env</tt> – Specifies the environment in which to run this rake task.</li>
|
654
|
-
<li><tt>:sudo</tt> – Whether or not to run this task using <tt>sudo</tt>. Defaults to <tt>false</tt>.</li>
|
655
|
-
</ul>
|
656
|
-
<h4 id="capify">9.13 <tt>capify!</tt></h4>
|
657
|
-
<p>Runs the <tt>capify</tt> command from Capistrano at the root of the application which generates Capistrano configuration.</p>
|
658
|
-
<div class="code_container">
|
659
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
660
|
-
capify!
|
661
|
-
</pre>
|
662
|
-
</div>
|
663
|
-
<h4 id="route">9.14 <tt>route</tt></h4>
|
664
|
-
<p>Adds text to the <tt>config/routes.rb</tt> file:</p>
|
665
|
-
<div class="code_container">
|
666
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
667
|
-
route("resources :people")
|
668
|
-
</pre>
|
669
|
-
</div>
|
670
|
-
<h4 id="readme">9.15 <tt>readme</tt></h4>
|
671
|
-
<p>Output the contents of a file in the template’s <tt>source_path</tt>, usually a <span class="caps">README</span>.</p>
|
672
|
-
<div class="code_container">
|
673
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
674
|
-
readme("README")
|
675
|
-
</pre>
|
676
|
-
</div>
|
677
|
-
|
678
|
-
<h3>Feedback</h3>
|
679
|
-
<p>
|
680
|
-
You're encouraged to help improve the quality of this guide.
|
681
|
-
</p>
|
682
|
-
<p>
|
683
|
-
If you see any typos or factual errors you are confident to
|
684
|
-
patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
|
685
|
-
and push the change yourself. That branch of Rails has public write access.
|
686
|
-
Commits are still reviewed, but that happens after you've submitted your
|
687
|
-
contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
|
688
|
-
cross-merged with master periodically.
|
689
|
-
</p>
|
690
|
-
<p>
|
691
|
-
You may also find incomplete content, or stuff that is not up to date.
|
692
|
-
Please do add any missing documentation for master. Check the
|
693
|
-
<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
694
|
-
for style and conventions.
|
695
|
-
</p>
|
696
|
-
<p>
|
697
|
-
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
698
|
-
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
699
|
-
</p>
|
700
|
-
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
701
|
-
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
702
|
-
</p>
|
703
|
-
</div>
|
704
|
-
</div>
|
705
|
-
</div>
|
706
|
-
|
707
|
-
<hr class="hide" />
|
708
|
-
<div id="footer">
|
709
|
-
<div class="wrapper">
|
710
|
-
<p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
|
711
|
-
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
712
|
-
</div>
|
713
|
-
</div>
|
714
|
-
|
715
|
-
<script type="text/javascript" src="javascripts/guides.js"></script>
|
716
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
717
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
718
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
719
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
720
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
721
|
-
<script type="text/javascript">
|
722
|
-
SyntaxHighlighter.all()
|
723
|
-
</script>
|
724
|
-
</body>
|
725
|
-
</html>
|