Skip to content

Instantly share code, notes, and snippets.

View basgys's full-sized avatar

Bastien Gysler basgys

View GitHub Profile
@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: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:3482915
Created August 26, 2012 19:31
Transfer BLOB to Amazon S3 with Paperclip
# loop on something .each do |my_model|
blob_to_file(my_model.blob_image) do |image_file|
my_model.update_attributes(s3_image: image_file)
end
# end
def blob_to_file(blob)
tfile = Tempfile.new('blob_to_file')
tfile.write(blob.force_encoding("UTF-8"))
yield(tfile) if block_given?
@basgys
basgys / gist:3358471
Created August 15, 2012 10:19
Reload image on failure with exponential backoff
$("img.thumb").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);
attempt++;
@basgys
basgys / gist:3181680
Last active October 7, 2015 14:48
Retryable transaction
class ActiveRecord::Base
# Retryable transaction
# =====================
#
# Author : Bastien Gysler (http://www.bastiengysler.com)
#
# ActiveRecord::Base.retryable_transaction(tries: 1, :when => ActiveRecord::RecordNotUnique) do |on|
# on.transaction do
# # transaction code here
@basgys
basgys / gist:2823166
Created May 29, 2012 07:48
Creates an accessible URL to your local machine via SSH
# config/tunnel.yml
public_host_username: username_ssh
public_host: my_public_domain
public_port: 8000
local_host: 0.0.0.0
local_port: 3000
# lib/tasks/tunnel.rake
namespace :tunnel do
desc "Start a ssh tunnel"
@basgys
basgys / logs.rake
Created May 11, 2012 14:21
Rake task to tail multiple log files
desc "Tail logs"
task :logs => :environment do |t, args|
log_files = ['log/searchd.log', 'log/searchd.query.log', 'log/resque.log', 'log/development.log']
system "tail -f -n 2 #{log_files.map {|f| "#{Rails.root}/#{f}"}.join(' ')} | perl -pe 's/^(==>)(.*)(<==)$/\e[1;33;40m$&\e[0m/g'"
end
@basgys
basgys / gist:2193099
Created March 25, 2012 11:48
Morse to english
map = {
'.-' => 'A',
'-...' => 'B',
'-.-.' => 'C',
'-..' => 'D',
'.' => 'E',
'..-.' => 'F',
'--.' => 'G',
'....' => 'H',
'..' => 'I',
@basgys
basgys / gist:2192992
Created March 25, 2012 11:19
Roman to Arabic number converter
input = "MCMXCIX" # 1999
# Roman to Arabic mapper
r2a = {"I" => 1, "V" => 5, "X" => 10, "L" => 50, "C" => 100, "D" => 500, "M" => 1000}
# Translate input to an array of arabic numbers
numbers = input.split(//).map {|c| r2a[c]}
count = numbers.count-1
# Compute result (more infos @ http://en.wikipedia.org/wiki/Roman_numerals)