Skip to content

Instantly share code, notes, and snippets.

class Coder < ActiveRecord::Base
after_save :clear_caches
private
def clear_caches
Stats.expire_coder_punten
end
@dv
dv / post.rb
Created September 12, 2014 18:43
Votable
# app/models/post
class Post < ActiveRecord::Base
include Votable
end
@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 / 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 = {}
# 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 / 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
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 / 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
Rails.application.assets.register_mime_type 'image/png', '.quant'
Rails.application.assets.register_engine '.quant', PngQuantProcessor
# Important to tell Sprockets this is a binary type, else you'll get UTF-8 byte sequence errors
Rails.application.assets.register_mime_type 'image/png', '.png'
Rails.application.assets.register_postprocessor 'image/png', :png_compressor do |context, data|
IO.popen("pngquant -", "rb+") do |process|
process.write(data)
process.close_write
process.read
end
end