Skip to content

Instantly share code, notes, and snippets.

View troelskn's full-sized avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
@troelskn
troelskn / raven.rb
Last active November 21, 2018 09:06
config/initializers/raven.rb
require 'raven'
require 'rake/task'
if Rails.env.production?
Raven.configure do |config|
config.dsn = 'http://xxx:xxx@app.getsentry.com/xxxxx'
end
module Rake
class Task
@troelskn
troelskn / identity_cache_middleware.rb
Created July 3, 2014 11:43
IdentityCacheMiddleware for ThinkingSphinx
require 'thinking_sphinx/middlewares'
module ThinkingSphinx
module Middlewares
class IdentityCacheMiddleware < ActiveRecordTranslator
def call(contexts)
contexts.each do |context|
Inner.new(context).call
end
@troelskn
troelskn / gist:fca41034ab3ebed4f507
Last active August 29, 2015 14:03
identity_cache benchmark
I benchmarked identity_cache with memcached vs. default active_record against mysql.
Setup is running on AWS, same availability zone. I used RDS (Multi AZ setup) for MySql and ElastiCache for memcached. The test server is on EC2. Instance types are m3.large for MySql, m3.xlarge for EC2 and r3.large for memcached.
The database is a copy of our production db. The products table holds about 1.4M entries.
The cache has been pre-heated with all 1.4M entries prior to the test.
During the test, I ran 10000 fetches:
@troelskn
troelskn / gist:10102663
Created April 8, 2014 08:52
jquery - find closest data attribute.
var listId = $(this).parents().filter(function() { return $(this).data("list-id"); }).eq(0).data("list-id");
@troelskn
troelskn / install_sphinxsearch_2.1.4.sh
Created March 27, 2014 11:57
Installing sphinxsearch 2.1.4 on Ubuntu 13.10
# Install dependencies manually
apt-get install odbcinst1debian2 libodbc1 unixodbc libpq5
# Fetch deb package
mkdir -p /usr/local/build ; cd /usr/local/build && [-f sphinxsearch_2.1.4-release-0ubuntu11~precise_amd64.deb ] || wget http://sphinxsearch.com/files/sphinxsearch_2.1.4-release-0ubuntu11~precise_amd64.deb
# Install package
dpkg --install /usr/local/build/sphinxsearch_2.1.4-release-0ubuntu11~precise_amd64.deb
@troelskn
troelskn / application_controller.rb
Created March 26, 2014 09:56
Rails preferred_layout
class ApplicationController < ActionController::Base
layout :conditionally_select_layout
# Like `layout`, but allows conditional overriding in app controller
def self.preferred_layout(value = nil)
@preferred_layout = value unless value.nil?
@preferred_layout
end
private
# Requires ImageMagick
# Converting the source from JPEG to PNG - if necessary
convert my_src_image.jpg my_src_image.png
# Option A
# - Requires a temporary intermediate file
# - Drill more than 10 might result in poor results
@troelskn
troelskn / application.coffee
Created December 14, 2013 20:43
Make dashing mobile friendly
# dashing.js is located in the dashing framework
# It includes jquery & batman for you.
#= require dashing.js
#= require_directory .
#= require_tree ../../widgets
console.log("Yeah! The dashboard has started!")
Dashing.on 'ready', ->
@troelskn
troelskn / link_user.html.erb
Last active November 13, 2017 11:06
Extend http://railscasts.com/episodes/235-devise-and-omniauth-revised to support scenario where a user first creates account without Facebook login, then later authenticates with Facebook.
@troelskn
troelskn / levenshtein.rb
Last active December 17, 2015 21:48 — forked from sunlightlabs/levenshtein.rb
Fixed to build on mac + 64 bit
# C implementation of Levenshtein text distance algorithm, uses RubyInline to call from within Ruby
# Wildly faster than the Text gem's Text::Levenshtein
# Example:
# l = Levenshtein.new
# l.distance 'hello', ' hello'
# => 1
# Taken from http://www.benallfree.com/2008/10/05/finding-duplicate-text-with-ruby/