Skip to content

Instantly share code, notes, and snippets.

function! RubyTrim(string)
return substitute(a:string, "^\\s\\+\\|\\s\\+$", "", "g")
endfunction
function! RubySplitOneliner()
let lineno = line(".")
let line = getline(lineno)
let leftBrace = stridx(line, "{")
let rightBrace = strridx(line, "}")
let indent = matchstr(line, "^\\s\\+")
@jferris
jferris / allow_rescue_workaround.rb
Created November 16, 2010 16:35
Fixes allow rescue on Rails 3
# Also make sure that config.action_dispatch.show_exceptions is set to true in config/environments/test.rb
class AllowRescue
def initialize(app)
@app = app
end
def call(env)
env['action_dispatch.show_exceptions'] = !!ActionController::Base.allow_rescue
@app.call(env)
@jferris
jferris / gist:726204
Created December 2, 2010 22:22 — forked from dball/gist:726192
Factory.define(:estimate_with_items) do |f|
# ...
f.passing(true).ignore
f.after_create do |e|
e.items << Factory(:estimate_item, :estimate => e, :inspection => true, :op => Factory(:inspection_op, :dealer => e.dealer)).tap do |item|
inspection = item.inspections.first
location = inspection.locations.first
outcome = inspection.failures.detect { |outcome| outcome.performed == passing }
location.create_data!(:inspection_failure_id => outcome.id, :created_by => 'factory')
@jferris
jferris / controller.rb
Created December 3, 2010 15:59
semiformal
class UsersController < ApplicationController
def edit
assign_user_form
end
def update
if assign_user_form.commit
redirect_to @user, :flash => "User saved."
else
render 'new'
@jferris
jferris / sync_ctags.vim
Created January 6, 2011 19:28
Automatically updates tmp/tags as files are written
function! UpdateCtags()
if filereadable("tmp/tags") && strpart(expand("%"), 0, len(getcwd())) == expand("%")
execute "silent! !ctags -f tmp/tags -a " . expand("%:p")
endif
endfunction
augroup ctags
autocmd!
autocmd BufWritePost * call UpdateCtags()
augroup END
if defined?(RSpec)
namespace :rcov do
RSpec::Core::RakeTask.new(:rspec_aggregate) do |task|
task.pattern = 'spec/**/*_spec.rb'
task.rspec_opts = "--format progress"
task.rcov = true
task.rcov_opts = "--rails --exclude osx\/objc,spec,gems\/ " +
"--aggregate tmp/coverage.data"
end
bash -c "echo -e 'daemonize yes\npidfile /var/run/redis.pid\nport 6379\ntimeout 300\nloglevel notice\nlogfile stdout\ndatabases 16\nsave 900 1\nsave 300 10\nsave 60 10000\nrdbcompression yes\ndbfilename dump.rdb\ndir /var/redis\nappendonly no\nappendfsync everysec\nvm-enabled no\nvm-swap-file /tmp/redis.swap\nvm-max-memory 0\nvm-page-size 32\nvm-pages 134217728\nvm-max-threads 4\nglueoutputbuf yes\nhash-max-zipmap-entries 64\nhash-max-zipmap-value 512\nactiverehashing yes\n' > /etc/redis/6379.conf
@jferris
jferris / composite_view.js
Created April 29, 2011 15:58
Cleaning up views as you leave them in Backbone
CompositeView = function(options) {
this.children = [];
Backbone.View.apply(this, [options]);
};
_.extend(CompositeView.prototype, Backbone.View.prototype, {
leave: function() {
this.unbind();
this.remove();
_(this.children).invoke("leave");
@jferris
jferris / Results
Created May 26, 2011 13:48
Benchmark some simple factories with factory_girl
Rehearsal ---------------------------------------------------------
Post.new 0.210000 0.010000 0.220000 ( 0.220546)
Factory.build(:post) 0.280000 0.000000 0.280000 ( 0.278631)
Post.new with save! 3.620000 1.180000 4.800000 ( 6.884482)
Factory.create(:post) 3.710000 1.200000 4.910000 ( 6.995768)
----------------------------------------------- total: 10.210000sec
user system total real
Post.new 0.180000 0.000000 0.180000 ( 0.178181)
Factory.build(:post) 0.230000 0.000000 0.230000 ( 0.235513)
Factory.define :gallery do |f|
f.name "Gallery Name"
end
Factory.define :region do |f|
f.name "Telugu"
end