railties 3.0.3 → 3.0.4.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/guides/assets/stylesheets/main.css +1 -1
  2. data/guides/rails_guides/helpers.rb +2 -7
  3. data/guides/source/3_0_release_notes.textile +2 -2
  4. data/guides/source/action_controller_overview.textile +0 -4
  5. data/guides/source/action_mailer_basics.textile +9 -11
  6. data/guides/source/action_view_overview.textile +0 -2
  7. data/guides/source/active_record_querying.textile +9 -57
  8. data/guides/source/active_record_validations_callbacks.textile +4 -6
  9. data/guides/source/active_support_core_extensions.textile +49 -2
  10. data/guides/source/api_documentation_guidelines.textile +1 -1
  11. data/guides/source/association_basics.textile +0 -2
  12. data/guides/source/caching_with_rails.textile +0 -1
  13. data/guides/source/command_line.textile +2 -3
  14. data/guides/source/configuring.textile +332 -64
  15. data/guides/source/contribute.textile +1 -1
  16. data/guides/source/contributing_to_ruby_on_rails.textile +370 -0
  17. data/guides/source/debugging_rails_applications.textile +0 -2
  18. data/guides/source/form_helpers.textile +0 -2
  19. data/guides/source/generators.textile +262 -9
  20. data/guides/source/getting_started.textile +0 -2
  21. data/guides/source/i18n.textile +13 -18
  22. data/guides/source/index.html.erb +9 -9
  23. data/guides/source/initialization.textile +4 -4
  24. data/guides/source/layout.html.erb +27 -2
  25. data/guides/source/layouts_and_rendering.textile +10 -16
  26. data/guides/source/migrations.textile +0 -2
  27. data/guides/source/performance_testing.textile +1 -3
  28. data/guides/source/plugins.textile +0 -2
  29. data/guides/source/rails_application_templates.textile +0 -2
  30. data/guides/source/rails_on_rack.textile +0 -2
  31. data/guides/source/routing.textile +13 -15
  32. data/guides/source/security.textile +3 -5
  33. data/guides/source/testing.textile +16 -12
  34. data/lib/rails/generators.rb +1 -1
  35. data/lib/rails/generators/actions.rb +1 -1
  36. data/lib/rails/generators/base.rb +1 -1
  37. data/lib/rails/generators/rails/app/app_generator.rb +2 -8
  38. data/lib/rails/generators/rails/app/templates/Gemfile +1 -1
  39. data/lib/rails/generators/rails/app/templates/config/boot.rb +3 -10
  40. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml +1 -1
  41. data/lib/rails/generators/rails/app/templates/gitignore +1 -1
  42. data/lib/rails/rack/logger.rb +1 -1
  43. data/lib/rails/railtie.rb +2 -2
  44. data/lib/rails/test_help.rb +0 -11
  45. data/lib/rails/version.rb +4 -3
  46. metadata +22 -17
  47. data/guides/source/contributing_to_rails.textile +0 -308
@@ -1,6 +1,6 @@
1
1
  h2. Contribute to the Rails Guides
2
2
 
3
- Rails Guides aim to improve the Rails documentation and to make the barrier to entry as low as possible. A reasonably experienced developer should be able to use the Guides to come up to speed on Rails quickly. You can track the overall effort at the "Rails Guides Lighthouse":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets. Our sponsors have contributed prizes for those who write an entire guide, but there are many other ways to contribute.
3
+ Rails Guides aim to improve the Rails documentation and to make the barrier to entry as low as possible. A reasonably experienced developer should be able to use the guides to come up to speed on Rails quickly. Our sponsors have contributed prizes for those who write an entire guide, but there are many other ways to contribute.
4
4
 
5
5
  endprologue.
6
6
 
