Skip to content

Instantly share code, notes, and snippets.

View trevorturk's full-sized avatar

Trevor Turk trevorturk

  • Chicago
View GitHub Profile
#
# Lunar/Moon phases ruby class
#
# Code is based upon Bradley E. Schaefer''s moon phase algorithm.
# Ruby version based on JavaScript Phase Calculator by Stephen R. Schmitt
class Moon
attr_reader :epoch, :phase, :days, :icon, :dist, :ll
# Return the current (or input a date) moon.
# Moon.new
@indirect
indirect / source.rake
Created September 29, 2009 18:47
rake tasks to fix whitespace
def find_and_replace_in_source_files(find, replace)
extensions = %w[.rhtml .rxml .erb .builder .rb .css .js .rake]
files = Dir["**/*"]
files.each do |file_name|
next if (file_name =~ /^vendor/) || !extensions.include?(File.extname(file_name))
text = File.open(file_name, 'r'){ |file| file.read }
changed = text.gsub!(find, replace)
File.open(file_name, 'w'){|file| file.write(changed)} if changed
end
@defunkt
defunkt / connection_fix.rb
Created November 19, 2009 20:00
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@cjolly
cjolly / 12hourTimeSelectRailsHelper.jquery.js
Created April 16, 2010 20:49
jQuery hack for 12 hour datetime selects in rails
function convertDateTimeSelectTo12Hour() {
var translate = {"00":"Midnight","01":"1am","02":"2am","03":"3am","04":"4am","05":"5am","06":"6am","07":"7am","08":"8am","09":"9am","10":"10am","11":"11am","12":"Noon","13":"1pm","14":"2pm","15":"3pm","16":"4pm","17":"5pm","18":"6pm","19":"7pm","20":"8pm","21":"9pm","22":"10pm","23":"11pm"};
$("select[name$='(4i)]'] option").each(function() {
$(this).text(translate[$(this).text()]);
});
}
require 'test_helper'
class UsersTest < ActionController::IntegrationTest
setup do
reset!
@conn = Faraday::Connection.new do |b|
b.adapter :action_dispatch, @integration_session
b.response :rails_json
end
end
# In your test_helper.rb
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
# Resque::Reattempt
#
# The simplest auto-retry on failure (ala DJ) for Resque
#
# Just extend your job with the module and then instead of defining perform,
# define `perform_with_reattempt`:
#
# class MyReattemptJob
# extend Resque::Reattempt
#
Host heroku.com
HostName heroku.com
User git
IdentityFile ~/.ssh/heroku.identity
IdentitiesOnly yes
@barinek
barinek / Fast user switching with Devise
Created August 27, 2010 14:46
Fast user switching with Devise
Ever wondered how to login as another user within Devise?
Recently we had a feature request that would provide Admins with the ability
to sign is as another user. You could imagine live demonstrations or even production
support calls where you would like to be signed in as another user, yet not
have to ask for or decrypt their current password. After stewing for a bit, we found a
pretty nice solution with Devise.
Here's the Cucumber feature...
#!/usr/bin/env ruby
%w{rubygems hmac-sha1 base64 cgi net/https uri openssl}.each{ |f| require f }
KEY = '<YOUR KEY>';
SECRET = '<YOUR SECRET>';
# encodes strings that make twitter oauth happy
def encode( string )
URI.escape( string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") ).gsub('*', '%2A')
end