Conseils à la volée pour écrire du meilleur code en ruby.

Rails starter

  • rails starter template : script qui permet d’installer des gems de bases, et certaines avec des conditions (nécessité de répondre à des questions pdt l’install)
  • Rails Composer The Rails generator on steroids for starter apps

Architecture guidelines

Voir aussi les ressources architecture dans Ressources.

  • Models : associations and constants (callbacks -> service objects, validations -> Form objects)
  • Controllers : HTTP routing, parameters parsing, authentication, content negotiation, calling the right service or editor object, exception catching, response formatting, and returning the right HTTP status code. The remaining -> service objects.
  • Helpers : used for utility methods. Otherwise -> Decorators
  • Always pass one instance variable per view.

Service objects

  • Called from controller.
  • replaces model callbacks
  • Services should call Query objects, and should not store state. Use instance methods, not class methods. There should be very few public methods in keeping with SRP.

Form objects

Replaces ActiveRecord models naming and validations for the model

Value objects

  • to keep your code cleaner and to group related attributes
  • use cases : money, date range
  • Value objects should have multiple attributes.
  • Attributes should be immutable throughout the object’s life cycle.
  • Equality is determined by the object’s attributes.

Query objects

  • Query object methods should return an object, a hash or an array, not an ActiveRecord association.

Decorators / Delegators

  • great way to use polymorphism — providing different implementations for different contexts or types, over-riding or sub-classing helpers.
  • replaces concerns too ?
  • for specific use cases : formatting model attributes for any kind of presentation logic.
  • Keep them light and breezy.

Ressources

Articles

Architecture & POROs :

Bouquins

Gems à tester

Linting / Debug / DevOps

  • rubocop : A Ruby static code analyzer, based on the community Ruby style guide
  • pronto : Quick automated code review of your changes
  • reek : Code smell detector for Ruby
  • overcommit : A fully configurable and extendable Git hook manager
  • whenever : Cron jobs in Ruby
  • ahoy : Simple, powerful visit tracking for Rails
  • turnout : Turnout makes it easy to put Rack apps into maintenance mode
  • brakeman : A static analysis security vulnerability scanner for Ruby on Rails applications mode
  • rack-mini-profiler : Profiler for your development and production Ruby rack apps.
  • bullet : help to kill N+1 queries and unused eager loading

Utilities

  • kaminari : A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for Ruby webapps (à la place de will_paginate ?)
  • sitemap_generator) : framework-agnostic XML Sitemap generator written in Ruby with automatic Rails integration. It supports Video, News, Image, Mobile, PageMap and Alternate Links sitemap extensions and includes Rake tasks for managing your sitemaps, as well as many other great features.

API

  • Json API rb : Efficient and convenient JSON API library for ruby (upgrade de active model serializer, en gros)
  • Json API serializers : Pure Ruby readonly serializers for the JSON:API spec.

Code structure

  • interactor : common interface for performing complex user interactions. (made by CollectiveIdea)
  • trailblazer : A High-Level Architecture for Ruby.
  • draper : Decorators/View-Models for Rails Applications
  • wisper : A micro library providing Ruby objects with Publish-Subscribe capabilities

Divers

  • Exposing your localhost to the Internet : https://www.sitepoint.com/adding-sms-capabilities-to-your-rails-app/#gist2970916

do / don’t

Batch process

plutôt que

Post.where(published: true).each do |post|
  post.archive!
end

faire :

Post.where(published: true).find_each do |post|
  post.archive!
end

Git

Supprimer d’un repo un fichier gitignoré sur le tard et déjà pushé :

git rm --cached `git ls-files -i --exclude-from=.gitignore`

Git commit standard

Mike Foley

  • Line Length: All lines must be <= 72 chars (URLs excluded). First line should be <= 50 chars. Second line must be blank.
  • Tense: Message must use imperative present tense: « Fix bug » and not « Fixed bug » or « Fixes bug. »
  • Subject Period: Do not end your subject line with a period.
  • Capitalize Subject: Begin all subject lines with a capital letter.
  • Frat House: No offensive content.
  • WIP: Do not commit WIPs to shared branches (disabled by default)

Cf Fit commit hook

Exemple :

git commit -m 'Init project' -m $'- Base gemfile\n- DB config'    

ce qui donne :

Init project

- Base gemfile
- DB config

Sources :