Skip to content

Instantly share code, notes, and snippets.

View gpr's full-sized avatar

Grégory Romé gpr

View GitHub Profile
@gpr
gpr / application.rb
Created February 21, 2014 08:29
Manage Rails application version number with rake tasks (includes git-flow support)
# config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env)
module App
class Application < Rails::Application
@gpr
gpr / gist:5f08a40d7bb3bd80ba78
Created January 12, 2015 10:50
Generate random bytes with Ruby
# Nothing to require
# You can define your seed or generate one using system random function
seed = srand
prng = Random.new(seed)
# Generate X bytes
X = 16
bin_str = prng.bytes(X)
@gpr
gpr / gist:03af2823eab9f9e7d867
Created January 19, 2015 08:48
Desktop memory cleanup task
# crontab -e
0 4 * * * sync; echo 3 > /proc/sys/vm/drop_caches
@gpr
gpr / process_mail_attachments.rb
Created March 3, 2015 11:34
Process email attachments in Rails
# message is Mail::Message
# see http://www.rubydoc.info/github/mikel/mail/Mail/Message
message.attachments.each do |attch|
f = Tempfile.new(attch.filename, Rails.root.join('tmp'))
puts " - Save #{attch.filename} to #{f.path}"
begin
f.binmode
f.write attch.decoded
ensure
class RouteRecognizer
attr_reader :paths
# To use this inside your app, call:
# `RouteRecognizer.new.initial_path_segments`
# This returns an array, e.g.: ['assets','blog','team','faq','users']
INITIAL_SEGMENT_REGEX = %r{^\/([^\/\(:]+)}
def initialize
@gpr
gpr / README.md
Last active August 29, 2015 14:20
Paperclip warning

WARNING

The model must be created before to attach a file.

@gpr
gpr / README.md
Last active August 29, 2015 14:24
Dynamic nested attributes belongs_to with simple_form

README

This gist shows how to create a dynamic subform for nested belongs_to association with simple_form.

Models

We have two models, Product and Component where:

@gpr
gpr / guard
Created July 24, 2015 12:41
Script for launching Guard in RubyMine (to avoid minitest output issue)
#!/usr/bin/env ruby
ENV.delete('RUBYLIB')
ENV.delete('RM_INFO')
exec 'bundle exec guard'
@gpr
gpr / myssl_controller.rb
Created November 4, 2013 08:30
How to get email from X509 certificate used for client authentication. The email could be used for setting the current user in Devise or Omniauth (considering that the user has been authenticated) Common name could be used instead of email by using SSL_CLIENT_S_DN_CN. request.env["SSL_CLIENT_S_DN_Email"] could be used by enabling +StdEnvVars but…
def get_ssl_username
request.env["REMOTE_USER"]
end
@gpr
gpr / csvget
Last active December 27, 2015 15:59
How to get a specific field value with csvkit
#!/bin/bash
#
# csvget -- use csvkit too get a specific field value
#
# Usage: csvget <field_name> <line_number>
# get the field <field_name> value at the <line_number>th line
FieldName=${1:?}
N=${2:?}