07 Nov 2005Rails Conventions
As I have been hacking away at Ruby On Rails I have found a number of conventions to make things easier to hack on software. The following is a list of various conventions I have adopted.
Coding style
- Two spaces, no tabs
- Blocks use { } if its a one liner, otherwise use do … end
- Surround equal signs with spaces,
@myvar = 'foo', not@myvar='foo' - Surround hash assignments with spaces,
:foo => 'bar', not:foo=>'bar' Class.method(my_arg)— notmethod( my_arg )ormethod my_arg
Derived from Typos code conventions
config/databases.yml
- config/databases.yml: Should NOT store the
config/database.ymlin source control. Instead storeconfig/database.yml.exampleand make users copy the file toconfig/database.yml. Addconfig/database.ymlinto ignore list as appropriate for source control system (svn:ignore, .cvsignore etc) to avoid being warned about non-source controlled file. - db/create.VENDOR.sql: Store creation scripts for the database in separate SQL files.
Standard Flash Usage
Place messages in the flash according to the following conventions
- * :notice* for positive feedback. eg. “User successfully updated.”
- * :message* for neutral feedback. eg. “User has 3 grace logins remaining.”
- * :warning* for negative feedback. eg. “User does not exist or password does not match.”
The view would then present this information in the following manner
<% for name in FLASH_[:notice, :warning, :message] %> <% if flash[name] %> <%= "<div id=\"#{name}\">#{flash[name]}</div>" %> <% end %> <% end %>
Derived from the email thread RFC: Standardising flash usage amongst Rails applications & generators
Posted in code and rails