Skip to content

Latest commit

 

History

History

ruby

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Ruby Style Guide

Sample

  • Avoid conditional modifiers (lines that end with conditionals).
  • Avoid multiple assignments per line (one, two = 1, 2).
  • Avoid organizational comments (# Validations).
  • Avoid ternary operators (boolean ? true : false). Use multi-line if instead to emphasize code branches.
  • Avoid explicit return statements.
  • Avoid using semicolons.
  • Avoid assignments on conditionals.
  • Prefer detect over find.
  • Prefer inject over reduce.
  • Prefer map over collect.
  • Prefer select over find_all.
  • Prefer single quotes for strings.
  • Use do...end for multi line blocks and {...} for one line blocks.
  • Use _ for unused block parameters.
  • Use %{} for single-line strings needing interpolation and double-quotes.
  • Use && and || for Boolean expressions.
  • Use ? suffix for predicate methods.
  • Use CamelCase for classes and modules, snake_case for variables and methods, SCREAMING_SNAKE_CASE for constants.
  • Use def self.method, not def Class.method or class << self.
  • Use def with parentheses when there are arguments.
  • Use each, not for, for iteration.
  • Use heredocs for multi-line strings.
  • Use %w() and %i() for hard-coded lists of strings and symbols.

Rails

  • Avoid member and collection routes.
  • Use private instead of protected when defining controller methods.
  • Name date columns with _on suffixes.
  • Name datetime columns with _at suffixes.
  • Name initializers for their gem name.
  • Order ActiveRecord associations alphabetically by attribute name.
  • Order ActiveRecord validations alphabetically by attribute name.
  • Order controller contents: filters, public methods, private methods.
  • Order model contents: constants, macros, public methods, private methods.
  • Put application-wide partials in the app/views/application directory.
  • Use def self.method, not the scope :method DSL.
  • Use the default render 'partial' syntax over render partial: 'partial'.

Rails Routes

  • Avoid the :except option in routes.
  • Order resourceful routes alphabetically by name.
  • Use the :only option to explicitly state exposed routes.

Background Jobs

  • Define a PRIORITY constant at the top of delayed job classes.
  • Define two public methods: self.enqueue and perform.
  • Put delayed job classes in app/jobs.

Email

Testing

Sample

  • Follow the best practices
  • Prefer factories to fixtures.
  • Avoid the private keyword in specs.
  • Prefer eq to == in RSpec.
  • Separate setup, exercise, verification, and teardown phases with newlines.
  • Use RSpec's expect syntax.
  • Use not_to instead of to_not in RSpec expectations.
  • Use an it example for each execution path through the method.
  • Use shared contexts but include them explicitly with include_context

Acceptance Tests

  • Avoid scenario titles that add no information, such as "successfully."
  • Avoid scenario titles that repeat the feature title.
  • Place helper methods for feature specs directly in a top-level Features module.
  • Use Capybara's feature/scenario DSL.
  • Use names like ROLE_ACTION_spec.rb, such as user_changes_password_spec.rb, for feature spec file names.
  • Use only one feature block per feature spec file.
  • Use scenario titles that describe the success and failure paths.
  • Use spec/features directory to store feature specs.
  • Use spec/support/features for support code related to feature specs.

Factories

  • Order factories.rb contents: sequences, traits, factory definitions.
  • Order factory attributes: implicit attributes, explicit attributes, child factory definitions. Each section's attributes are alphabetical.
  • Order factory definitions alphabetically by factory name.
  • Use one factories.rb file per project.

Unit Tests

Sample

  • Don't prefix it block descriptions with 'should'.
  • Use context blocks for each method under test and name them accordingly. Use .method for class methods and #method for instance methods.