Skip to content

Instantly share code, notes, and snippets.

@josephchoe
josephchoe / test_induced_design_damage.rb
Created September 22, 2017 20:28 — forked from dhh/test_induced_design_damage.rb
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
@josephchoe
josephchoe / rails-new-bare
Last active June 13, 2019 17:08 — forked from thewoolleyman/rails-new-bare
command to generate a barebones no-js no-db rails app
#!/usr/bin/env bash
rails new --skip-javascript --skip-turbolinks --skip-keeps --skip-git --skip-bundle --skip-test --skip-coffee testing-rails $1
rails new --skip-keeps --skip-git --skip-bundle --skip-test --skip-action-mailer --skip-action-cable testing-rails --api
bundle exec rails new \
--skip-keeps \
--skip-git \
--skip-bundle \
# app/serializers/customer_serializer_spec.rb
class CustomerSerializer < ActiveModel::Serializer
attributes :customer_number, :email, :username
def customer_number
object.account_number
end
def merchant_id
@josephchoe
josephchoe / validate_facebook_signature.rb
Created September 8, 2017 17:47 — forked from schneems/validate_facebook_signature.rb
validate facebook signed_request in ruby
require 'base64'
def base64_url_decode(str)
str += '=' * (4 - str.length.modulo(4))
Base64.decode64(str.gsub("-", "+").gsub("_", "/"))
end # source: https://github.com/ptarjan/base64url/blob/master/ruby.rb
require 'hmac'
require 'hmac-sha2' ## used to decode facebook return values
@josephchoe
josephchoe / sinatra_proxy
Created September 6, 2017 19:52 — forked from burgalon/ sinatra_proxy
Testing client side app (like BackboneJS) with Capybara, VCR, webmock, factory girl
Testing client side app (like BackboneJS) with Capybara, VCR, webmock, factory girl
# Today's little useless tidbit converts a url's query string into a hash
# set of k/v pairs in a way that merges duplicates and massages empty
# querystring values
def qs_to_hash(query_string)
keyvals = query_string.split('&').inject({}) do |result, q|
k,v = q.split('=')
if !v.nil?
result.merge({k => v})
elsif !result.key?(k)