Skip to content

Instantly share code, notes, and snippets.

View nthj's full-sized avatar

Nathaniel Jones nthj

  • Orlando, FL
View GitHub Profile
@nthj
nthj / association_class_name_guesser.rb
Last active August 29, 2015 13:56
I feel evil. [Guess namespaced associations automatically.]
class ActiveRecord::Reflection::AssociationReflection
class AmbiguousAssociationClassError < Exception
def initialize results
super "Could not guess association, please specify using class_name. Likely options: #{results.join(', ')}"
end
end
def klass
super
rescue NameError
@nthj
nthj / config.ru
Created February 26, 2014 23:22
unicorn
# This file is used by Rack-based servers to start the application.
$stdout.sync = true
require 'unicorn/worker_killer'
##
## Maximum Requests
##
@nthj
nthj / post-checkout
Created May 23, 2014 15:03
Automatically create a new database for each of your development branches
#!/bin/bash
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"
@nthj
nthj / 01_person.rb
Created May 28, 2014 02:30
Mild refactoring of an example from "Rails Does Not Define Your Application Architecture" | http://www.naildrivin5.com/blog/2014/05/27/rails-does-not-define-your-application-architecture.html
class Person < ActiveRecord::Base
end
@nthj
nthj / extension.scss
Created August 20, 2014 17:31
Font Awesome Awesomeness
// multi-ticket icon
.fa-tickets {
&:after, &:before {
content: "\f145";
}
&:after {
margin-left: -12px;
opacity: 0.7;
}
module AccessibleBlock
extend ActiveSupport::Concern
module ClassMethods
@accessible_block = false
def key(*arguments, &block)
super
attr_accessible arguments[0] if @accessible_block
end
@nthj
nthj / application_controller.rb
Created April 1, 2011 19:32
Rails controller before/after shortcuts (ruby 1.9.2 compatible)
class ApplicationController < ActionController::Base
extend Filterable
after_update -> { user.clear_cache! }
end
@nthj
nthj / string.rb
Created April 5, 2011 00:53
Ruby string extensions for easily defining questionable methods like "email?"
class String
def questionable
questionable? ? to_s : "#{to_s}?"
end
def questionable!
replace questionable
end
def questionable?
@nthj
nthj / object.rb
Created April 5, 2011 01:27
Ruby object extension for use with HAML
# Easily add id and class attributes to HAML elements
#
# %li{ account.element.to_hash } ->
# <li class='account' id='account-7'>
#
# %span{ account.element.button.to_hash } ->
# <span class='account-button' id='account-7-button'>
#
# %span{ :class => account.element.save.button.class } ->
# <span class='account-save-button'>
@nthj
nthj / constituent_importer.rb
Created June 6, 2011 03:58
Constituent Importer for Capvoice
# constituent_importer.rb
#
# When the customer uploads an Excel file,
# we generate a unique ID for the file and
# store it on our AWS/S3 jobs bucket.
#
# Then we create a job with that unique ID,
# and our background workers execute the job as follows
class ConstituentImporter < Struct.new(:job_object_id)