@@ -0,0 +1,370 @@
1
+ h2. Contributing to Ruby on Rails
2
+
3
+ This guide covers ways in which _you_ can become a part of the ongoing development of Ruby on Rails. After reading it, you should be familiar with:
4
+
5
+ * Using Lighthouse to report issues
6
+ * Cloning master and running the test suite
7
+ * Helping to resolve existing issues
8
+ * Contributing to the Ruby on Rails documentation
9
+ * Contributing to the Ruby on Rails code
10
+
11
+ Ruby on Rails is not "someone else's framework." Over the years, hundreds of people have contributed to Ruby on Rails ranging from a single character to massive architectural changes or significant documentation. All with the goal of making Ruby on Rails better for everyone. Even if you don't feel up to writing code or documentation yet, there are a variety of other ways that you can contribute, from reporting issues to testing patches.
12
+
13
+ endprologue.
14
+
15
+ h3. Reporting an Issue
16
+
17
+ Ruby on Rails uses a "Lighthouse project":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/ to track issues (primarily bugs and contributions of new code). If you've found a bug in Ruby on Rails, this is the place to start. You'll need to create a (free) Lighthouse account in order to comment on issues or to upload patches.
18
+
19
+ NOTE: Bugs in the most recent released version of Ruby on Rails are likely to get the most attention. Also, the Rails core team is always interested in feedback from those who can take the time to test _edge Rails_ (the code for the version of Rails that is currently under development). Later in this guide you'll find out how to get edge Rails for testing.
20
+
21
+ h4. Creating a Bug Report
22
+
23
+ If you've found a problem in Ruby on Rails which is not a security risk do a search in Lighthouse in case it was already reported. If you find no ticket addressing it you can "add a new one":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/new. (See the next section for reporting security issues.)
24
+
25
+ At the minimum, your ticket needs a title and descriptive text. But that's only a minimum. You should include as much relevant information as possible. You need to at least post the code sample that has the issue. Even better is to include a unit test that shows how the expected behavior is not occurring. Your goal should be to make it easy for yourself - and others - to replicate the bug and figure out a fix.
26
+
27
+ You shouldn't assign the bug to a particular core developer unless you know for sure which developer will be handling that issue. The core team periodically reviews issues and assigns developers and milestones to them.
28
+
29
+ You should set tags for your issue. Use the "bug" tag for a bug report, and add the "patch" tag if you are attaching a patch. Try to find some relevant tags from the existing tag list (which will appear as soon as you start typing in the "Choose some tags" textbox), rather than creating new tags.
30
+
31
+ Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kind of bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to confirm it with a "I'm having this problem too" comment.
32
+
33
+ h4. Special Treatment for Security Issues
34
+
35
+ WARNING: Please do not report security vulnerabilities on public Lighthouse tickets. The "Rails security policy page":http://rubyonrails.org/security details the procedure to follow for security issues.
36
+
37
+ h4. What About Feature Requests?
38
+
39
+ Please don't put "feature request" tickets into Lighthouse. If there's a new feature that you want to see added to Ruby on Rails, you'll need to write the code yourself - or convince someone else to partner with you to write the code. Later in this guide you'll find detailed instructions for proposing a patch to Ruby on Rails. If you enter a wishlist item in Lighthouse with no code, you can expect it to be marked "invalid" as soon as it's reviewed.
40
+
41
+ h3. Running the Test Suite
42
+
43
+ To move on from submitting bugs to helping resolve existing issues or contributing your own code to Ruby on Rails, you _must_ be able to run its test suite. In this section of the guide you'll learn how to set up the tests on your own computer.
44
+
45
+ h4. Install git
46
+
47
+ Ruby on Rails uses git for source code control. The "git homepage":http://git-scm.com/ has installation instructions. There are a variety of resources on the net that will help you get familiar with git:
48
+
49
+ * "Everyday Git":http://www.kernel.org/pub/software/scm/git/docs/everyday.html will teach you just enough about git to get by.
50
+ * The "PeepCode screencast":https://peepcode.com/products/git on git ($9) is easier to follow.
51
+ * "GitHub":http://github.com/guides/home offers links to a variety of git resources.
52
+ * "Pro Git":http://progit.org/book/ is an entire book about git with a Creative Commons license.
53
+
54
+ h4. Clone the Ruby on Rails Repository
55
+
56
+ Navigate to the folder where you want the Ruby on Rails source code (it will create its own +rails+ subdirectory) and run:
57
+
58
+ <shell>
59
+ git clone git://github.com/rails/rails.git
60
+ cd rails
61
+ </shell>
62
+
63
+ h4. Set up and Run the Tests
64
+
65
+ The test suite must pass with any submitted code. No matter whether you are writing a new patch, or evaluating someone else's, you need to be able to run the tests.
66
+
67
+ Install first libxml2 and libxslt together with their development files for Nokogiri. In Ubuntu that's
68
+
69
+ <shell>
70
+ sudo apt-get install libxml2 libxml2-dev libxslt1-dev
71
+ </shell>
72
+
73
+ Also, SQLite3 and its development files for the +sqlite3-ruby+ gem, in Ubuntu you're done with
74
+
75
+ <shell>
76
+ sudo apt-get install sqlite3 libsqlite3-dev
77
+ </shell>
78
+
79
+ Get a recent version of "Bundler":http://gembundler.com/:
80
+
81
+ <shell>
82
+ gem install bundler
83
+ </shell>
84
+
85
+ and run:
86
+
87
+ <shell>
88
+ bundle install --without db
89
+ </shell>
90
+
91
+ This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back at these soon. With dependencies installed, you can run the test suite with:
92
+
93
+ <shell>
94
+ rake test
95
+ </shell>
96
+
97
+ You can also run tests for an specific framework, like Action Pack, by going into its directory and executing the same command:
98
+
99
+ <shell>
100
+ cd actionpack
101
+ rake test
102
+ </shell>
103
+
104
+ h4. Warnings
105
+
106
+ The test suite runs with warnings enabled. Ideally Ruby on Rails should issue no warning, but there may be a few, and also some from third-party libraries. Please ignore (or fix!) them if any, and submit patches that do not issue new warnings.
107
+
108
+ As of this writing they are specially noisy with Ruby 1.9. If you are sure about what you are doing and would like to have a more clear output, there's a way to override the flag:
109
+
110
+ <shell>
111
+ RUBYOPT=-W0 rake test
112
+ </shell>
113
+
114
+ h4. Testing Active Record
115
+
116
+ The test suite of Active Record attempts to run four times, once for SQLite3, once for each of the two MySQL gems (+mysql+ and +mysql2+), and once for PostgreSQL. We are going to see now how to setup the environment for them.
117
+
118
+ WARNING: If you're working with Active Record code, you _must_ ensure that the tests pass for at least MySQL, PostgreSQL, and SQLite3. Subtle differences between the various adapters have been behind the rejection of many patches that looked OK when tested only against MySQL.
119
+
120
+ h5. SQLite3
121
+
122
+ The gem +sqlite3-ruby+ does not belong to the "db" group indeed, if you followed the instructions above you're ready. This is how you run the Active Record test suite only for SQLite3:
123
+
124
+ <shell>
125
+ cd activerecord
126
+ rake test_sqlite3
127
+ </shell>
128
+
129
+ h5. MySQL and PostgreSQL
130
+
131
+ To be able to run the suite for MySQL and PostgreSQL we need their gems. Install first the servers, their client libraries, and their development files. In Ubuntu just run
132
+
133
+ <shell>
134
+ sudo apt-get install mysql-server libmysqlclient15-dev
135
+ sudo apt-get install postgresql postgresql-client postgresql-contrib libpq-dev
136
+ </shell>
137
+
138
+ After that run:
139
+
140
+ <shell>
141
+ rm .bundle/config
142
+ bundle install
143
+ </shell>
144
+
145
+ We need first to delete +.bundle/config+ because Bundler remembers in that file that we didn't want to install the "db" group (alternatively you can edit the file).
146
+
147
+ In order to be able to run the test suite against MySQL you need to create a user named +rails+ with privileges on the test databases:
148
+
149
+ <shell>
150
+ mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
151
+ to 'rails'@'localhost';
152
+ mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
153
+ to 'rails'@'localhost';
154
+ </shell>
155
+
156
+ and create the test databases:
157
+
158
+ <shell>
159
+ cd activerecord
160
+ rake mysql:build_databases
161
+ </shell>
162
+
163
+ PostgreSQL's authentication works differently. A simple way to setup the development environment for example is to run with your development account
164
+
165
+ <shell>
166
+ sudo -u postgres createuser --superuser $USER
167
+ </shell>
168
+
169
+ and after that create the test databases with
170
+
171
+ <shell>
172
+ cd activerecord
173
+ rake postgresql:build_databases
174
+ </shell>
175
+
176
+ NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation.
177
+
178
+ If you’re using another database, check the files under +activerecord/test/connections+ for default connection information. You can edit these files if you _must_ on your machine to provide different credentials, but obviously you should not push any such changes back to Rails.
179
+
180
+ You can now run tests as you did for +sqlite3+, the tasks are
181
+
182
+ <shell>
183
+ test_mysql
184
+ test_mysql2
185
+ test_postgresql
186
+ </shell>
187
+
188
+ respectively. As we mentioned before
189
+
190
+ <shell>
191
+ rake test
192
+ </shell>
193
+
194
+ will now run the four of them in turn.
195
+
196
+ You can also invoke +test_jdbcmysql+, +test_jdbcsqlite3+ or +test_jdbcpostgresql+. Check out the file +activerecord/RUNNING_UNIT_TESTS+ for information on running more targeted database tests, or the file +ci/ci_build.rb+ to see the test suite that the continuous integration server runs.
197
+
198
+ h4. Older versions of Ruby on Rails
199
+
200
+ If you want to add a fix to older versions of Ruby on Rails, you'll need to set up and switch to your own local tracking branch. Here is an example to switch to the 2-3-stable branch:
201
+
202
+ <shell>
203
+ git branch --track 2-3-stable origin/2-3-stable
204
+ git checkout 2-3-stable
205
+ </shell>
206
+
207
+ TIP: You may want to "put your git branch name in your shell prompt":http://qugstart.com/blog/git-and-svn/add-colored-git-branch-name-to-your-shell-prompt/ to make it easier to remember which version of the code you're working with.
208
+
209
+ h3. Helping to Resolve Existing Issues
210
+
211
+ As a next step beyond reporting issues, you can help the core team resolve existing issues. If you check the "open tickets":https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets?q=state%3Aopen list in Lighthouse, you'll find lots of issues already requiring attention. What can you do for these? Quite a bit, actually:
212
+
213
+ h4. Verifying Bug Reports
214
+
215
+ For starters, it helps to just verify bug reports. Can you reproduce the reported issue on your own computer? If so, you can add a comment to the ticket saying that you're seeing the same thing.
216
+
217
+ If something is very vague, can you help squish it down into something specific? Maybe you can provide additional information to help reproduce a bug, or eliminate needless steps that aren't required to help demonstrate the problem.
218
+
219
+ If you find a bug report without a test, it's very useful to contribute a failing test. This is also a great way to get started exploring the source code: looking at the existing test files will teach you how to write more tests. New tests are best contributed in the form of a patch, as explained later on in the "Contributing to the Rails Code" section.
220
+
221
+ Anything you can do to make bug reports more succinct or easier to reproduce is a help to folks trying to write code to fix those bugs - whether you end up writing the code yourself or not.
222
+
223
+ h4. Testing Patches
224
+
225
+ You can also help out by examining patches that have been submitted to Ruby on Rails via Lighthouse. To apply someone's changes you need to first create a dedicated branch:
226
+
227
+ <shell>
228
+ git checkout -b testing_branch
229
+ </shell>
230
+
231
+ Then you can apply their patch:
232
+
233
+ <shell>
234
+ git apply their-patch-file.diff
235
+ </shell>
236
+
237
+ After applying a patch, test it out! Here are some things to think about:
238
+
239
+ * Does the patch actually work?
240
+ * Are you happy with the tests? Can you follow what they're testing? Are there any tests missing?
241
+ * Does it have proper documentation coverage? Should documentation elsewhere be updated?
242
+ * Do you like the implementation? Can you think of a nicer or faster way to implement a part of their change?
243
+
244
+ Once you're happy that the patch contains a good change, comment on the Lighthouse ticket indicating your approval. Your comment should indicate that you like the change and what you like about it. Something like:
245
+
246
+ <blockquote>
247
+ I like the way you've restructured that code in generate_finder_sql, much nicer. The tests look good too.
248
+ </blockquote>
249
+
250
+ If your comment simply says "+1", then odds are that other reviewers aren't going to take it too seriously. Show that you took the time to review the patch. Once three people have approved it, add the "verified" tag. This will bring it to the attention of a core team member who will review the changes looking for the same kinds of things.
251
+
252
+ h3. Contributing to the Rails Documentation
253
+
254
+ Ruby on Rails has two main sets of documentation: The guides help you to learn Ruby on Rails, and the API is a reference.
255
+
256
+ You can create a ticket in Lighthouse to fix or expand documentation. However, if you're confident about your changes you can push them yourself directly via "docrails":http://github.com/lifo/docrails/tree/master. docrails is a branch with an *open commit policy* and public write access. Commits to docrails are still reviewed, but that happens after they are pushed. docrails is merged with master regularly, so you are effectively editing the Ruby on Rails documentation.
257
+
258
+ When working with documentation, please take into account the "API Documentation Guidelines":api_documentation_guidelines.html and the "Ruby on Rails Guides Guidelines":ruby_on_rails_guides_guidelines.html.
259
+
260
+ NOTE: As explained above, ordinary code patches should have proper documentation coverage. docrails is only used for isolated documentation improvements.
261
+
262
+ WARNING: docrails has a very strict policy: no code can be touched whatsoever, no matter how trivial or small the change. Only RDoc and guides can be edited via docrails.
263
+
264
+ If you have an idea for a new guide you can refer to the "contribution page":contribute.html for instructions on getting involved.
265
+
266
+ h3. Contributing to the Rails Code
267
+
268
+ h4. Clone the Rails Repository
269
+
270
+ The first thing you need to do to be able to contribute code is to clone the repository:
271
+
272
+ <shell>
273
+ git clone git://github.com/rails/rails.git
274
+ </shell>
275
+
276
+ and create a dedicated branch:
277
+
278
+ <shell>
279
+ cd rails
280
+ git checkout -b my_new_branch
281
+ </shell>
282
+
283
+ It doesn’t really matter what name you use, because this branch will only exist on your local computer.
284
+
285
+ h4. Write Your Code
286
+
287
+ Now get busy and add or edit code. You’re on your branch now, so you can write whatever you want (you can check to make sure you’re on the right branch with +git branch -a+). But if you’re planning to submit your change back for inclusion in Rails, keep a few things in mind:
288
+
289
+ * Get the code right
290
+ * Use Rails idioms and helpers
291
+ * Include tests that fail without your code, and pass with it
292
+ * Update the documentation, the surrounding one, examples elsewhere, guides, whatever is affected by your contribution
293
+
294
+ h4. Follow the Coding Conventions
295
+
296
+ Rails follows a simple set of coding style conventions.
297
+
298
+ * Two spaces, no tabs
299
+ * Prefer +&amp;&amp;+/+||+ over +and+/+or+
300
+ * +MyClass.my_method(my_arg)+ not +my_method( my_arg )+ or +my_method my_arg+
301
+ * Follow the conventions you see used in the source already
302
+
303
+ h4. Sanity Check
304
+
305
+ You should not be the only person who looks at the code before you submit it. You know at least one other Rails developer, right? Show them what you’re doing and ask for feedback. Doing this in private before you push a patch out publicly is the “smoke test” for a patch: if you can’t convince one other developer of the beauty of your code, you’re unlikely to convince the core team either.
306
+
307
+ You might also want to check out the "RailsBridge BugMash":http://wiki.railsbridge.org/projects/railsbridge/wiki/BugMash as a way to get involved in a group effort to improve Rails. This can help you get started and help check your code when you're writing your first patches.
308
+
309
+ h4. Commit Your Changes
310
+
311
+ When you're happy with the code on your computer, you need to commit the changes to git:
312
+
313
+ <shell>
314
+ git commit -a -m "Here is a commit message"
315
+ </shell>
316
+
317
+ h4. Update master
318
+
319
+ It’s pretty likely that other changes to master have happened while you were working. Go get them:
320
+
321
+ <shell>
322
+ git checkout master
323
+ git pull
324
+ </shell>
325
+
326
+ Now reapply your patch on top of the latest changes:
327
+
328
+ <shell>
329
+ git checkout my_new_branch
330
+ git rebase master
331
+ </shell>
332
+
333
+ No conflicts? Tests still pass? Change still seems reasonable to you? Then move on.
334
+
335
+ h4. Create a Patch
336
+
337
+ Now you can create a patch file to share with other developers (and with the core team). Still in your branch, run
338
+
339
+ <shell>
340
+ git commit -a
341
+ git format-patch master --stdout > my_new_patch.diff
342
+ </shell>
343
+
344
+ Sanity check the results of this operation: open the diff file in your text editor of choice and make sure that no unintended changes crept in.
345
+
346
+ h4. Create a Lighthouse Ticket
347
+
348
+ Now create a ticket with your patch. Go to the "new ticket":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/new page at Lighthouse. Fill in a reasonable title and description, remember to attach your patch file, and tag the ticket with the ‘patch’ tag and whatever other subject area tags make sense.
349
+
350
+ h4. Get Some Feedback
351
+
352
+ Now you need to get other people to look at your patch, just as you've looked at other people's patches. You can use the rubyonrails-core mailing list or the #rails-contrib channel on IRC freenode for this. You might also try just talking to Rails developers that you know.
353
+
354
+ h4. Iterate as Necessary
355
+
356
+ It’s entirely possible that the feedback you get will suggest changes. Don’t get discouraged: the whole point of contributing to an active open source project is to tap into community knowledge. If people are encouraging you to tweak your code, then it’s worth making the tweaks and resubmitting. If the feedback is that your code doesn’t belong in the core, you might still think about releasing it as a plugin.
357
+
358
+ And then...think about your next contribution!
359
+
360
+ h3. Rails Contributors
361
+
362
+ All contributions, either via master or docrails, get credit in "Rails Contributors":http://contributors.rubyonrails.org.
363
+
364
+ h3. Changelog
365
+
366
+ * December 28, 2010: Complete revision by "Xavier Noria":credits.html#fxn
367
+ * April 6, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
368
+ * August 1, 2009: Updates/amplifications by "Mike Gunderloy":credits.html#mgunderloy
369
+ * March 2, 2009: Initial draft by "Mike Gunderloy":credits.html#mgunderloy
370
+
@@ -706,8 +706,6 @@ h3. References
706
706
 
