Skip to content

Instantly share code, notes, and snippets.

@torkale
torkale / backup.rake
Created October 3, 2011 11:24
Sygen version of mysql backup using rake
require 'find'
namespace :db do
desc "Backup the database to a file. Options: DIR=base_dir RAILS_ENV=production MAX=7 SKIP_DEV=true"
task :backup => [:environment] do
skip = ENV["SKIP_DEV"] || ""
unless(RAILS_ENV == "development" && skip == "")
base_path = ENV["DIR"] || Dir.home
@torkale
torkale / buffer.coffee.js
Created March 28, 2012 21:11
buffer that gets a method and callback and executes one method at time
window.Buffer =
buffer: []
executing: false
pushToBuffer: (method, callback) ->
Buffer.buffer.push ->
Buffer.executing = true
method (result) ->
callback(result)
Buffer.executeFromBuffer()
Buffer.executeFromBuffer() unless Buffer.executing
@torkale
torkale / download_latest_backup.rb
Created April 15, 2012 07:49
download latest backup from S3
require 'rubygems'
require 'fog'
connection = Fog::Storage.new(:provider => 'AWS')
directory = connection.directories.find{|dir| dir.key == ENV['BUCKET_KEY']}
file = directory.files.last
File.open ENV['BACKUP_FILE'], 'w' do |backup|
backup << file.body
end
@torkale
torkale / user.rb
Created April 24, 2012 06:06
repeating scopes
class User < ActiveRecord::Base
STATUS = { :active => 1, :inactive => 2, :pending => 3, :announce => 4, :curator => 5 }
scope :active, where("status = #{STATUS[:active]}")
scope :inactive, where("status = #{STATUS[:inactive]}")
scope :pending, where("status = #{STATUS[:pending]}")
scope :announce, where("status = #{STATUS[:announce]}")
end
@torkale
torkale / user.rb
Created April 24, 2012 06:18
repeating singleton methods
class User < ActiveRecord::Base
STATUS = { :active => 1, :inactive => 2, :pending => 3, :announce => 4, :curator => 5 }
def self.active; where(:status => STATUS[:active]); end
def self.inactive; where(:status => STATUS[:inactive]); end
def self.pending; where(:status => STATUS[:pending]); end
def self.announce; where(:status => STATUS[:announce]); end
end
@torkale
torkale / user.rb
Created April 24, 2012 06:20
DRY using define
class User < ActiveRecord::Base
STATUS = { :active => 1, :inactive => 2, :pending => 3, :announce => 4, :curator => 5 }
STATUS.each do |k, v|
define_singleton_method(k) do
where(:status => v)
end
end
end
@torkale
torkale / spec_helper.rb
Created May 9, 2012 09:00
VCR in spec helper
require 'vcr'
...
VCR.configure do |c|
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :fakeweb
c.configure_rspec_metadata!
end
RSpec.configure do |config|
...
@torkale
torkale / vcr_spec.rb
Created May 9, 2012 09:04
describe and context with vcr
describe "group", :vcr do
...
end
context "other group", :vcr do
...
end
@torkale
torkale / .gitignore
Created May 9, 2012 09:07
Instruct git to ignore vcr cassettes
spec/cassettes*
@torkale
torkale / Gemfile
Created May 9, 2012 09:23
add vcr and fakeweb to gemfile
group :test do
gem 'vcr'
gem 'fakeweb'
end