-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails_application_templates.html
442 lines (403 loc) · 22.7 KB
/
rails_application_templates.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rails Application Templates — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" data-turbolinks-track="reload">
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="javascripts/syntaxhighlighter.js" data-turbolinks-track="reload"></script>
<script src="javascripts/turbolinks.js" data-turbolinks-track="reload"></script>
<script src="javascripts/guides.js" data-turbolinks-track="reload"></script>
<script src="javascripts/responsive-tables.js" data-turbolinks-track="reload"></script>
</head>
<body class="guide">
<div>
<img src="images/edge_badge.png" alt="edge-badge" id="edge-badge" />
</div>
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">Veja mais em <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
<span class="red-button more-info-button">
Mais Ruby on Rails
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="https://weblog.rubyonrails.org/">Blog</a></li>
<li class="more-info"><a href="https://guides.rubyonrails.org/">Guia</a></li>
<li class="more-info"><a href="http://api.rubyonrails.org/">API</a></li>
<li class="more-info"><a href="https://stackoverflow.com/questions/tagged/ruby-on-rails">Pedir ajuda</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">Contribuir no GitHub</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guias</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<div class="guides-section-container">
<div class="guides-section">
<dt>Comece Aqui</dt>
</div>
<div class="guides-section">
<dt>Models</dt>
</div>
<div class="guides-section">
<dt>Views</dt>
</div>
<div class="guides-section">
<dt>Controllers</dt>
</div>
<div class="guides-section">
<dt>Other Components</dt>
</div>
<div class="guides-section">
<dt>Digging Deeper</dt>
</div>
<div class="guides-section">
<dt>Extending Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">Creating and Customizing Rails Generators & Templates</a></dd>
</div>
<div class="guides-section">
<dt>Contributions</dt>
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Guides Guidelines</a></dd>
</div>
<div class="guides-section">
<dt>Policies</dt>
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
</div>
<div class="guides-section">
<dt>Release Notes</dt>
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
<dd><a href="5_2_release_notes.html">Version 5.2 - April 2018</a></dd>
<dd><a href="5_1_release_notes.html">Version 5.1 - April 2017</a></dd>
<dd><a href="5_0_release_notes.html">Version 5.0 - June 2016</a></dd>
<dd><a href="4_2_release_notes.html">Version 4.2 - December 2014</a></dd>
<dd><a href="4_1_release_notes.html">Version 4.1 - April 2014</a></dd>
<dd><a href="4_0_release_notes.html">Version 4.0 - June 2013</a></dd>
<dd><a href="3_2_release_notes.html">Version 3.2 - January 2012</a></dd>
<dd><a href="3_1_release_notes.html">Version 3.1 - August 2011</a></dd>
<dd><a href="3_0_release_notes.html">Version 3.0 - August 2010</a></dd>
<dd><a href="2_3_release_notes.html">Version 2.3 - March 2009</a></dd>
<dd><a href="2_2_release_notes.html">Version 2.2 - November 2008</a></dd>
</div>
</div>
</div>
</li>
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribua</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Guias</option>
<optgroup label="Comece Aqui">
</optgroup>
<optgroup label="Models">
</optgroup>
<optgroup label="Views">
</optgroup>
<optgroup label="Controllers">
</optgroup>
<optgroup label="Other Components">
</optgroup>
<optgroup label="Digging Deeper">
</optgroup>
<optgroup label="Extending Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">Creating and Customizing Rails Generators & Templates</option>
</optgroup>
<optgroup label="Contributions">
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
<option value="ruby_on_rails_guides_guidelines.html">Guides Guidelines</option>
</optgroup>
<optgroup label="Policies">
<option value="maintenance_policy.html">Maintenance Policy</option>
</optgroup>
<optgroup label="Release Notes">
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
<option value="5_2_release_notes.html">Version 5.2 - April 2018</option>
<option value="5_1_release_notes.html">Version 5.1 - April 2017</option>
<option value="5_0_release_notes.html">Version 5.0 - June 2016</option>
<option value="4_2_release_notes.html">Version 4.2 - December 2014</option>
<option value="4_1_release_notes.html">Version 4.1 - April 2014</option>
<option value="4_0_release_notes.html">Version 4.0 - June 2013</option>
<option value="3_2_release_notes.html">Version 3.2 - January 2012</option>
<option value="3_1_release_notes.html">Version 3.1 - August 2011</option>
<option value="3_0_release_notes.html">Version 3.0 - August 2010</option>
<option value="2_3_release_notes.html">Version 2.3 - March 2009</option>
<option value="2_2_release_notes.html">Version 2.2 - November 2008</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Rails Application Templates</h2><p>Application templates are simple Ruby files containing DSL for adding gems/initializers etc. to your freshly created Rails project or an existing Rails project.</p><p>After reading this guide, you will know:</p>
<ul>
<li>How to use templates to generate/customize Rails applications.</li>
<li>How to write your own reusable application templates using the Rails template API.</li>
</ul>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li><a href="#usage">Usage</a></li>
<li>
<a href="#template-api">Template API</a>
<ul>
<li><a href="#gem-args">gem(*args)</a></li>
<li><a href="#gem-group-names-block">gem_group(*names, &block)</a></li>
<li><a href="#add-source-source-options-block">add_source(source, options={}, &block)</a></li>
<li><a href="#environment-application-data-nil-options-block">environment/application(data=nil, options={}, &block)</a></li>
<li><a href="#vendor-lib-file-initializer-filename-data-nil-block">vendor/lib/file/initializer(filename, data = nil, &block)</a></li>
<li><a href="#rakefile-filename-data-nil-block">rakefile(filename, data = nil, &block)</a></li>
<li><a href="#generate-what-args">generate(what, *args)</a></li>
<li><a href="#run-command">run(command)</a></li>
<li><a href="#rails-command-command-options">rails_command(command, options = {})</a></li>
<li><a href="#route-routing-code">route(routing_code)</a></li>
<li><a href="#inside-dir">inside(dir)</a></li>
<li><a href="#ask-question">ask(question)</a></li>
<li><a href="#yes-questionmark-question-or-no-questionmark-question">yes?(question) or no?(question)</a></li>
<li><a href="#git-command">git(:command)</a></li>
<li><a href="#after-bundle-block">after_bundle(&block)</a></li>
</ul>
</li>
<li><a href="#advanced-usage">Advanced Usage</a></li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="usage"><a class="anchorlink" href="#usage">1 Usage</a></h3><p>To apply a template, you need to provide the Rails generator with the location of the template you wish to apply using the <code>-m</code> option. This can either be a path to a file or a URL.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails new blog -m ~/template.rb
$ rails new blog -m http://example.com/template.rb
</pre>
</div>
<p>You can use the <code>app:template</code> rails command to apply templates to an existing Rails application. The location of the template needs to be passed in via the LOCATION environment variable. Again, this can either be path to a file or a URL.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails app:template LOCATION=~/template.rb
$ rails app:template LOCATION=http://example.com/template.rb
</pre>
</div>
<h3 id="template-api"><a class="anchorlink" href="#template-api">2 Template API</a></h3><p>The Rails templates API is easy to understand. Here's an example of a typical Rails template:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# template.rb
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rails_command("db:migrate")
after_bundle do
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
</pre>
</div>
<p>The following sections outline the primary methods provided by the API:</p><h4 id="gem-args"><a class="anchorlink" href="#gem-args">2.1 gem(*args)</a></h4><p>Adds a <code>gem</code> entry for the supplied gem to the generated application's <code>Gemfile</code>.</p><p>For example, if your application depends on the gems <code>bj</code> and <code>nokogiri</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
gem "bj"
gem "nokogiri"
</pre>
</div>
<p>Please note that this will NOT install the gems for you and you will have to run <code>bundle install</code> to do that.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
bundle install
</pre>
</div>
<h4 id="gem-group-names-block"><a class="anchorlink" href="#gem-group-names-block">2.2 gem_group(*names, &block)</a></h4><p>Wraps gem entries inside a group.</p><p>For example, if you want to load <code>rspec-rails</code> only in the <code>development</code> and <code>test</code> groups:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
gem_group :development, :test do
gem "rspec-rails"
end
</pre>
</div>
<h4 id="add-source-source-options-block"><a class="anchorlink" href="#add-source-source-options-block">2.3 add_source(source, options={}, &block)</a></h4><p>Adds the given source to the generated application's <code>Gemfile</code>.</p><p>For example, if you need to source a gem from <code>"http://code.whytheluckystiff.net"</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
add_source "http://code.whytheluckystiff.net"
</pre>
</div>
<p>If block is given, gem entries in block are wrapped into the source group.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
add_source "http://gems.github.com/" do
gem "rspec-rails"
end
</pre>
</div>
<h4 id="environment-application-data-nil-options-block"><a class="anchorlink" href="#environment-application-data-nil-options-block">2.4 environment/application(data=nil, options={}, &block)</a></h4><p>Adds a line inside the <code>Application</code> class for <code>config/application.rb</code>.</p><p>If <code>options[:env]</code> is specified, the line is appended to the corresponding file in <code>config/environments</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
environment 'config.action_mailer.default_url_options = {host: "http://yourwebsite.example.com"}', env: 'production'
</pre>
</div>
<p>A block can be used in place of the <code>data</code> argument.</p><h4 id="vendor-lib-file-initializer-filename-data-nil-block"><a class="anchorlink" href="#vendor-lib-file-initializer-filename-data-nil-block">2.5 vendor/lib/file/initializer(filename, data = nil, &block)</a></h4><p>Adds an initializer to the generated application's <code>config/initializers</code> directory.</p><p>Let's say you like using <code>Object#not_nil?</code> and <code>Object#not_blank?</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
initializer 'bloatlol.rb', <<-CODE
class Object
def not_nil?
!nil?
end
def not_blank?
!blank?
end
end
CODE
</pre>
</div>
<p>Similarly, <code>lib()</code> creates a file in the <code>lib/</code> directory and <code>vendor()</code> creates a file in the <code>vendor/</code> directory.</p><p>There is even <code>file()</code>, which accepts a relative path from <code>Rails.root</code> and creates all the directories/files needed:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
file 'app/components/foo.rb', <<-CODE
class Foo
end
CODE
</pre>
</div>
<p>That'll create the <code>app/components</code> directory and put <code>foo.rb</code> in there.</p><h4 id="rakefile-filename-data-nil-block"><a class="anchorlink" href="#rakefile-filename-data-nil-block">2.6 rakefile(filename, data = nil, &block)</a></h4><p>Creates a new rake file under <code>lib/tasks</code> with the supplied tasks:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
rakefile("bootstrap.rake") do
<<-TASK
namespace :boot do
task :strap do
puts "i like boots!"
end
end
TASK
end
</pre>
</div>
<p>The above creates <code>lib/tasks/bootstrap.rake</code> with a <code>boot:strap</code> rake task.</p><h4 id="generate-what-args"><a class="anchorlink" href="#generate-what-args">2.7 generate(what, *args)</a></h4><p>Runs the supplied rails generator with given arguments.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
generate(:scaffold, "person", "name:string", "address:text", "age:number")
</pre>
</div>
<h4 id="run-command"><a class="anchorlink" href="#run-command">2.8 run(command)</a></h4><p>Executes an arbitrary command. Just like the backticks. Let's say you want to remove the <code>README.rdoc</code> file:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
run "rm README.rdoc"
</pre>
</div>
<h4 id="rails-command-command-options"><a class="anchorlink" href="#rails-command-command-options">2.9 rails_command(command, options = {})</a></h4><p>Runs the supplied command in the Rails application. Let's say you want to migrate the database:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
rails_command "db:migrate"
</pre>
</div>
<p>You can also run commands with a different Rails environment:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
rails_command "db:migrate", env: 'production'
</pre>
</div>
<p>You can also run commands as a super-user:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
rails_command "log:clear", sudo: true
</pre>
</div>
<p>You can also run commands that should abort application generation if they fail:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
rails_command "db:migrate", abort_on_failure: true
</pre>
</div>
<h4 id="route-routing-code"><a class="anchorlink" href="#route-routing-code">2.10 route(routing_code)</a></h4><p>Adds a routing entry to the <code>config/routes.rb</code> file. In the steps above, we generated a person scaffold and also removed <code>README.rdoc</code>. Now, to make <code>PeopleController#index</code> the default page for the application:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
route "root to: 'person#index'"
</pre>
</div>
<h4 id="inside-dir"><a class="anchorlink" href="#inside-dir">2.11 inside(dir)</a></h4><p>Enables you to run a command from the given directory. For example, if you have a copy of edge rails that you wish to symlink from your new apps, you can do this:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
inside('vendor') do
run "ln -s ~/commit-rails/rails rails"
end
</pre>
</div>
<h4 id="ask-question"><a class="anchorlink" href="#ask-question">2.12 ask(question)</a></h4><p><code>ask()</code> gives you a chance to get some feedback from the user and use it in your templates. Let's say you want your user to name the new shiny library you're adding:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
lib_name = ask("What do you want to call the shiny library ?")
lib_name << ".rb" unless lib_name.index(".rb")
lib lib_name, <<-CODE
class Shiny
end
CODE
</pre>
</div>
<h4 id="yes-questionmark-question-or-no-questionmark-question"><a class="anchorlink" href="#yes-questionmark-question-or-no-questionmark-question">2.13 yes?(question) or no?(question)</a></h4><p>These methods let you ask questions from templates and decide the flow based on the user's answer. Let's say you want to Freeze Rails only if the user wants to:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
rails_command("rails:freeze:gems") if yes?("Freeze rails gems?")
# no?(question) acts just the opposite.
</pre>
</div>
<h4 id="git-command"><a class="anchorlink" href="#git-command">2.14 git(:command)</a></h4><p>Rails templates let you run any git command:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
git :init
git add: "."
git commit: "-a -m 'Initial commit'"
</pre>
</div>
<h4 id="after-bundle-block"><a class="anchorlink" href="#after-bundle-block">2.15 after_bundle(&block)</a></h4><p>Registers a callback to be executed after the gems are bundled and binstubs
are generated. Useful for all generated files to version control:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
after_bundle do
git :init
git add: '.'
git commit: "-a -m 'Initial commit'"
end
</pre>
</div>
<p>The callbacks gets executed even if <code>--skip-bundle</code> and/or <code>--skip-spring</code> has
been passed.</p><h3 id="advanced-usage"><a class="anchorlink" href="#advanced-usage">3 Advanced Usage</a></h3><p>The application template is evaluated in the context of a
<code>Rails::Generators::AppGenerator</code> instance. It uses the <code>apply</code> action
provided by
<a href="https://github.com/erikhuda/thor/blob/master/lib/thor/actions.rb#L207">Thor</a>.
This means you can extend and change the instance to match your needs.</p><p>For example by overwriting the <code>source_paths</code> method to contain the
location of your template. Now methods like <code>copy_file</code> will accept
relative paths to your template's location.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def source_paths
[__dir__]
end
</pre>
</div>
<h3>Feedback</h3>
<p>
You're encouraged to help improve the quality of this guide.
</p>
<p>
Please contribute if you see any typos or factual errors.
To get started, you can read our <a href="https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
</p>
<p>
You may also find incomplete content or stuff that is not up to date.
Please do add any missing documentation for master. Make sure to check
<a href="https://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
if the issues are already fixed or not on the master branch.
Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
for style and conventions.
</p>
<p>
If for whatever reason you spot something to fix but cannot patch it yourself, please
<a href="https://github.com/rails/rails/issues">open an issue</a>.
</p>
<p>And last but not least, any kind of discussion regarding Ruby on Rails
documentation is very welcome on the <a href="https://groups.google.com/forum/#!forum/rubyonrails-docs">rubyonrails-docs mailing list</a>.
</p>
</div>
</div>
</div>
<hr class="hide" />
<div id="footer">
<div class="wrapper">
<p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
</div>
</div>
</body>
</html>