Skip to content

Instantly share code, notes, and snippets.

Avatar

Bastien Gysler basgys

View GitHub Profile
View clustering.go
package main
import (
"fmt"
"math"
"sort"
"strings"
"github.com/pbnjay/clustering"
)
View gist:7098697
class UberRelation
include Enumerable
def initialize(scopes)
scopes.kind_of?(Array) or raise ArgumentError.new("Argument scopes is not an array!")
@scopes = scopes
end
def each(&block)
block_given? ? load.each{|scope| block.call(scope)} : load
View soft_deletable.rb
module SoftDeletable
extend ActiveSupport::Concern
def soft_delete!
find_each do |record|
record.soft_delete!
end
end
included do
@basgys
basgys / aggregate_field.rb
Created Sep 27, 2013
This module allows to create a virtual field from multiple fields
View aggregate_field.rb
# = AggregateField
#
# This module allows to create a virtual field from multiple fields
#
# Example:
#
# class DummyClass
# include AggregateField
#
# attr_accessor :first_name, :last_name
@basgys
basgys / gist:5728380
Last active Dec 18, 2015
Wrap mysql2 deadlocks in an ActiveRecord exception
View gist:5728380
# This core extension aims to manage Deadlocks
# with a proper ActiveRecord exception.
#
# It only works for mysql2
#
# Example:
#
# MyModel.transaction do
# begin
# record1 = MyModel.find(:id, lock: true)
@basgys
basgys / simple_paperclip.rb
Created Jun 5, 2013
Use Paperclip without ActiveRecord
View simple_paperclip.rb
# == Paperclip without ActiveRecord
#
# Simple and lightweight object that can use Paperclip
#
#
# Customized part can be extracted in another class which
# would inherit from SimplePaperclip.
#
# class MyClass < SimplePaperclip
# attr_accessor :image_file_name # :<atached_file_name>_file_name
@basgys
basgys / paperclip_background_upload.rb
Created May 1, 2013
Upload images in background. This is a draft
View paperclip_background_upload.rb
# has_attached_file :image, configuration
# background_upload_for :image
module PaperclipBackgroundUpload
extend ActiveSupport::Concern
included do
def background
@background || false
end
View gist:5356952
package ch.hegarc.ig.clientscomptes.configuration;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
@basgys
basgys / gist:4443808
Last active Dec 10, 2015
Reload images on failure every x seconds
View gist:4443808
// Reload images on failure every x seconds
$("img").error(function() {
var path = $(this).data('src');
var img = $(this);
// Put back mock image
$(img).attr('src', 'http://placehold.it/56x56');
// Increment loading counter
var attempt = ($(img).data('attempt') || 0);
@basgys
basgys / gist:4266041
Created Dec 12, 2012
Rails - Render a partial in a different format
View gist:4266041
# Render a partial in a different format
#
# Parameter : String or Array
# Usage :
# <% with_format('html') do %>
# <%= render(partial: 'partial') %>
# <% end %>
def with_format(formats)
original_formats = self.formats
self.formats = formats.kind_of?(Array) ? formats : [formats]
You can’t perform that action at this time.