Skip to content

Instantly share code, notes, and snippets.

@simonc
Last active December 16, 2015 16:00
Show Gist options
  • Save simonc/5460432 to your computer and use it in GitHub Desktop.
Save simonc/5460432 to your computer and use it in GitHub Desktop.
My coding conventions for Rails

Rails conventions

General

  • Methods should be sorted alphabeticaly

Writing a private method

class Plop
  def some_method
    #...
  end

  # Ruby 2.1
  private def some_private_method
    # ...
  end

  # Ruby < 2.1
  def some_private_method
    # ...
  end
  private :some_private_method

  def self.some_private_class_method
    # ...
  end
  private_class_method :some_private_class_method
end

Models

General layout

# ruby requires

class ClassName < ActiveRecord::Base
  # constants
  # extends/includes
  # callbacks
  # attr_accessors
  # associations
  # delegates
  # extra capabilities
  # accepts_nested_attributes_for
  # validations
  # scopes
  # other definition such as alias_method
  # class methods
  # instance methods
end

validations

  • Fields and validators should be sorted alphabeticaly
class ClassName < ActiveRecord::Base
  validates :field,
    length: { in: 1..100 },
    presence: true

  validates :other_field,
    length: { in: 1..100 },
    numericality: { only_integer: true },
    presence: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment