contributing_to_ruby_on_rails.textile 19.4 KB
Newer Older
1 2 3 4
h2. Contributing to Ruby on Rails

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:

5
* Using GitHub to report issues
6 7 8 9 10 11 12 13 14 15 16
* Cloning master and running the test suite
* Helping to resolve existing issues
* Contributing to the Ruby on Rails documentation
* Contributing to the Ruby on Rails code

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.

endprologue.

h3. Reporting an Issue

17
Ruby on Rails uses "GitHub Issue Tracking":https://github.com/rails/rails/issues 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) GitHub account in order to either submit an issue, comment on them or create pull requests.
18 19 20 21 22

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.

h4. Creating a Bug Report

23
If you've found a problem in Ruby on Rails which is not a security risk do a search in GitHub Issues in case it was already reported. If you find no issue addressing it you can "add a new one":https://github.com/rails/rails/issues/new. (See the next section for reporting security issues.)
24

25
At the minimum, your issue report 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

A
s/a/an/  
Akira Matsuda 已提交
27
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 issue report in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the issue report will automatically see any activity or that others will jump to fix it. Creating an issue 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.
28 29 30

h4. Special Treatment for Security Issues

31
WARNING: Please do not report security vulnerabilities with public GitHub issue reports. The "Rails security policy page":http://rubyonrails.org/security details the procedure to follow for security issues.
32 33 34

h4. What About Feature Requests?

35
Please don't put "feature request" items into GitHub Issues. 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 GitHub Issues with no code, you can expect it to be marked "invalid" as soon as it's reviewed.
36 37 38 39 40 41 42 43 44 45 46

h3. Running the Test Suite

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.

h4. Install git

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:

* "Everyday Git":http://www.kernel.org/pub/software/scm/git/docs/everyday.html will teach you just enough about git to get by.
* The "PeepCode screencast":https://peepcode.com/products/git on git ($9) is easier to follow.
V
Vijay Dev 已提交
47
* "GitHub":http://help.github.com offers links to a variety of git resources.
48 49 50 51 52 53 54
* "Pro Git":http://progit.org/book/ is an entire book about git with a Creative Commons license.

h4. Clone the Ruby on Rails Repository

Navigate to the folder where you want the Ruby on Rails source code (it will create its own +rails+ subdirectory) and run:

<shell>
55 56
$ git clone git://github.com/rails/rails.git
$ cd rails
57 58 59 60 61 62
</shell>

h4. Set up and Run the Tests

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.

63 64 65
Install first libxml2 and libxslt together with their development files for Nokogiri. In Ubuntu that's

<shell>
66
$ sudo apt-get install libxml2 libxml2-dev libxslt1-dev
67 68 69 70 71
</shell>

Also, SQLite3 and its development files for the +sqlite3-ruby+ gem, in Ubuntu you're done with

<shell>
72
$ sudo apt-get install sqlite3 libsqlite3-dev
73
</shell>
74 75 76 77

Get a recent version of "Bundler":http://gembundler.com/:

<shell>
78
$ gem install bundler
79 80 81 82 83
</shell>

and run:

<shell>
84
$ bundle install --without db
85 86 87 88 89
</shell>

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:

<shell>
90
$ rake test
91 92
</shell>

A
s/an/a/  
Akira Matsuda 已提交
93
You can also run tests for a specific framework, like Action Pack, by going into its directory and executing the same command:
94 95

<shell>
96 97
$ cd actionpack
$ rake test
98 99
</shell>

100 101 102 103 104 105 106
If you want to run tests from the specific directory use the +TEST_DIR+ environment variable. For example, this will run tests inside +railties/test/generators+ directory only:

<shell>
$ cd railties
$ TEST_DIR=generators rake test
</shell>

107 108 109 110 111 112 113
h4. Warnings

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.

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:

<shell>
114
$ RUBYOPT=-W0 rake test
115 116 117 118 119 120 121 122
</shell>

h4. Testing Active Record

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.

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.

123 124
h5. Set up Database Configuration

C
Chris Kimpton 已提交
125
The Active Record test suite requires a custom config file: +activerecord/test/config.yml+ . 
126 127 128 129
An example is provided: +activerecord/test/config.example.yml+ - copy this and amend as needed for your environment.  

