Skip to content

Instantly share code, notes, and snippets.

View cblavier's full-sized avatar

Christian Blavier cblavier

View GitHub Profile
@cblavier
cblavier / within_box_events.rb
Last active August 29, 2015 13:56
Clustered geoloc search
def within_box_events
# reading lower_left corner and upper_right corner parameters
ll_lng = Float(params[:lower_left][:lng])
ll_lat = Float(params[:lower_left][:lat])
ur_lng = Float(params[:upper_right][:lng])
ur_lat = Float(params[:upper_right][:lat])
# getting coordinates of a 9 times broader box, to get better clustering behavior at edges
q_ll_lng = [ll_lng - ((ur_lng - ll_lng) / 2), -180].max
q_ur_lng = [ur_lng + ((ur_lng - ll_lng) / 2), 180].min
@cblavier
cblavier / event.rb
Created November 21, 2010 19:52
Unique token generation
class Event < ActiveRecord::Base
def self.new_with_token params = {}
params[:token] = generate_token
while (Event.find_by_token(params[:token]))
params[:token] = generate_token
end
Event.new(params)
end
@cblavier
cblavier / gist:992156
Created May 25, 2011 22:37
How can i sort an array that includes similar items by number of those similar items ?
array = ["a", "b", "b", "a", "c", "b"]
occurences = array.inject(Hash.new(0)) { |h,k| h[k] += 1; h }
array.sort_by { |k| occurences[k] }
@cblavier
cblavier / _feedback_tab.html.haml
Created August 20, 2011 14:53
GetSatisfaction feedback tab
%script{:charset => "utf-8", :type => "text/javascript"}
var is_ssl = ("https:" == document.location.protocol);
var asset_host = is_ssl ? "https://s3.amazonaws.com/getsatisfaction.com/" : "http://s3.amazonaws.com/getsatisfaction.com/";
document.write(unescape("%3Cscript src='http://getsatisfaction.com/javascripts/feedback-v2.js' type='text/javascript'%3E%3C/script%3E"));
%script{:charset => "utf-8", :type => "text/javascript"}
var feedback_widget_options = {};
feedback_widget_options.display = "overlay";
feedback_widget_options.company = "sharypic";
feedback_widget_options.placement = "right";
@cblavier
cblavier / _feedback_tab.html.haml
Created August 20, 2011 14:52
GetSatisfaction feedback tab
%script{:charset => "utf-8", :type => "text/javascript"}
var is_ssl = ("https:" == document.location.protocol);
var asset_host = is_ssl ? "https://s3.amazonaws.com/getsatisfaction.com/" : "http://s3.amazonaws.com/getsatisfaction.com/";
document.write(unescape("%3Cscript src='http://getsatisfaction.com/javascripts/feedback-v2.js' type='text/javascript'%3E%3C/script%3E"));
%script{:charset => "utf-8", :type => "text/javascript"}
var feedback_widget_options = {};
feedback_widget_options.display = "overlay";
feedback_widget_options.company = "sharypic";
@cblavier
cblavier / scripts.coffee
Created April 3, 2015 12:28
Coffee + CJSX compilation with Gulp / Browserify / Watchify
gulp = require('gulp')
uglify = require('gulp-uglify')
gulpif = require('gulp-if')
reload = require('./watch').reload
source = require('vinyl-source-stream')
streamify = require('gulp-streamify')
bundle_dir = "./public/"
vendor_dir = "./node_modules/"
src_dir = "./assets/javascripts/"
@cblavier
cblavier / gist:ce26998f8f48a09d12fa
Created January 14, 2016 17:45
EXPLAIN (ANALYZE, BUFFERS) SELECT DISTINCT locations.id FROM pois, locations WHERE pois.poi_kind_id = 3 AND ST_DWithin(pois.coordinates, locations.coordinates, 500, FALSE);
Unique (cost=2407390.71..2407390.72 rows=2 width=4) (actual time=3338.080..3338.252 rows=918 loops=1)
Buffers: shared hit=559
-> Sort (cost=2407390.71..2407390.72 rows=2 width=4) (actual time=3338.079..3338.145 rows=963 loops=1)
Sort Key: locations.id
Sort Method: quicksort Memory: 70kB
Buffers: shared hit=559
-> Nested Loop (cost=0.00..2407390.71 rows=2 width=4) (actual time=2.466..3337.835 rows=963 loops=1)
Join Filter: (((pois.coordinates)::geography && _st_expand((locations.coordinates)::geography, 500::double precision)) AND ((locations.coordinates)::geography && _st_expand((pois.coordinates)::geography, 500::double precision)) AND _st_dwithin((pois.coordinates)::geography, (locations.coordinates)::geography, 500::double precision, false))
Rows Removed by Join Filter: 4531356
Buffers: shared hit=559
@cblavier
cblavier / time_helper.ex
Created March 5, 2015 17:55
TimeHelper
defmodule TimeHelper do
def wait_until(fun), do: wait_until(500, fun)
def wait_until(0, fun), do: fun.()
def wait_until(timeout, fun) defo
try do
fun.()
rescue
@cblavier
cblavier / mongoid.rb
Created January 25, 2013 09:57
Get all mongoid Models in your project. Optionally filter them by superclass.
module Jobbr
module Mongoid
include ::Mongoid
extend self
# Return all Mongoid models.
# You can also pass a parent class to get all Mongoid childrens
@cblavier
cblavier / embedded_findable.rb
Last active April 8, 2017 14:30
Helps to override find method for a Mongoid embedded document.
module Mongoid
# Helps to override find method in an embedded document.
# Usage :
# - add to your model "include Mongoid::EmbeddedFindable"
# - override find method with:
# def self.find(id)
# find_through(Book, 'chapter', id)
# end
module EmbeddedFindable