Skip to content

Whiteboard Style Guide: Rails

Chad edited this page Jan 15, 2016 · 9 revisions

Whiteboard-Rails-Style-Guide

Standard rules (suggestions) for Rails code at Whiteboard

Ruby

Methods
  • Methods should be 5 lines long or less
  • Method names should be clear, in english, what the method is doing
    • get_inv_cdt_lmt should be get_invoice_credit_limit
  • Methods should be separate by a single blank line
# BAD
def method_one
  # stuff
end
def method_two
  # stuff
end

# GOOD
def method_one
  # stuff
end

def method_two
  # stuff
end
  • Private methods under the private keyword can EITHER be indented or inline with public methods, just be consistent within your classes

Models

Model File Structure
class User < ActiveRecord::Base
  
  # scopes
  
  # CONSTANTS
  
  # attr_ - whatevers
  
  # belongs_tos
  
  # has_manys
  
  # validations on fields
  
  # custom validations
  
  # callbacks
  
  # methods
  
end
  

Controllers

Controller File Structure
class PostsController < ApplicationController

  # includes Modules / Concerns
  
  # before / after filters

  # REST actions ... index, show, create etc

  # Non REST actions ... any custom controller methods / actions
  # ^^ Think hard about this, lots of times a new controller is warranted for
  #    non REST actions.

private

  # params, small methods to help application flow. KEEP CONTROLLER ACTIONS SMALL

  # helper methods for views. Define helper method directly under method declaration
  # That way you know immediately whether a method is defined as a helper method

  def post_owner
    post.user
  end
  helper_method :post_owner


end
Before/After/Around Filters

Before filters and others are great for Authentication ( authenticate_user! ) or Authorization ( current_user.admin? ). Filters should not be used to set instance variables, and especially should not depend on other filters.

Clone this wiki locally