Skip to content

Instantly share code, notes, and snippets.

View buren's full-sized avatar

Jacob Burenstam Linder buren

View GitHub Profile
@buren
buren / gist:6961940
Last active June 1, 2017 07:48
Custom database for rails branch

Custom database for Rails branch

(Below is an example with MySQL)

Customize config/database.yml to handle multiple databases.

<%
  # How to setup a custom database for a branch
  branch = `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '')
  suffix = `git config --bool branch.#{branch}.database`.chomp == 'true' ? "_#{branch}" : "_development"
@buren
buren / zsh-git-prompt
Last active August 29, 2015 13:55
zsh-git-prompt
# Shameless steal from http://askubuntu.com/a/85111
# git branch in zsh-prompt
echo '
autoload -Uz vcs_info
precmd () { vcs_info }
setopt prompt_subst
PS1="\$vcs_info_msg_0_$PS1"' >> ~/.zshrc
@buren
buren / RetryableCall.rb
Created April 16, 2014 09:21
Retry given block until response is not nil or max_retries is reached
class RetryableCall
DEFAULT_OPTIONS = {
sleep: 0.5,
max_retries: 5
}
def self.perform options = {}, &block
new(options).perform(&block)
end

Keybase proof

I hereby claim:

  • I am buren on github.
  • I am buren (https://keybase.io/buren) on keybase.
  • I have a public key whose fingerprint is EBA4 FF7D C0DF C205 02B8 9A24 C85F B75E 410E 4CC4

To claim this, I am signing this object:

@buren
buren / Singelton.java
Created May 7, 2014 19:26
Simple Java Singelton Example
package utils;
import java.net.URL;
public class Enduro {
private static Enduro ourInstance = new Enduro();
public static Enduro getInstance() {
return ourInstance;
}
@buren
buren / response_follow_redirect.rb
Last active August 8, 2016 15:14
Get response given an URL and follow redirects
require 'uri'
require 'net/http'
require 'openssl'
MAX_ATTEMPTS = 2
# Get response given an URL and follow redirects
def self.url_response(uri)
found = false
url = URI.parse(uri)
@buren
buren / gem_console.rb
Created October 5, 2014 08:29
Put this in the Gem's Rakefile.
# https://gist.github.com/buren-trialbee/f51c6d37ea96618bcc49
task :console do
require 'irb'
require 'irb/completion'
require 'my_gem'
ARGV.clear
IRB.start
end
@buren
buren / meta_ruby.rb
Created October 22, 2014 10:11
Simple Ruby meta programming example
class Meta
def self.make_method(meth_name)
define_method meth_name do |adjective|
puts "#{meth_name} is a #{adjective} method name"
end
end
def method_missing(meth, *args, &block)
puts "Method #{meth} is not defined on this class"
self.class.make_method(meth)
@buren
buren / rails_web_console_param.rb
Last active February 13, 2016 18:16
Attach a rails web console to any page by adding ?web_console=
# config/initializers/web_console.rb
WebConsoleBeforeAction = ->(controller) do
controller.console if controller.params[:web_console]
end
ApplicationController.before_action(WebConsoleBeforeAction) if defined?(WebConsole) && Rails.env.development?
# NOTE:
# For security reasons only do this in development.
@buren
buren / rails_strong_parameters_refactor.rb
Last active December 24, 2020 11:59
A simple pattern to refactor permitted params for Rails with StrongParameters included.
# Rails StrongParameters refactor
#
# Inspired by Ryan Bates's Screencast #371
# http://railscasts.com/episodes/371-strong-parameters
#
# A simple pattern to refactor permitted params for Rails with StrongParameters.
# app/models/author.rb
class Author < ActiveRecord::Base
validates_presence_of :name, :birth_date