**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.** API Documentation Guidelines ============================ This guide documents the Ruby on Rails API documentation guidelines. After reading this guide, you will know: * How to write effective prose for documentation purposes. * Style guidelines for documenting different kinds of Ruby code. -------------------------------------------------------------------------------- RDoc ---- The [Rails API documentation](https://api.rubyonrails.org) is generated with [RDoc](https://ruby.github.io/rdoc/). To generate it, make sure you are in the rails root directory, run `bundle install` and execute: ```bash bundle exec rake rdoc ``` Resulting HTML files can be found in the ./doc/rdoc directory. Please consult the RDoc documentation for help with the [markup](https://ruby.github.io/rdoc/RDoc/Markup.html), and also take into account these [additional directives](https://ruby.github.io/rdoc/RDoc/Parser/Ruby.html). Wording ------- Write simple, declarative sentences. Brevity is a plus: get to the point. Write in present tense: "Returns a hash that...", rather than "Returned a hash that..." or "Will return a hash that...". Start comments in upper case. Follow regular punctuation rules: ```ruby # Declares an attribute reader backed by an internally-named # instance variable. def attr_internal_reader(*attrs) ... end ``` Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage. Documentation has to be concise but comprehensive. Explore and document edge cases. What happens if a module is anonymous? What if a collection is empty? What if an argument is nil? The proper names of Rails components have a space in between the words, like "Active Support". `ActiveRecord` is a Ruby module, whereas Active Record is an ORM. All Rails documentation should consistently refer to Rails components by their proper name, and if in your next blog post or presentation you remember this tidbit and take it into account that'd be phenomenal. Spell names correctly: Arel, minitest, RSpec, HTML, MySQL, JavaScript, ERB. When in doubt, please have a look at some authoritative source like their official documentation. Use the article "an" for "SQL", as in "an SQL statement". Also "an SQLite database". Prefer wordings that avoid "you"s and "your"s. For example, instead of ```markdown If you need to use `return` statements in your callbacks, it is recommended that you explicitly define them as methods. ``` use this style: ```markdown If `return` is needed it is recommended to explicitly define a method. ``` That said, when using pronouns in reference to a hypothetical person, such as "a user with a session cookie", gender neutral pronouns (they/their/them) should be used. Instead of: * he or she... use they. * him or her... use them. * his or her... use their. * his or hers... use theirs. * himself or herself... use themselves. English ------- Please use American English (*color*, *center*, *modularize*, etc). See [a list of American and British English spelling differences here](https://en.wikipedia.org/wiki/American_and_British_English_spelling_differences). Oxford Comma ------------ Please use the [Oxford comma](https://en.wikipedia.org/wiki/Serial_comma) ("red, white, and blue", instead of "red, white and blue"). Example Code ------------ Choose meaningful examples that depict and cover the basics as well as interesting points or gotchas. Use two spaces to indent chunks of code--that is, for markup purposes, two spaces with respect to the left margin. The examples themselves should use [Rails coding conventions](contributing_to_ruby_on_rails.html#follow-the-coding-conventions). Short docs do not need an explicit "Examples" label to introduce snippets; they just follow paragraphs: ```ruby # Converts a collection of elements into a formatted string by # calling +to_s+ on all elements and joining them. # # Blog.all.to_formatted_s # => "First PostSecond PostThird Post" ``` On the other hand, big chunks of structured documentation may have a separate "Examples" section: ```ruby # ==== Examples # # Person.exists?(5) # Person.exists?('5') # Person.exists?(name: "David") # Person.exists?(['name LIKE ?', "%#{query}%"]) ``` The results of expressions follow them and are introduced by "# => ", vertically aligned: ```ruby # For checking if an integer is even or odd. # # 1.even? # => false # 1.odd? # => true # 2.even? # => true # 2.odd? # => false ``` If a line is too long, the comment may be placed on the next line: ```ruby # label(:article, :title) # # => # # label(:article, :title, "A short title") # # => # # label(:article, :title, "A short title", class: "title_label") # # => ``` Avoid using any printing methods like `puts` or `p` for that purpose. On the other hand, regular comments do not use an arrow: ```ruby # polymorphic_url(record) # same as comment_url(record) ``` Booleans -------- In predicates and flags prefer documenting boolean semantics over exact values. When "true" or "false" are used as defined in Ruby use regular font. The singletons `true` and `false` need fixed-width font. Please avoid terms like "truthy", Ruby defines what is true and false in the language, and thus those words have a technical meaning and need no substitutes. As a rule of thumb, do not document singletons unless absolutely necessary. That prevents artificial constructs like `!!` or ternaries, allows refactors, and the code does not need to rely on the exact values returned by methods being called in the implementation. For example: ```markdown `config.action_mailer.perform_deliveries` specifies whether mail will actually be delivered and is true by default ``` the user does not need to know which is the actual default value of the flag, and so we only document its boolean semantics. An example with a predicate: ```ruby # Returns true if the collection is empty. # # If the collection has been loaded # it is equivalent to collection.size.zero?. If the # collection has not been loaded, it is equivalent to # collection.exists?. If the collection has not already been # loaded and you are going to fetch the records anyway it is better to # check collection.length.zero?. def empty? if loaded? size.zero? else @target.blank? && !scope.exists? end end ``` The API is careful not to commit to any particular value, the method has predicate semantics, that's enough. File Names ---------- As a rule of thumb, use filenames relative to the application root: ``` config/routes.rb # YES routes.rb # NO RAILS_ROOT/config/routes.rb # NO ``` Fonts ----- ### Fixed-width Font Use fixed-width fonts for: * Constants, in particular class and module names. * Method names. * Literals like `nil`, `false`, `true`, `self`. * Symbols. * Method parameters. * File names. ```ruby class Array # Calls +to_param+ on all its elements and joins the result with # slashes. This is used by +url_for+ in Action Pack. def to_param collect { |e| e.to_param }.join '/' end end ``` WARNING: Using `+...+` for fixed-width font only works with simple content like ordinary method names, symbols, paths (with forward slashes), etc. Please use `...` for everything else, notably class or module names with a namespace as in `ActiveRecord::Base`. You can quickly test the RDoc output with the following command: ```bash $ echo "+:to_param+" | rdoc --pipe # =>
:to_param