Skip to content

Instantly share code, notes, and snippets.

View billhorsman's full-sized avatar

Bill Horsman billhorsman

View GitHub Profile
@billhorsman
billhorsman / application_controller.rb
Created June 13, 2017 10:43
Handling ActionController::UnknownFormat
class ApplicationController < ActionController::Base
# Rather than return a 500 (internal server error) let's just say we can't
# find what they are looking for. Typically, this is when someone hits one
# of our valid routes but asks for the .xml format or something.
rescue_from ActionController::UnknownFormat, with: :raise_unknown_format
def raise_unknown_format
render plain: 'Unknown Format', status: :not_found
end
@billhorsman
billhorsman / gist:10627860
Last active August 29, 2015 13:59
keybase.md
### Keybase proof
I hereby claim:
* I am billhorsman on github.
* I am billhorsman (https://keybase.io/billhorsman) on keybase.
* I have a public key whose fingerprint is D96D 6222 E6CB 8A78 E03F A1A0 6C5C E69B 98D5 42B5
To claim this, I am signing this object:
@billhorsman
billhorsman / capture.rb
Created April 7, 2014 07:55
Captured Groups in Regex and Ruby
# I didn't know that this test made the captured variable available as local variables.
if /(?<x>\d+)/ =~ "foo 123 bar"
puts x
end
# => 123
@billhorsman
billhorsman / Gemfile
Created April 3, 2014 08:37
Turbolinks and AddThis
# This ensures that jQuery is reinitialized after the page:change event
gem 'jquery-turbolinks'
@billhorsman
billhorsman / user.rb
Created March 5, 2014 15:49
Postgresql Wildcard Search
class User
# This is how I see most wildcard searches done. It matches anywhere, including
# in the middle of a word. E.g. for "John Doe"
# "jo" => yes
# "do" => yes
# "oe" => yes
#
def self.search_anywhere(query)
where("LOWER(users.first_name) LIKE :query OR LOWER(users.last_name) LIKE :query OR LOWER(users.email) LIKE :query", query: "%#{query}.downcase%")
@billhorsman
billhorsman / user.rb
Created January 30, 2014 16:06
Correcting case in a name
class User
attr_accessor :first_name, :last_name
def name
out = [first_name, last_name].join(' ').strip
out =~ /\A([a-z\s'\-]*|[A-Z\s'\-]*)\Z/ ? out.titleize : out
end
end
@billhorsman
billhorsman / foo_test.rb
Last active December 19, 2015 01:19
Using let instance of instance variables in minitest.
# Using instance variables
describe "Foo" do
before do
@foo = FactoryGirl.create :foo
@bar = FactoryGirl.create :bar
end
it "sends message" do
assert @foo.msg("pow!")
# @bar is not used in this test
@billhorsman
billhorsman / code.rake
Last active December 18, 2015 17:29
Use ruby_parser gem to list all ruby files that produce an error. I'm using this to highlight Ruby files that might not be parsed successfully by Code Climate.
namespace :code do
desc "Parse with ruby_parser and list files producing errors"
task :parse do
bad = []
Dir.glob("**/*.rb").each do |filename|
if system("ruby_parse_extract_error #{filename} > /dev/null 2>&1")
print "."
else
bad << filename
## The quick-and-nasty CVE-2013-0156 Heroku inspector!
## Originally brought to you by @elliottkember with changes by @markpundsack @ Heroku
## Download and run using:
## ruby heroku-CVE-2013-0156.rb
apps = `heroku list 2> /dev/null`.split("\n")
apps = apps.map {|app|
case app.strip
when /^===/
# Some "heroku apps" lines have === formatting for grouping. They're not apps.
$form.fileupload
dataType: "json"
done: (e, data) ->
result = if data.result?
# XHR (as used by, say, Chrome) gives you a simple JSON object
data.result
else
# iframe (as used by IE) gives you text back that you'll want to parse manually)
# Urgh. UTF8 value (passed by Rails form) causes JSON to fall over. Crappy solution on next line.
data = JSON.stringify(data).replace(/"name":"utf8","value":"[^"]*"/, '"name":"utf8","value":"REMOVED"')