The above should happen automatically - so this section may be removed when the underlying issue that causes the file to be 
missing/corrupt is resolved.
130 131 132

The SQLite3 config should work "as is".

133 134 135 136 137
h5. SQLite3

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:

<shell>
138 139
$ cd activerecord
$ rake test_sqlite3
140 141 142 143
</shell>

h5. MySQL and PostgreSQL

144 145 146
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

<shell>
147 148
$ sudo apt-get install mysql-server libmysqlclient15-dev
$ sudo apt-get install postgresql postgresql-client postgresql-contrib libpq-dev
149
</shell>
150 151 152 153

After that run:

<shell>
154 155
$ rm .bundle/config
$ bundle install
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
</shell>

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).

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:

<shell>
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
       to 'rails'@'localhost';
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
       to 'rails'@'localhost';
</shell>

and create the test databases:

<shell>
172 173
$ cd activerecord
$ rake mysql:build_databases
174 175 176 177 178
</shell>

PostgreSQL's authentication works differently. A simple way to setup the development environment for example is to run with your development account

<shell>
179
$ sudo -u postgres createuser --superuser $USER
180 181 182 183 184
</shell>

and after that create the test databases with

<shell>
185 186
$ cd activerecord
$ rake postgresql:build_databases
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
</shell>

NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation.

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.

You can now run tests as you did for +sqlite3+, the tasks are

<shell>
test_mysql
test_mysql2
test_postgresql
</shell>

respectively. As we mentioned before

<shell>
204
$ rake test
205 206 207 208 209 210 211 212
</shell>

will now run the four of them in turn.

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.

h4. Older versions of Ruby on Rails

213
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 3-0-stable branch:
214 215

<shell>
216 217
$ git branch --track 3-0-stable origin/3-0-stable
$ git checkout 3-0-stable
218 219 220 221 222 223
</shell>

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.

h3. Helping to Resolve Existing Issues

224
As a next step beyond reporting issues, you can help the core team resolve existing issues. If you check the "Everyone's Issues":https://github.com/rails/rails/issues?sort=created&direction=desc&state=open&page=1 list in GitHub Issues, you'll find lots of issues already requiring attention. What can you do for these? Quite a bit, actually:
225 226 227

h4. Verifying Bug Reports

228
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 issue saying that you're seeing the same thing.
229 230 231 232 233 234 235 236 237

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.

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.

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.

h4. Testing Patches

238
You can also help out by examining pull requests that have been submitted to Ruby on Rails via GitHub. To apply someone's changes you need to first create a dedicated branch:
239 240

<shell>
241
$ git checkout -b testing_branch
242 243
</shell>

244
Then you can use their remote branch to update your codebase. For example, let's say the GitHub user JohnSmith has forked and pushed to the topic branch located at https://github.com/JohnSmith/rails.
245 246

<shell>
247
$ git remote add JohnSmith git://github.com/JohnSmith/rails.git
248
$ git pull JohnSmith topic
249 250
</shell>

251
After applying their branch, test it out! Here are some things to think about:
252

253
* Does the change actually work?
254 255 256 257
* Are you happy with the tests? Can you follow what they're testing? Are there any tests missing?
* Does it have proper documentation coverage? Should documentation elsewhere be updated?
* Do you like the implementation? Can you think of a nicer or faster way to implement a part of their change?

258
Once you're happy that the pull request contains a good change, comment on the GitHub issue indicating your approval. Your comment should indicate that you like the change and what you like about it. Something like:
259 260 261 262 263

<blockquote>
I like the way you've restructured that code in generate_finder_sql, much nicer. The tests look good too.
</blockquote>

264
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 pull request.
265 266 267 268 269

h3. Contributing to the Rails Documentation

Ruby on Rails has two main sets of documentation: The guides help you to learn Ruby on Rails, and the API is a reference.

270 271 272 273
You can help improve the Rails guides by making them more coherent, adding missing information, correcting factual errors, fixing typos, bringing it up to date with the latest edge Rails. To get involved in the translation of Rails guides, please see "Translating Rails Guides":https://wiki.github.com/lifo/docrails/translating-rails-guides.

