Skip to content

Instantly share code, notes, and snippets.

View basgys's full-sized avatar

Bastien Gysler basgys

View GitHub Profile
@basgys
basgys / gist:3543695
Created August 30, 2012 22:47
Newsletter publication with emailvision4rails
StandardNewsletter.daily.publish
@basgys
basgys / gist:3543670
Created August 30, 2012 22:46
Newsletter generation with emailvision4rails
$ rails generate newsletter standard daily weekly
create app/newsletters/standard_newsletter.rb
create app/views/standard_newsletter/daily.html.emv
create app/views/standard_newsletter/daily.text.emv
create app/views/standard_newsletter/weekly.html.emv
create app/views/standard_newsletter/weekly.text.emv
@basgys
basgys / gist:4266041
Created December 12, 2012 08:20
Rails - Render a partial in a different format
# 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]
@basgys
basgys / gist:4443808
Last active December 10, 2015 13:48
Reload images on failure every x seconds
// 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:5356952
Created April 10, 2013 18:02
BundleReader
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 / paperclip_background_upload.rb
Created May 1, 2013 07:30
Upload images in background. This is a draft
# has_attached_file :image, configuration
# background_upload_for :image
module PaperclipBackgroundUpload
extend ActiveSupport::Concern
included do
def background
@background || false
end
@basgys
basgys / gist:5728380
Last active December 18, 2015 04:49
Wrap mysql2 deadlocks in an ActiveRecord exception
# 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 / aggregate_field.rb
Created September 27, 2013 14:23
This module allows to create a virtual field from multiple fields
# = AggregateField
#
# This module allows to create a virtual field from multiple fields
#
# Example:
#
# class DummyClass
# include AggregateField
#
# attr_accessor :first_name, :last_name
module SoftDeletable
extend ActiveSupport::Concern
def soft_delete!
find_each do |record|
record.soft_delete!
end
end
included do
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