Skip to content

Instantly share code, notes, and snippets.

dnagir / rspec-syntax-cheat-sheet.rb
Created Nov 5, 2010
RSpec 2 syntax cheat sheet by example
View rspec-syntax-cheat-sheet.rb
# RSpec 2.0 syntax Cheet Sheet by
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
dnagir / application_helper.rb
Created Aug 18, 2015
Render a different variant in rails
View application_helper.rb
module ApplicationHelper
def with_variant(new_variant, &block)
old_variants = lookup_context.variants
lookup_context.variants = [new_variant]
lookup_context.variants = old_variants
dnagir / gist:1573414
Created Jan 7, 2012 — forked from freshtonic/gist:1390291
Run Postgres Specs in Ramdisk
View gist:1573414
  • This creates a 560mb ramdisk. Adjust the size accordingly. I think the number at the end of the command is the number of disk blocks. They are 2kb in size for me.
  • Restarting postgres is not necessary; you can create the ramdisk and tablespace while postgres is running.
  • You will lose all data in the ramdisk tablespace when you shut your machine down

  $ diskutil erasevolume HFS+ "postgres_ramdisk" `hdiutil attach -nomount ram://1165430`
  Started erase on disk1
  Unmounting disk
  Initialized /dev/rdisk1 as a 569 MB HFS Plus volume
dnagir / cloudinary_upload.rb
Created Apr 24, 2013
Uploading cloudinary files using DelayedJob without a shared file system. It stores the binary in the database along with DJ and will have some impact on that. So the DJ table needs to be updated to accomodate that (such as making it `UNLOGGED` etc)
View cloudinary_upload.rb
module Jobs
class CloudinaryUpload <, :model, :field, :file_name, :content_type, :data)
include Logging
def upload
uploader =, field)
store =
io =, original_filename: file_name, content_type: content_type)
dnagir / deploy.rb
Created May 18, 2011
Flexible Rails deployment with Capistrano and Nginx
View deploy.rb
set :domain, ENV["domain"]
set :application, domain
set :user, ENV["user"]
set :destination, ENV["destination"] || domain
set :web_conf, ENV["web_conf"] || ENV["environment"] || 'production'
raise "please set" unless domain
raise "please set user=server_username" unless user
set :port, ENV["port"] || 1234
set :repository, "."
dnagir / timezone.rb
Last active Dec 16, 2019
Rspec time zones sledgehammer
View timezone.rb
# spec/support/timezone.rb
module TimeZoneHelpers
extend ActiveSupport::Concern
def self.randomise_timezone!
offsets = ActiveSupport::TimeZone.all.group_by(&:formatted_offset)
zones = offsets[offsets.keys.sample] # Random offset to better vary the time zone differences = zones.sample # Random zone from the offset (can be just 1st, but let's do random)
puts "Current rand time zone: #{}. Repro: = #{}"

Keybase proof

I hereby claim:

  • I am dnagir on github.
  • I am dnagir ( on keybase.
  • I have a public key ASBRKxPaRBzw3Jtnf172oWrOYt26nFkIkKk1fQBviIFkHwo

To claim this, I am signing this object:

dnagir / async_smtp_delivery_method.rb
Created Feb 10, 2012
Threaded mail delivery in rails
View async_smtp_delivery_method.rb
# lib/async_smtp_delivery_method.rb
require 'mail'
class AsyncSmtpDeliveryMethod
def initialize(settings)
@settings = settings
def deliver!(mail)
View dry_validation_and_struct.rb
require 'dry/validation'
require 'dry/types'
require 'dry/struct'
module Types
include Dry::Types()
class OrderContract < Dry::Validation::Contract
params do
dnagir / example.rb
Last active May 3, 2019
Dry Transaction Inheritance Repro
View example.rb
require 'bundler/inline'
gemfile do
gem 'dry-validation', '=1.0.0.rc1'
gem 'dry-transaction', '=0.13.0'
gem 'minitest', '=5.11.3'
require 'dry-validation'
require 'dry/transaction'