Skip to content

Instantly share code, notes, and snippets.

@dv
dv / Capfile
Created April 10, 2014 11:27
Capistrano 3 + Rails 4 + Whenever + DelayedJob + Nginx + Postgresql
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Rails (includes bundler, rails/assets and rails/migrations)
require 'capistrano/rails'
# Whenever
require 'socket'
require 'openssl'
tcp_client = TCPSocket.new "google.com", 443
ssl_client = OpenSSL::SSL::SSLSocket.new tcp_client
ssl_client.hostname = "google.com" # SNI, if not it returns the first SSL cert for this IP instead of hostname
ssl_client.connect
# Work
@dv
dv / hash_diff.rb
Created June 8, 2014 08:33
Returns the difference between two hashes, much like ActiveRecord attribute changes.
def hash_diff(hash_a, hash_b)
hash_changes = {}
(hash_a.keys + hash_b.keys).uniq.each do |key|
if hash_a[key] != hash_b[key]
hash_changes[key] = [hash_a[key], hash_b[key]]
end
end
hash_changes
# services/csv_importer.rb
class CSVImporter
attr_reader :failed_rows, :success_count
def initialize(content, separator, paid_column)
@content = content
@separator = separator
@paid_column = paid_column
@failed_rows = []
@dv
dv / deep_struct.rb
Created August 15, 2014 13:06
Easy Settings in Rails
# lib/deep_struct.rb
# Source: http://andreapavoni.com/blog/2013/4/create-recursive-openstruct-from-a-ruby-hash
require 'ostruct'
module Utils
class DeepStruct < OpenStruct
def initialize(hash=nil)
@table = {}
@hash_table = {}
@dv
dv / spec_helper.rb
Created August 18, 2014 11:44
Easy Timecop in Rspec
config.treat_symbols_as_metadata_keys_with_true_values = true
config.around(:each) do |example|
if example.metadata[:freeze_time]
Timecop.freeze(Time.now)
example.run
Timecop.return
end
@dv
dv / post.rb
Created September 12, 2014 18:43
Votable
# app/models/post
class Post < ActiveRecord::Base
include Votable
end
class Coder < ActiveRecord::Base
after_save :clear_caches
private
def clear_caches
Stats.expire_coder_punten
end
@dv
dv / turbo_turbo.js.coffee
Last active March 2, 2021 04:05
Fix setTimout, setInterval, and global ajax requests when using Turbolinks
# This library fixes common problems with turbolinks
# - Overwrite setTimeout and setInterval to intercept generated ID's
# - Keep track of Ajax requests
#
# When turbolinks' unload event is called, we:
# - Cancel all setTimeouts and setIntervals
# - Abort all still running Ajax requests
$.turboTurbo =
@dv
dv / run.rb
Created January 20, 2015 11:48
Forcing nginx into submission (aka running nginx using dynamically compiled config files)
require 'rubygems'
require 'erb'
# Nginx needs its config files to contain absolute paths, so
# we need to compile them to set the correct paths.
def expand(filename)
full_path = File.expand_path(File.dirname(__FILE__)) # "~/Users/david/poetry/sslqsquire/canary"
File.join(full_path, filename)
end