Skip to content

Instantly share code, notes, and snippets.

View trevorturk's full-sized avatar

Trevor Turk trevorturk

  • Chicago
View GitHub Profile
@replaid
replaid / fixture_validation_test.rb
Created August 5, 2014 21:22
Validating your Fixtures in Rails 4
# Adapted for Rails 4 from http://trevorturk.com/2008/09/20/validating-your-fixtures/
# I put this in test/models/. Trevor put his in test/integration/.
require 'test_helper'
class FixtureValidationTest < ActiveSupport::TestCase
test "fixtures should be valid" do
models = ActiveRecord::FixtureSet.all_loaded_fixtures.keys
models.each do |model|
@javan
javan / string-to-proc.md
Created August 7, 2014 22:05
Ruby String#to_proc
class String
  def to_proc
    Proc.new { |n| self.split(".").inject(n) { |n, method| n.send(method) } }
  end
end
&gt;&gt; Person.first(2).map(&amp;"name.downcase.reverse.capitalize")
@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
@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
# 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
(($) ->
regex = /http:\/\/(.*youtube\.com\/watch.*|.*\.youtube\.com\/v\/.*|youtu\.be\/.*|.*\.youtube\.com\/user\/.*#.*|.*\.youtube\.com\/.*#.*\/.*|.*justin\.tv\/.*|.*justin\.tv\/.*\/b\/.*|www\.ustream\.tv\/recorded\/.*|www\.ustream\.tv\/channel\/.*|www\.ustream\.tv\/.*|qik\.com\/video\/.*|qik\.com\/.*|.*revision3\.com\/.*|.*\.dailymotion\.com\/video\/.*|.*\.dailymotion\.com\/.*\/video\/.*|www\.collegehumor\.com\/video:.*|.*twitvid\.com\/.*|www\.break\.com\/.*\/.*|vids\.myspace\.com\/index\.cfm\?fuseaction=vids\.individual&videoid.*|www\.myspace\.com\/index\.cfm\?fuseaction=.*&videoid.*|www\.metacafe\.com\/watch\/.*|blip\.tv\/file\/.*|.*\.blip\.tv\/file\/.*|video\.google\.com\/videoplay\?.*|.*revver\.com\/video\/.*|video\.yahoo\.com\/watch\/.*\/.*|video\.yahoo\.com\/network\/.*|.*viddler\.com\/explore\/.*\/videos\/.*|liveleak\.com\/view\?.*|www\.liveleak\.com\/view\?.*|animoto\.com\/play\/.*|dotsub\.com\/view\/.*|www\.overstream\.net\/view\.php\?oid=.*|www\.livestream\.com\/.*|www\.worldstarhiphop\.com\/vide