707
707
  h3. Changelog
708
708
 
709
- "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5
710
-
711
709
  * April 4, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
712
710
  * November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by "Emilio Tagua":credits.html#miloops
713
711
  * October 19, 2008: Copy editing pass by "Mike Gunderloy":credits.html#mgunderloy
@@ -767,8 +767,6 @@ Many apps grow beyond simple forms editing a single object. For example when cre
767
767
 
768
768
  h3. Changelog
769
769
 
770
- "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1
771
-
772
770
  * April 6, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
773
771
 
774
772
  h3. Authors
@@ -1,4 +1,4 @@
1
- h2. Creating and Customizing Rails Generators
1
+ h2. Creating and Customizing Rails Generators & Templates
2
2
 
3
3
  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.
4
4
 
@@ -10,6 +10,7 @@ In this guide you will:
10
10
  * Customize your scaffold by creating new generators
11
11
  * Customize your scaffold by changing generator templates
12
12
  * Learn how to use fallbacks to avoid overwriting a huge set of generators
13
+ * Learn how to create an application template
13
14
 
14
15
  endprologue.
15
16
 
@@ -45,6 +46,8 @@ class InitializerGenerator < Rails::Generators::Base
45
46
  end
46
47
  </ruby>
47
48
 
49
+ NOTE: +create_file+ is a method provided by +Thor::Actions+ and the documentation for it and its brethren can be found at "rdoc.info":http://rdoc.info/github/wycats/thor/master/Thor/Actions.
50
+
48
51
  Our new generator is quite simple: it inherits from +Rails::Generators::Base+ and has one method definition. Each public method in the generator is executed when a generator is invoked. Finally, we invoke the +create_file+ method that will create a file at the given destination with the given content. If you are familiar with the Rails Application Templates API, you'll feel right at home with the new generators API.
