Skip to content

Instantly share code, notes, and snippets.

View RichardJordan's full-sized avatar

Richard Jordan RichardJordan

  • Los Angeles, CA
View GitHub Profile
require "spec_helper"
describe ExampleController do
context "GET #index" do
let(:resources) { FactoryGirl.create_list(:resource) }
before do
get :index
end
@RichardJordan
RichardJordan / sample_error_handling.rb
Last active August 29, 2015 13:56
Example: error handling
# from this blog post: http://bugroll.com/how-to-write-good-error-messages.html
# an example of good error handling
begin
update_purchase_captured_at!(purchase, captured_at)
rescue Exception => e
Honeybadger.notify(e)
Rails.logger.error(%Q{\
[#{Time.zone.now}][rake purchases:update_captured_at] Error updating \
class SessionsController < ApplicationController
expose(:login_form) { LoginForm.new }
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
reset_session
if params[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
@RichardJordan
RichardJordan / generate_haiku_name.rb
Last active August 29, 2015 13:56
Create haiku name, heroku-style, for sign-up process
module FormUtilities
module GenerateHaikuName
attr_reader :haiku_name
ADJS = [
"autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark",
"summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter",
"patient", "twilight", "dawn", "crimson", "wispy", "weathered", "blue",
"billowing", "broken", "cold", "damp", "falling", "frosty", "green",
@RichardJordan
RichardJordan / registrations_controller.rb
Last active August 29, 2015 13:56
Registration of user and account: RegistrationsController
# see https://gist.github.com/RichardJordan/9059633 for explanation
# -----------------------------------------------------------------
# in routes.rb - resources :registrations, only: [:new, :create]
class RegistrationsController < ApplicationController
include FormUtilities::GenerateHaikuName
attr_writer :registrar_source
@RichardJordan
RichardJordan / registration_of_user_and_account.md
Last active August 29, 2015 13:56
Registration of user and account: a series of gists which show a way to handle registration where multiple models need to be created and saved.

Registration of user and account

We often need to register both a user and an account at the same time. A popular approach to this is to load up on active_model callbacks but that is a route to future problems. It also encourages far to much coupling to the framework itself.

This approach uses a hexagonal / ports & adapters approach separating out domain logic from Rails. We are using a fewadvanced techniques here.

@RichardJordan
RichardJordan / registration.rb
Last active August 29, 2015 13:56
Registration of user and account: Registration form object
# see https://gist.github.com/RichardJordan/9059633 for explanation
# -----------------------------------------------------------------
class Registration
extend ActiveModel::Naming
include ActiveModel::Model
include ActiveModel::Validations::Callbacks
include ActiveModel::ForbiddenAttributesProtection
attr_accessor :email, :password, :password_confirmation,
@RichardJordan
RichardJordan / registrar.rb
Last active August 29, 2015 13:56
Registration of user and account: the Registrar model, shepherding the registration process
# see https://gist.github.com/RichardJordan/9059633 for explanation
# -----------------------------------------------------------------
require_relative '../../lib/abilities/roles_admin'
class Registrar < Struct.new(:listener)
attr_writer :admin_user_source, :registration_source, :responsibilities
delegate :account, :fullname, :shortname, :email, :password,
@RichardJordan
RichardJordan / roles_admin.rb
Last active August 29, 2015 13:56
Registration of user and account: RolesAdmin module to add ability to grant roles
# see https://gist.github.com/RichardJordan/9059633 for explanation
# -----------------------------------------------------------------
module RolesAdmin
attr_writer :role_source
def grant_role!(name, on_account: nil, to_user: nil)
role = new_role(name: name, account: on_account, user: to_user)
role.grant(self)
@RichardJordan
RichardJordan / role.rb
Created February 17, 2014 22:37
Registration of user and account: Role.rb, which manages roles for permissions and other uses
# see https://gist.github.com/RichardJordan/9059633 for explanation
# -----------------------------------------------------------------
############################################
# create_table "roles", force: true do |t| #
# t.integer "account_id", null: false #
# t.integer "user_id", null: false #
# t.string "name", null: false #
# t.datetime "created_at" #
# t.datetime "updated_at" #