Skip to content

Instantly share code, notes, and snippets.

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: = #{}"
dnagir /
Last active Dec 17, 2015
PropConnect licenses
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 /
Created Nov 23, 2012
Unobtrusive and testable AJAX in Rails 3

The problem

Initially asked at RORO but I will elaborate with an example.

What we need to do is the following:

  • we list a set of items (let's say posts) and want to show a drop-down menu next to each one (quick example)
  • when the drop-down arrow is clicked the menu is loaded from the server.
View background_worker_starter.rb
class BackgroundWorkerStarter
attr_reader :env
def initialize(env)
@env = env
def start!
write_pid!(pid_file) if pid_file
View credit_card.rb
class CreditCard
include ActiveAttr::Model
attribute :number
attribute :expiry
attribute :cvn
attribute :name
validate :must_be_valid
validates :number, :expiry, :cvn, :name, presence: true
dnagir / examples.rb
Created Jul 20, 2012
Why squeel is great
View examples.rb
# Example 1, not so cool (look at the 'less-than' and OR condition)
# plain AR:
where(status: 'reserved').
where("reservations.status = ? OR reservations.reservation_type = ?", 'active', 'offline').
where(reservations: {reservation_fee_received: false}).
where("reservations.fee_due_at < ?",
# with squeel:
dnagir / faster_helper.rb
Created Jun 28, 2012
Fast specs with Rails
View faster_helper.rb
ENV["RAILS_ENV"] ||= 'test'
cur_dir = File.expand_path(File.dirname(__FILE__) + '/..')
$LOAD_PATH << "#{cur_dir}"
if defined? Rails
# Most likely going with the full env
puts "Running faster_helper with full Rails env..."
# Eagerly load the Rails so that stubbed dependencies still work
View comments_controller.rb
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment =[:comment]) # etc
dnagir / database_cleaner.rb
Created Mar 16, 2012
Using transactional DB cleanup
View database_cleaner.rb
# spec/support/database_cleaner.rb
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with :truncation # Delete leftovers if any
DatabaseCleaner.strategy = :transaction
config.before(:each) do