49
52
 
50
53
  To invoke our new generator, we just need to do:
@@ -92,7 +95,7 @@ class InitializerGenerator < Rails::Generators::NamedBase
92
95
  end
93
96
  </ruby>
94
97
 
95
- First, notice that we are inheriting from +Rails::Generators::NamedBase+ instead of +Rails::Generators::Base+. This means that our generator expects at least one argument, which will be the name of the initializer.
98
+ First, notice that we are inheriting from +Rails::Generators::NamedBase+ instead of +Rails::Generators::Base+. 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 +name+.
96
99
 
97
100
  We can see that by invoking the description of this new generator (don't forget to delete the old generator file):
98
101
 
@@ -131,6 +134,8 @@ $ rails generate initializer core_extensions
131
134
 
132
135
  We can see that now a initializer named core_extensions was created at +config/initializers/core_extensions.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+.
133
136
 
137
+ The methods that are available for generators are covered in the "final section":#generator-methods of this guide.
138
+
134
139
  h3. Generators Lookup
135
140
 
136
141
  When you run +rails generate initializer core_extensions+ Rails requires these files in turn until one is found:
@@ -201,18 +206,18 @@ config.generators do |g|
201
206
  end
202
207
  </ruby>
203
208
 
204
- If we generate another resource with the scaffold generator, we can notice 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.
209
+ 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.
205
210
 
206
- To demonstrate this, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator:
211
+ 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:
207
212
 
208
213
  <shell>
209
- $ rails generate generator my_helper
214
+ $ rails generate generator rails/my_helper
210
215
  </shell>
211
216
 
212
217
  After that, we can delete both the +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:
213
218
 
214
219
  <ruby>
215
- class MyHelperGenerator < Rails::Generators::NamedBase
220
+ class Rails::MyHelperGenerator < Rails::Generators::NamedBase
216
221
  def create_helper_file
217
222
  create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
218
223
  module #{class_name}Helper
@@ -265,7 +270,7 @@ Since Rails 3.0, this is easy to do due to the hooks concept. Our new helper doe
265
270
  To do that, we can change the generator this way:
266
271
 
267
272
  <ruby>
268
- class MyHelperGenerator < Rails::Generators::NamedBase
273
+ class Rails::MyHelperGenerator < Rails::Generators::NamedBase
269
274
  def create_helper_file
270
275
  create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
271
276
  module #{class_name}Helper
@@ -278,7 +283,7 @@ end
278
283
  end
279
284
  </ruby>
280
285
 
281
- Now, when the helper generator is invoked and TestUnit is configured as the test framework, it will try to invoke both +MyHelper::Generators::TestUnitGenerator+ and +TestUnit::Generators::MyHelperGenerator+. Since none of those are defined, we can tell our generator to invoke +TestUnit::Generators::HelperGenerator+ instead, which is defined since it's a Rails generator. To do that, we just need to add:
286
+ Now, when the helper generator is invoked and TestUnit is configured as the test framework, it will try to invoke both +Rails::TestUnitGenerator+ and +TestUnit::MyHelperGenerator+. Since none of those are defined, we can tell our generator to invoke +TestUnit::Generators::HelperGenerator+ instead, which is defined since it's a Rails generator. To do that, we just need to add:
282
287
 
283
288
  <ruby>
284
289
  # Search for :helper instead of :my_helper
@@ -361,9 +366,257 @@ $ rails generate scaffold Comment body:text
361
366
 
362
367
  Fallbacks allow your generators to have a single responsibility, increasing code reuse and reducing the amount of duplication.
363
368
 
369
+ h3. Application templates
370
+
371
+ 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".
372
+
373
+ <ruby>
374
+ gem("rspec-rails", :group => "test")
375
+ gem("cucumber-rails", :group => "test")
376
+
377
+ if yes?("Would you like to install Devise?")
378
+ gem("devise")
379
+ generate("devise:install")
380
+ model_name = ask("What would you like the user model to be called? [user]")
381
+ model_name = "user" if model_name.blank?
382
+ generate("devise", model_name)
383
+ end
384
+ </ruby>
385
+
386
+ 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.
387
+
388
+ Imagine that this template was in a file called +template.rb+. We can use it to modify the outcome of the +rails new+ command by using the +-m+ option and passing in the filename:
389
+
390
+ <shell>
391
+ rails new thud -m template.rb
392
+ </shell>
393
+
394
+ This command will generate the +Thud+ application, and then apply the template to the generated output.
395
+
396
+ Templates don't have to be stored on the local system, the +-m+ option also supports online templates:
397
+
398
+ <shell>
399
+ rails new thud -m https://gist.github.com/722911
400
+ </shell>
401
+
402
+ 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.
403
+
404
+ h3. Generator methods
405
+
406
+ The following are methods available for both generators and templates for Rails.
407
+
408
+ NOTE: Methods provided by Thor are not covered this guide and can be found in "Thor's documentation":http://rdoc.info/github/wycats/thor/master/Thor/Actions.html
409
+
410
+ h4. +plugin+
411
+
412
+ +plugin+ will install a plugin into the current application.
413
+
414
+ <ruby>
415
+ plugin("dynamic-form", :git => "git://github.com/rails/dynamic-form.git")
416
+ </ruby>
417
+
418
+ Available options are:
419
+
420
+ * +:git+ - Takes the path to the git repository where this plugin can be found.
421
+ * +:branch+ - The name of the branch of the git repository where the plugin is found.
422
+ * +:submodule+ - Set to +true+ for the plugin to be installed as a submodule. Defaults to +false+.
423
+ * +:svn+ - Takes the path to the svn repository where this plugin can be found.
424
+ * +:revision+ - The revision of the plugin in an SVN repository.
425
+
426
+ h4. +gem+
427
+
428
+ Specifies a gem dependency of the application.
429
+
430
+ <ruby>
431
+ gem("rspec", :group => "test", :version => "2.1.0")
432
+ gem("devise", "1.1.5")
433
+ </ruby>
434
+
435
+ Available options are:
436
+
437
+ * +:group+ - The group in the +Gemfile+ where this gem should go.
438
+ * +:version+ - The version string of the gem you want to use. Can also be specified as the second argument to the method.
439
+ * +:git+ - The URL to the git repository for this gem.
440
+
441
+ Any additional options passed to this method are put on the end of the line:
442
+
443
+ <ruby>
444
+ gem("devise", :git => "git://github.com/plataformatec/devise", :branch => "master")
445
+ </ruby>
446
+
447
+ The above code will put the following line into +Gemfile+:
448
+
449
+ <ruby>
450
+ gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"
451
+ </ruby>
452
+
453
+
454
+ h4. +add_source+
455
+
456
+ Adds a specified source to +Gemfile+:
457
+
458
+ <ruby>
459
+ add_source "http://gems.github.com"
460
+ </ruby>
461
+
462
+ h4. +application+
463
+
464
+ Adds a line to +config/application.rb+ directly after the application class definition.
465
+
466
+ <ruby>
467
+ application "config.asset_host = 'http://example.com'"
468
+ </ruby>
469
+
470
+ This method can also take a block:
471
+
472
+ <ruby>
473
+ application do
474
+ "config.asset_host = 'http://example.com'"
475
+ end
476
+ end
477
+ </ruby>
478
+
479
+ Available options are:
480
+
481
+ * +: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:
482
+
483
+ <ruby>
484
+ application(nil, :env => "development") do
485
+ "config.asset_host = 'http://localhost:3000'"
486
+ end
487
+ </ruby>
488
+
489
+ h4. +git+
490
+
491
+ Runs the specified git command:
492
+
493
+ <ruby>
494
+ git :init
495
+ git :add => "."
496
+ git :commit => "-m First commit!"
497
+ git :add => "onefile.rb", :rm => "badfile.cxx"
498
+ </ruby>
499
+
500
+ 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.
501
+
502
+ h4. +vendor+
503
+
504
+ Places a file into +vendor+ which contains the specified code.
505
+
506
+ <ruby>
507
+ vendor("sekrit.rb", '#top secret stuff')
508
+ </ruby>
509
+
510
+ This method also takes a block:
511
+
512
+ <ruby>
513
+ vendor("seeds.rb") do
514
+ "puts 'in ur app, seeding ur database'"
515
+ end
516
+ </ruby>
517
+
518
+ h4. +lib+
519
+
520
+ Places a file into +lib+ which contains the specified code.
521
+
522
+ <ruby>
523
+ lib("special.rb", 'p Rails.root')
524
+ </ruby>
525
+
526
+ This method also takes a block:
527
+
528
+ <ruby>
529
+ lib("super_special.rb") do
530
+ puts "Super special!"
531
+ end
532
+ </ruby>
533
+
534
+ h4. +rakefile+
535
+
536
+ Creates a Rake file in the +lib/tasks+ directory of the application.
537
+
538
+ <ruby>
539
+ rakefile("test.rake", 'hello there')
540
+ </ruby>
541
+
542
+ This method also takes a block:
543
+
544
+ <ruby>
545
+ rakefile("test.rake") do
546
+ %Q{
547
+ task :rock => :environment do
548
+ puts "Rockin'"
549
+ end
550
+ }
551
+ end
552
+ </ruby>
553
+
554
+ h4. +initializer+
555
+
556
+ Creates an initializer in the +config/initializers+ directory of the application:
557
+
558
+ <ruby>
559
+ initializer("begin.rb", "puts 'this is the beginning'")
560
+ </ruby>
561
+
562
+ This method also takes a block:
563
+
564
+ <ruby>
565
+ initializer("begin.rb") do
566
+ puts "Almost done!"
567
+ end
568
+ </ruby>
569
+
570
+ h4. +generate+
571
+
572
+ Runs the specified generator where the first argument is the generator name and the remaining arguments are passed directly to the generator.
573
+
574
+ <ruby>
575
+ generate("scaffold", "forums title:string description:text")
576
+ </ruby>
577
+
578
+
579
+ h4. +rake+
580
+
581
+ Runs the specified Rake task.
582
+
583
+ <ruby>
584
+ rake("db:migrate")
585
+ </ruby>
586
+
587
+ Available options are:
588
+
589
+ * +:env+ - Specifies the environment in which to run this rake task.
590
+ * +:sudo+ - Whether or not to run this task using +sudo+. Defaults to +false+.
591
+
592
+ h4. +capify!+
593
+
594
+ Runs the +capify+ command from Capistrano at the root of the application which generates Capistrano configuration.
595
+
596
+ <ruby>
597
+ capify!
598
+ </ruby>
599
+
600
+ h4. +route+
601
+
602
+ Adds text to the +config/routes.rb+ file:
603
+
604
+ <ruby>
605
+ route("resources :people")
606
+ </ruby>
607
+
608
+ h4. +readme+
609
+
610
+ Output the contents of a file in the template's +source_path+, usually a README.
611
+
612
+ <ruby>
613
+ readme("README")
614
+ </ruby>
615
+
364
616
  h3. Changelog
365
617
 
366
- "Lighthouse Ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/102
618
+ * December 1, 2010: Documenting the available methods and options for generators and templates by "Ryan Bigg":http://ryanbigg.com
619
+ * December 1, 2010: Addition of Rails application templates by "Ryan Bigg":http://ryanbigg.com
367
620
 
368
621
  * August 23, 2010: Edit pass by "Xavier Noria":credits.html#fxn
369
622