Skip to content

Instantly share code, notes, and snippets.

View forked_specs.rb
module ForkedSpecs
def forked_specs(*specs)
ActiveRecord::Base.clear_all_connections!
pids = []
specs.each do |spec|
pids << fork do
ActiveRecord::Base.establish_connection
spec.call
end
@nicoolas25
nicoolas25 / Gemfile
Created Oct 29, 2013
Rails 4 + Devise 3 + (google | windows live | facebook) Oauth2
View Gemfile
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
gem 'omniauth-windowslive'
@nicoolas25
nicoolas25 / subdir.rb
Created Jan 16, 2014
Using a subdirectory with mina.
View subdir.rb
# encoding: utf-8
#
# This extension allows you to select a subdirectory instead of a whole repository.
# It is useful when you're fetching a git project and your actual code is in a
# subdirectory.
#
namespace :subdir do
desc "Select a subdirectory as the root of your project"
@nicoolas25
nicoolas25 / action_view_ext.rb
Created Nov 22, 2013
Add the :required_mark option to the labels. (Rails 4)
View action_view_ext.rb
class ActionView::Helpers::FormBuilder
alias :original_label :label
# Add the :required_mark option to the label helper that add a mark before the content.
def label(method, content_or_options = nil, options = nil, &block)
if content_or_options && content_or_options.class == Hash
options = content_or_options
else
content = content_or_options
end
@nicoolas25
nicoolas25 / jquery_ujs_bindings.js
Created Oct 31, 2013
Use Rails's Javascript remote calls on div.
View jquery_ujs_bindings.js
(function($){
$(document).on('rails:attachBindings', function(){
$.rails.buttonClickSelector = $.rails.buttonClickSelector + ', div[data-remote]';
return true;
});
})(jQuery);
@nicoolas25
nicoolas25 / benchmark.rb
Last active Dec 21, 2015
Compare an access speed between an instance variable and an accessor.
View benchmark.rb
require 'benchmark'
n = 5000000
class A
def initialize(attribute)
@attribute = attribute
end
@nicoolas25
nicoolas25 / fineuploader-3.0.patch
Created Dec 4, 2012
Patch to make dragonfly happy with fineuploader 3.0. (for 3.1 see the paramsInRequestBody option)
View fineuploader-3.0.patch
--- fineuploader-3.0.js 2012-11-16 21:30:02.000000000 +0100
+++ fineuploader-3.0.patched.js 2012-12-04 10:21:56.799781578 +0100
@@ -1796,7 +1796,15 @@
var protocol = this._options.demoMode ? "GET" : "POST"
var form = qq.toElement('<form method="' + protocol + '" enctype="multipart/form-data"></form>');
- var queryString = qq.obj2url(params, this._options.endpoint);
+ // Params arn't given through form's action anymore but appened into it.
+ var queryString = qq.obj2url({}, this._options.endpoint);
+ for (paramName in params) {
@nicoolas25
nicoolas25 / user.rb
Created Nov 13, 2012
Allow only one connection before confirmation with Devise.
View user.rb
module OneConnectionBeforeConfirm
extend ActiveSupport::Concern
included do
before_create :generate_confirmation_token
after_create :send_on_create_confirmation_instructions
end
# This actions are not properly trigerred because of the rest of our modifications.
before_create :generate_confirmation_token
@nicoolas25
nicoolas25 / range_mixer.rb
Created Jul 4, 2012
Some class that may be useful for dealing with time ranges.
View range_mixer.rb
# This class assumes that you have ranges associated to a value
# and that you want to merge those values. This is, for instance,
# useful to count overlaps of date ranges.
#
# Example of uses:
#
# rm = RangeMixer.new(0..10, 0)
# rm.add(0..5){ |memo| memo + 1 }
# rm.add(0..5){ |memo| memo + 4 }
#
@nicoolas25
nicoolas25 / loadable_model.rb
Created Jun 28, 2015
Custom relation with caching and preloading (ActiveRecord)
View loadable_model.rb
require "active_support/concern"
require "loadable/relation"
module Loadable
module Model extend ActiveSupport::Concern
module ClassMethods
def loadable(name, options)
loadable_relations[name] = build_relation(name, options)
define_reader(name)