Skip to content

Instantly share code, notes, and snippets.

View Commands to Reproduce
# setup
gem install rails -v $RAILS_VERSION && \
rails _${RAILS_VERSION}_ new rails-${RAILS_VERSION}-test && \
cd rails-${RAILS_VERSION}-test && \
rails generate model User && \
rails db:migrate && \
echo 'class User < ActiveRecord::Base; FOO = where(id: [1]); end' > app/models/user.rb && \
echo 'Rails.application.routes.draw { User }' > config/routes.rb
View user_workspaces.sql
-- user_workspaces_v01
-- Pull Organizations and Workspaces affiliated with,
-- and also organizations we have projects we are collaborating on
-- first version :: ugly and super slow, see updated version next
WITH user_workspaces AS (
SELECT a.user_id,
FROM user_affiliations a
nthj / calculator_dsl.rb
Created Nov 17, 2015
DSL for calculating various fees on an eCommerce purchase. Developed for @Ticketbud. Not our actual equation, but an example of how you could use it...
View calculator_dsl.rb
# Assumes we respond_to #fee_schedule, which has various details,
# like processor_rate, merchant_custom_handling_fee, or the like.
module CalculatorDSL
extend ActiveSupport::Concern
included do
singleton_class.send :alias_method, :method_missing, :define_method
nthj / example.rb
Last active Oct 14, 2016
Methods I like to monkey-patch onto the Object class in Ruby
View example.rb
# Say you want to look up the attrs of a Stripe Event for logging to your internal database.
attrs = begin
retriable(Stripe::APIConnectionError, Stripe::APIError, max: 25) do
# ... retrieve attrs from the Stripe event here...
rescue Stripe::APIConnectionError, Stripe::APIError
# We're inside an SQS queue block
throw :skip_delete # we'll just have to wait on this event, come back later
rescue Stripe::Error
notify $!
nthj / exceptionable.rb
Created Oct 21, 2015
Fun shorthand for raising exceptions from Rails callbacks
View exceptionable.rb
class StandardError
# Accountant::CannotDestroyFinancialData =
# before_destroy(&Accountant::CannotDestroyFinancialData)
def self.to_proc
e = self; -> { raise e }
nthj / extension.scss
Created Aug 20, 2014
Font Awesome Awesomeness
View extension.scss
// multi-ticket icon
.fa-tickets {
&:after, &:before {
content: "\f145";
&:after {
margin-left: -12px;
opacity: 0.7;
nthj / 01_person.rb
Created May 28, 2014
Mild refactoring of an example from "Rails Does Not Define Your Application Architecture" |
View 01_person.rb
class Person < ActiveRecord::Base
nthj / post-checkout
Created May 23, 2014
Automatically create a new database for each of your development branches
View post-checkout
set -e
printf '\npost-checkout hook\n\n'
if [[ $3 == 1 ]]; then
echo "Creating `git name-rev --name-only $2`"
createdb "ticketbud_`git name-rev --name-only $2`_development"
# This file is used by Rack-based servers to start the application.
$stdout.sync = true
require 'unicorn/worker_killer'
## Maximum Requests
nthj / association_class_name_guesser.rb
Last active Aug 29, 2015
I feel evil. [Guess namespaced associations automatically.]
View association_class_name_guesser.rb
class ActiveRecord::Reflection::AssociationReflection
class AmbiguousAssociationClassError < Exception
def initialize results
super "Could not guess association, please specify using class_name. Likely options: #{results.join(', ')}"
def klass
rescue NameError
You can’t perform that action at this time.