Skip to content

Instantly share code, notes, and snippets.

Avatar

Harlow Ward harlow

View GitHub Profile
View reshaping_the_command_approach.rb
# MODEL
class Case < ActiveRecord::Base
include Eventable
has_many :tasks
concerning :Assignment do
def assign_to(new_owner:, details:)
transaction do
View gist:b3d70d9bb5873838fdea
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
View gist:a9667ac1309544d32461
# Bulk API design
#
# resources :posts
class PostsController < ActiveController::Base
# GET /posts/1,4,50,90
# post_url([ @post, @post ])
def show_many
@posts = Post.find(params[:ids])
end
View gist:bfe3820adca57f7c1808
class NotesController < ApplicationController
def create
@note = @project.notes.create params[:note].merge(
creator: current_user, subscribers: extract_subscribers(params[:note]))
@note.subscribers.each { |subscriber| Subscriptions.note(@note, subscriber).deliver }
end
end
class Subscriptions < ActionMailer::Base
View example_1.go
func (h *RESTHandler) finishReq(op *Operation, req *http.Request, w http.ResponseWriter) {
result, complete := op.StatusOrResult()
obj := result.Object
if complete {
status := http.StatusOK
if result.Created {
status = http.StatusCreated
}
switch stat := obj.(type) {
case *api.Status:
View case_assignments_controller.rb
class Case::AssignmentsController < ApplicationController
before_action :set_case, :set_new_owner
def create
ActiveRecord::Base.transaction do
# update case owner
@case.update! case_params.merge(owner: @new_owner)
# transfer open tasks to new owner
@case.tasks.open.each { |task| task.update!(owner: @new_owner) }
View config_for.rb
module YourApp
class Application < Rails::Application
# Convenience for loading config/foo.yml for the current Rails env.
#
# Example:
#
# config/cleversafe.yml:
#
# production:
# url: http://127.0.0.1:8080
View 0000_packages.config
# Install Git needed for Git based gems
packages:
yum:
git: []
@harlow
harlow / fake_github.rb
Last active Aug 29, 2015
Have stubbed service emulate failures
View fake_github.rb
require 'sinatra/base'
class FakeGitHub < Sinatra::Base
cattr_reader :emulate_failure
def self.reset!
@@emulate_failure = false
end
def self.emulate_failure!
@harlow
harlow / hoteltonight.json
Created Apr 15, 2015
HotelTonight lookup on Clearbit
View hoteltonight.json
{
"id": "d5639304-f15a-48b5-9711-7bd42e6c4d72",
"name": "HotelTonight",
"legalName": "HotelTonight, Inc",
"domain": "hoteltonight.com",
"url": "http://hoteltonight.com",
"site": {
"url": "http://hoteltonight.com",
"title": "HotelTonight | Last-Minute Deals on Great Hotels",
"h1": "PLAN LESS. ",