Skip to content

Instantly share code, notes, and snippets.

create temporary table u (id int primary key, whatever varchar);
\copy u from 'out-file.csv' quote '"' csv
# 20 sec
update the_actual_table set whatever = u.whatever from u where the_actual_table.id = u.id
# 37 minutes
@apeckham
apeckham / x.rb
Created February 1, 2014 04:04
sequel using activerecord's connection details
Sequel.connect ActiveRecord::Base.connection_config.merge(adapter: 'postgres')
@apeckham
apeckham / regexp.rb
Created February 3, 2014 05:53
new lambda syntax regexp
search: " lambda \{ \|([^\|]+)\| "
replace: " ->($1) { "
@apeckham
apeckham / status_matchers.rb
Created February 9, 2014 02:07
rspec status code matchers for better failure messages
# http://webcache.googleusercontent.com/search?q=cache:pM2SE1PFjwgJ:michal.muskala.eu/blog/2013/11/13/rspec-response-code-testing/+&cd=1&hl=en&ct=clnk&gl=us
# unless we're in a non-rack environment
# (which is unlikely when testing status codes, nonetheless possible)
require 'rack/utils'
module StatusMatchers
# We will use this simple class as our matcher engine
class BeStatus
def initialize(status)
@apeckham
apeckham / gist:8911660
Last active August 29, 2015 13:56
regex to replace map { |object| object.something } with map(&:something)
search: .([\w]+) \{ \|(.+)\| \2\.([\w_!\?]+) \}
replace: .$1(&:$3)
or
\{\s*\|(.+)\|\s*\1\.([\w_!?]+)\s*\}
(&:$2)
@apeckham
apeckham / application_controller_spec.rb
Created February 15, 2014 00:53
testing protect_from_forgery
require 'spec_helper'
class MyApplicationController < ApplicationController
def hello
render text: 'hello'
end
end
describe MyApplicationController do
describe '#protect_from_forgery' do
@apeckham
apeckham / 01 ignore columns initializer.rb
Last active August 29, 2015 13:56
find unused columns: ignore one column at a time and see if tests still pass
class ActiveRecord::Base
def self.columns
super.reject do |column|
"#{table_name}.#{column.name}" == ENV['IGNORE_COLUMN']
end
end
end
@apeckham
apeckham / aa.rb
Created February 17, 2014 23:14
activeadmin component
module ActiveAdmin
module Views
class Calendar < ActiveAdmin::Component
def build(page_presenter, collection)
self.content = collection.ai(html: true).html_safe
end
def self.index_name
"calendar"
end
@apeckham
apeckham / .sh
Created March 11, 2014 00:09
run bundle_benchmark in a loop
while :; do bundle_benchmark; done
@apeckham
apeckham / rubymine_navigate_test.rb
Created March 11, 2014 03:28
command-shift-T (toggle between spec and implementation) for both coffeescript and ruby in rubymine
#!/usr/bin/env ruby
#http://pivotallabs.com/swapping-javascript-spec-implementation-rubymine/
file = ARGV[0] || raise
basename = File.basename(file)
basename_to_open = if file.include?('spec')
basename.sub /_spec\.(coffee|rb)/, '.\1'
else
basename.sub /\.(\w+)$/, '_spec.\1'