If you're confident about your changes, you can push them yourself directly via "docrails":https://github.com/lifo/docrails. 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.

V
Vijay Dev 已提交
274
If you are unsure of the documentation changes, you can create an issue in the "Rails":https://github.com/rails/rails/issues issues tracker on GitHub.
275 276 277

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.

278
NOTE: As explained earlier, ordinary code patches should have proper documentation coverage. docrails is only used for isolated documentation improvements.
279

280
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. Also, CHANGELOGs should never be edited in docrails.
281 282 283 284 285 286 287 288

h3. Contributing to the Rails Code

h4. Clone the Rails Repository

The first thing you need to do to be able to contribute code is to clone the repository:

<shell>
289
$ git clone git://github.com/rails/rails.git
290 291 292 293 294
</shell>

and create a dedicated branch:

<shell>
295 296
$ cd rails
$ git checkout -b my_new_branch
297 298
</shell>

299
It doesn’t really matter what name you use, because this branch will only exist on your local computer and your personal repository on Github. It won't be part of the Rails git repository.
300 301 302 303 304 305 306 307 308 309 310 311 312 313

h4. Write Your Code

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:

* Get the code right
* Use Rails idioms and helpers
* Include tests that fail without your code, and pass with it
* Update the documentation, the surrounding one, examples elsewhere, guides, whatever is affected by your contribution

h4. Follow the Coding Conventions

Rails follows a simple set of coding style conventions.

314 315 316 317 318 319 320 321 322
* Two spaces, no tabs.
* No trailing whitespace. Blank lines should not have any space.
* Indent after private/protected.
* Prefer +&amp;&amp;+/+||+ over +and+/+or+.
* Prefer class << self block over self.method for class methods.
* +MyClass.my_method(my_arg)+ not +my_method( my_arg )+ or +my_method my_arg+.
* a = b and not a=b.
* Follow the conventions you see used in the source already.

323
These are some guidelines and please use your best judgment in using them.
324 325 326 327 328 329 330 331 332 333 334 335

h4. Sanity Check

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.

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.

h4. Commit Your Changes

When you're happy with the code on your computer, you need to commit the changes to git:

<shell>
336
$ git commit -a -m "Here is a commit message on what I changed in this commit"
337 338 339 340 341 342 343
</shell>

h4. Update master

It’s pretty likely that other changes to master have happened while you were working. Go get them:

<shell>
344 345
$ git checkout master
$ git pull
346 347 348 349 350
</shell>

Now reapply your patch on top of the latest changes:

<shell>
351 352
$ git checkout my_new_branch
$ git rebase master
353 354 355 356
</shell>

No conflicts? Tests still pass? Change still seems reasonable to you? Then move on.

357
h4. Fork
358

359
Navigate to the Rails "GitHub repository":https://github.com/rails/rails and press "Fork" in the upper right hand corner.
360

361
Add the new remote to your local repository on your local machine:
362 363

<shell>
364
$ git remote add mine git@github.com:<your user name>/rails.git
365 366
</shell>

367
Push to your remote:
368 369

<shell>
370
$ git push mine my_new_branch
371
</shell>
372

373
h4. Issue a Pull Request
374

375
Navigate to the Rails repository you just pushed to (e.g. https://github.com/your-user-name/rails) and press "Pull Request" in the upper right hand corner.
376 377 378 379

Write your branch name in branch field (is filled with master by default) and press "Update Commit Range"

Ensure the changesets you introduced are included in the "Commits" tab and that the "Files Changed" incorporate all of your changes.
380 381

Fill in some details about your potential patch including a meaningful title. When finished, press "Send pull request." Rails Core will be notified about your submission.
382 383 384

h4. Get Some Feedback

N
Nicolas Cavigneaux 已提交
385
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":http://groups.google.com/group/rubyonrails-core/ or the #rails-contrib channel on IRC freenode for this. You might also try just talking to Rails developers that you know.
386 387 388 389 390 391 392 393 394 395

h4. Iterate as Necessary

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.

And then...think about your next contribution!

h3. Rails Contributors

All contributions, either via master or docrails, get credit in "Rails Contributors":http://contributors.rubyonrails.org.