-
-
Notifications
You must be signed in to change notification settings - Fork 277
Rails API Documentation Conventions
As of June 2010 the official documentation is generated with RDoc 2.2.
For help in RDoc you may consult its online documentation and Chapter 16 of the Pickaxe.
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:
# 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 (which is the version we are documenting), reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.
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 single space in between the words: “Active Record”. Please write them like that. There’s a lack of unanimity in books, but we checked with David.
Spell names correctly: HTML, MySQL, JavaScript, ERb.
Using a pair of +
for fixed-width fonts only works with words; that is: anything matching \A\w+\z
:
# Use +has_many+ in the base, and +belongs_to+ in the associated model.
But <tt>
is needed for anything else, notably symbols, setters, inline snippets, etc:
# * <tt>:host</tt> - Overrides the default (current) host if provided.
Use fixed-width fonts for:
- constants, in particular class and module names
- method names
- literals like
nil
,false
,true
,self
- symbols
- method parameters
# Copies the instance variables of +object+ into +self+.
#
# Instance variable names in the +exclude+ array are ignored. If +object+
# responds to <tt>protected_instance_variables</tt> the ones returned are
# also ignored. For example, Rails controllers implement that method.
# ...
def copy_instance_variables_from(object, exclude = [])
...
end
Also use a fixed-width font for file names:
# This can also be set as a configuration option in <tt>config/environment.rb</tt>:
#
# config.action_mailer.default_url_options = { :host => "example.com" }
When “true” and “false” are English words rather than Ruby keywords use a regular font:
# If <tt>reload_plugins?</tt> is false, add this to your plugin's <tt>init.rb</tt>
# to make it reloadable:
#
# Dependencies.load_once_paths.delete lib_path
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):
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
The description starts in upper case and ends with a full stop—it’s standard English.
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 code conventions.
Short docs do not need an explicit “Examples” label to introduce snippets, they just follow paragraphs:
# Converts a collection of elements into a formatted string by calling
# <tt>to_s</tt> on all elements and joining them.
#
# Blog.find(:all).to_formatted_s # => "First PostSecond PostThird Post"
On the other hand big chunks of structured documentation may have a separate “Examples” section:
# ==== Examples
#
# Person.exists?(5)
# Person.exists?('5')
# Person.exists?(:name => "David")
# Person.exists?(['name LIKE ?', "%#{query}%"])
The result of expressions follow them and are introduced by "# => ", vertically aligned:
# For checking if a fixnum 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:
# label(:post, :title)
# # => <label for="post_title">Title</label>
#
# label(:post, :title, "A short title")
# # => <label for="post_title">A short title</label>
#
# label(:post, :title, "A short title", :class => "title_label")
# # => <label for="post_title" class="title_label">A short title</label>
In general, we avoid using any printing methods like puts
or p
.
On the other hand, regular comments do not use an arrow:
# polymorphic_url(record) # same as comment_url(record)
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?
#{severity} >= @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:
# def self.find_by_login_and_activated(*args)
# options = args.extract_options!
# ...
# end
self.class_eval %{
def self.#{method_id}(*args)
options = args.extract_options!
...
end
}
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