Skip to content
This repository
tree: 9d06b49913
Fetching contributors…

Cannot retrieve contributors at this time

file 178 lines (129 sloc) 6.514 kb

API Documentation Guidelines

This guide documents the Ruby on Rails API documentation guidelines.

endprologue.

RDoc

The Rails API documentation is generated with RDoc 2.5. Please consult the documentation for help with the markup, and take into account also these additional directives.

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:

  1. 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 recommended idioms 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, Test::Unit, 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”.

English

Please use American English (color, center, modularize, etc.). See a list of American and British English spelling differences here.

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 two spaces with respect to the left margin; the examples
themselves should use Rails coding conventions.

Short docs do not need an explicit “Examples” label to introduce snippets, they just follow paragraphs:

  1. Converts a collection of elements into a formatted string by calling
  2. to_s on all elements and joining them.
    #
  3. Blog.all.to_formatted_s # => “First PostSecond PostThird Post”

On the other hand, big chunks of structured documentation may have a separate “Examples” section:

  1. ==== Examples
    #
  2. Person.exists?(5)
  3. Person.exists?(‘5’)
  4. Person.exists?(:name => “David”)
  5. Person.exists?([‘name LIKE ?’, “#{query}”])

The result of expressions follow them and are introduced by "# => ", vertically aligned:

  1. For checking if a fixnum is even or odd.
    #
  2. 1.even? # => false
  3. 1.odd? # => true
  4. 2.even? # => true
  5. 2.odd? # => false

If a line is too long, the comment may be placed on the next line:

  1. label(:post, :title)
  2. # => Title
    #
  3. label(:post, :title, “A short title”)
  4. # => A short title
    #
  5. label(:post, :title, “A short title”, :class => “title_label”)
  6. # => A short title

Avoid using any printing methods like puts or p for that purpose.

On the other hand, regular comments do not use an arrow:

  1. polymorphic_url(record) # same as comment_url(record)

Filenames

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

class Array

  1. Calls to_param on all its elements and joins the result with
  2. slashes. This is used by url_for in Action Pack.
    def to_param
    collect { |e| e.to_param }.join ‘/’
    end
    end

WARNING: Using a pair of +…+ for fixed-width font only works with words; that is: anything matching \A\w+\z. For anything else use <tt>…</tt>, notably symbols, setters, inline snippets, etc:

Regular Font

When “true” and “false” are English words rather than Ruby keywords use a regular font:

Description Lists

In lists of options, parameters, etc. use a hyphen between the item and its description (reads better than a colon because normally options are symbols):

  1. * :allow_nil – Skip validation if attribute is nil.

The description starts in upper case and ends with a full stop—it’s standard English.

Dynamically Generated Methods

Methods created with (module|class)_eval(STRING) have a comment by their side with an instance of the generated code. That comment is 2 spaces apart from the template:

for severity in Severity.constants
class_eval <<-EOT, FILE, LINE
def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
end # end
#
def #{severity.downcase}? # def debug?

  1. >= @level # DEBUG >= @level
    end # end
    EOT
    end

If the resulting lines are too wide, say 200 columns or more, we put the comment above the call:

  1. def self.find_by_login_and_activated(*args)
  2. options = args.extract_options!
  3. end
    self.class_eval %{
    def self.#{method_id}(*args)
    options = args.extract_options!

    end
    }
Something went wrong with that request. Please try again.