Skip to content

Instantly share code, notes, and snippets.

View ssaunier's full-sized avatar

Sébastien Saunier ssaunier

View GitHub Profile
@apneadiving
apneadiving / before.rb
Last active August 29, 2015 14:07
The before is some standard code, `with_waterfall` presents another way to write it (fully chainable)
# In controller
result = TaxCalculator.new(order).call
if result[:success]
render json: { value: result[:value] }
else
render json: { errors: result[:errors] }, status: 422
end
# The service
class TaxCalculator
@pcreux
pcreux / pipable.rb
Last active June 12, 2018 17:08
*nix has pipes, Elixir has pipes, Ruby deserves pipes.
# Elixir has pipes `|>`. Let's try to implement those in Ruby.
#
# I want to write this:
#
# email.body | RemoveSignature | HighlightMentions | :html_safe
#
# instead of:
#
# HighlightMentions.call(RemoveSignature.call(email.body)).html_safe
#
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_permitted_parameters
# Handles editing when provider is facebook
def update_resource(resource, params)
if current_user.provider == "facebook"
params.delete("current_password")
resource.update_without_password(params)
else
resource.update_with_password(params)
@dhh
dhh / test_induced_design_damage.rb
Last active June 22, 2023 06:18
This is an extraction from Jim Weirich's "Decoupling from Rails" talk, which explained how to apply the hexagonal design pattern to make every layer of your application easily unit testable (without touching the database etc). It only seeks to extract a single method, the EmployeesController#create method, to illustrate the design damage that's …
# Original Rails controller and action
class EmployeesController < ApplicationController
def create
@employee = Employee.new(employee_params)
if @employee.save
redirect_to @employee, notice: "Employee #{@employee.name} created"
else
render :new
end
@matthewlein
matthewlein / Gruntfile.js
Last active April 8, 2020 07:54
The simplest way to have grunt run a server, watch all the files for changes, and livereload on change. Set up to compile SASS and livereload the css on changes.Using the package.json, all you need to do is npm install and then it should work.
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
// don't watch node_modules
// used in watch files below
var excludes = [
'!**/node_modules/**'
@thbar
thbar / maintenance.rake
Created November 28, 2013 10:10
Quick, slow, dirty way to verify how aged your gems are.
task :outdated => :environment do
include ActionView::Helpers::DateHelper
regexp = /\* ([^ ]+) \((\S+) > ([^)]+)\)/
outdated = `bundle outdated`.scan(regexp)
outdated.each do |gem_name, available, current|
data = JSON.parse(`curl --silent https://rubygems.org/api/v1/versions/#{gem_name}.json`)
version = data.find { |e| e['number'] == current }
age = distance_of_time_in_words_to_now(Time.parse(version['built_at']))
puts " * #{gem_name}: (#{available} > #{current}, built #{age} ago)"
end
@pcreux
pcreux / Gemfile
Last active December 11, 2023 20:24
Fast Rails + Heroku Configuration
group :production do
gem 'unicorn'
# Enable gzip compression on heroku, but don't compress images.
gem 'heroku-deflater'
# Heroku injects it if it's not in there already
gem 'rails_12factor'
end
@lukehefson
lukehefson / uninstall-GHfM.sh
Created November 27, 2013 13:48
Completely uninstall GitHub for Mac
#!/bin/bash
function remove_dir () {
rm -rf "$1_"
if [ -d "$1" ]
then
mv "$1" "$1_"
fi
}
École du tech lead: Conversation autour de la dette technique
----
TLDR de Cyrille Deruel @CyrilleDeruel
Les 3 phrases sur la dettes
- Pas dépuration de la dette sans refactoring et pas de refactoring sans tests automatisés
- Normalement tu n'as pas besoin d'aller chercher la dette, c'est la dette qui te trouve
- Ta dette tu la gères tous les jours
@ssaunier
ssaunier / Gemfile
Last active December 24, 2015 21:19
Prevent stubbing nonexistant method with rspec-fire and rspec-mocks. Discussed at http://sebastien.saunier.me/blog/2013/10/06/on-testing.html
source 'https://rubygems.org'
ruby "2.0.0"
gem "rspec"
gem "rspec-mocks"
gem "rspec-fire"