Skip to content

Instantly share code, notes, and snippets.

# MODEL
class Case < ActiveRecord::Base
include Eventable
has_many :tasks
concerning :Assignment do
def assign_to(new_owner:, details:)
transaction do
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
# 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
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
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:
@harlow
harlow / case_assignments_controller.rb
Last active August 29, 2015 14:12
Assigning cases to users. A de-factoring of https://gist.github.com/dhh/9348053
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) }
@harlow
harlow / config_for.rb
Last active August 29, 2015 14:12 — forked from dhh/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
# Install Git needed for Git based gems
packages:
yum:
git: []
@harlow
harlow / fake_github.rb
Last active August 29, 2015 14:16
Have stubbed service emulate failures
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 April 15, 2015 18:17
HotelTonight lookup on Clearbit
{
"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. ",