Skip to content

Instantly share code, notes, and snippets.

View billhorsman's full-sized avatar

Bill Horsman billhorsman

View GitHub Profile
<%= stylesheet_link_tag :global, :cache => (Rails.env == 'development' ? nil : 'global') %>
@billhorsman
billhorsman / descriptive_sql_load_log.rb
Created July 20, 2011 14:35 — forked from JackDanger/descriptive_sql_load_log.rb
Let Rails display file names and line numbers for log activity.
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
protected
# Turn:
# User Load (6.3ms) SELECT * FROM "users"
# Into:
# User Load /app/views/_partial.erb:27 (6.3ms) in `_app_views_partial_erb` SELECT * FROM "users"
@billhorsman
billhorsman / twice.rb
Created June 22, 2012 08:03
Two simple queries instead of one complex (or impossible) one.
# Problem: the query you want to build to bring back the right objects is complicated. When
# you try and eagerly load the objects you need it all breaks and goes to shit. This often
# happens if you are using "group" or "count", for instance. If you don't eagerly load the
# objects you need then you have an n+1 (or worse) problem as you make an additional query
# for each object you use.
# So try doing the query in two parts: 1) get the primary key for each object you need,
# 2) eagerly load all the objects that match.
# Grab the ids. Don't worry about eagerly loading. The important thing is that it doesn't
@billhorsman
billhorsman / circle.yml
Created September 2, 2012 22:35
Circle CI Config for deployment to Heroku with 10 min timeout
deployment:
staging:
branch: master
commands:
- git push [email protected]:yakify-ci.git:
timeout: 600
dependencies:
pre:
$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"')
## 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.
@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
@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 / 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 / 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%")