Skip to content

Instantly share code, notes, and snippets.

@dnagir
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 http://ApproachE.com
# 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
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
begin
lookup_context.variants = [new_variant]
return block.call
ensure
lookup_context.variants = old_variants
end
end
@dnagir
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
  Erasing
  Initialized /dev/rdisk1 as a 569 MB HFS Plus volume
@dnagir
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 < Struct.new(:uploader_class, :model, :field, :file_name, :content_type, :data)
include Logging
def upload
uploader = uploader_class.new(model, field)
store = Cloudinary::CarrierWave::Storage.new(uploader)
io = Cloudinary::Blob.new(data, original_filename: file_name, content_type: content_type)
uploader.cache!(io)
store.store!(uploader.file)
@dnagir
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 domain=app.domain.name.com" unless domain
raise "please set user=server_username" unless user
set :port, ENV["port"] || 1234
set :repository, "."
@dnagir
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
Time.zone = zones.sample # Random zone from the offset (can be just 1st, but let's do random)
puts "Current rand time zone: #{Time.zone}. Repro: Time.zone = #{Time.zone.name.inspect}"
end
View keybase.md

Keybase proof

I hereby claim:

  • I am dnagir on github.
  • I am dnagir (https://keybase.io/dnagir) on keybase.
  • I have a public key ASBRKxPaRBzw3Jtnf172oWrOYt26nFkIkKk1fQBviIFkHwo

To claim this, I am signing this object:

@dnagir
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
end
def deliver!(mail)
View dry_validation_and_struct.rb
require 'dry/validation'
require 'dry/types'
require 'dry/struct'
module Types
include Dry::Types()
end
class OrderContract < Dry::Validation::Contract
params do
@dnagir
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'
end
require 'dry-validation'
require 'dry/transaction'