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 / gist:5861886
Created June 25, 2013 20:04
calculator
def calculator(operator, expression, *exprs)
exprs.each { |n| expression = expression.send(operator, n) }
expression
end
rails_admin do
configure :expertise do
bootstrap_wysihtml5 true
end
edit do
group 'Login' do
field :email
field :password do
visible false
@nthj
nthj / gist:5297916
Last active December 15, 2015 17:39 — forked from shripadk/gist:552554
How to setup Heroku Hostname SSL with GoDaddy SSL Certificate and Zerigo DNS
Heroku recently added an exciting new 'Hostname SSL' option. This option offers the broad compatibility of IP-based SSL, but at 1/5 the price ($20 / month at the time of this writing).
The following tutorial explains how to use Heroku's new 'Hostname SSL' option on your Heroku project. Before we begin, let's list what we're using here:
* Heroku Hostname SSL
* GoDaddy Standard SSL Certificate
* Zerigo DNS
Note: I am not using the Heroku Zerigo DNS add-on, instead I have a separate Zerigo account for my DNS needs. I do this because Zerigo offers 30 hosts on free direct accounts, versus only 10 hosts on the free Heroku add-on.
@nthj
nthj / rpn.rb
Last active December 11, 2015 14:29
Reverse Polish Notation
def rpn expression
stack = []
expression.split(' ').each do |token|
stack << if token =~ /\d/
token
else
[stack.pop, stack.pop].map { |t| t.to_i }.inject do |sum, x|
eval "#{x} #{token} #{sum}"
end
@nthj
nthj / calculator_dsl.rb
Created November 17, 2015 17:11
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...
# ASSUMPTIONS
# 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
end
@nthj
nthj / exceptionable.rb
Created October 21, 2015 01:49
Fun shorthand for raising exceptions from Rails callbacks
class StandardError
# Accountant::CannotDestroyFinancialData = Class.new(Exception)
# before_destroy(&Accountant::CannotDestroyFinancialData)
def self.to_proc
e = self; -> { raise e }
end
end
@nthj
nthj / cache.rb
Created October 25, 2012 20:30
Nuke your cache on deployment in Rails 3
# Throw this into config/initializers/cache.rb
if ARGV.any? { |a| %w(s server thin).include?(a) }
puts "=> Initializing environment, clearing cache"
Rails.cache.clear
end
@nthj
nthj / paperclip.rake
Created October 2, 2012 18:14
How to fix lost Paperclip asset pictures, because asset_file_name wasn't successfully saved
namespace :paperclip do
# Fix broken timestamps
task :fix => :environment do
# All user picture files in the production environment (directories[1])
files = User.last.picture.send(:connection).directories[Rails.env.staging? ? 2 : 1].files.select do |file|
file.key.starts_with?('users/pictures')
end.sort_by(&:last_modified)
(0..300).each do |ranged|
puts "Ranged: #{ranged}"
@nthj
nthj / Gemfile
Created September 25, 2012 03:33
Quickly dump your latest Heroku backup into Rackspace Cloud Files via rake db:backup
gem 'heroku'
@nthj
nthj / rails
Created September 14, 2012 20:58
Automatically kill zombie Thin servers when booting up a rails server
#!/usr/bin/env ruby
# script/rails
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
# Automatically kill zombie Thin servers when booting up a rails server
if ENV['RACK_ENV'] == 'development' && pid = `ps -ef | grep thin | grep -v grep | sort -r | sed -n 2p | awk '{print $2}'`
puts "=> Killing existing server with pid #{pid}"
`kill -9 #{pid}`
end