Skip to content

Instantly share code, notes, and snippets.

@alexmchale
alexmchale / post-commit
Last active August 29, 2015 13:57
A post-commit hook for Git to print (and copy to clipboard on OSX) the likely GitHub URL for the commit
#!/usr/bin/env ruby
=begin
This hook will print the likely GitHub URL for each new commit.
Install this hook with wget:
wget 'https://gist.githubusercontent.com/alexmchale/9915238/raw/post-commit' -O .git/hooks/post-commit; chmod u+x .git/hooks/post-commit
Install this hook with curl:
protect do |user|
if user.system_admin?
scope { all }
can :create
can :read
can :update
can :destroy
elsif user.organization_admin?
scope { where(id: user.organization_id) }
protect do |user|
if user.system_admin?
scope { all }
can :create
can :read
can :update
can :destroy
elsif user.organization_admin?
scope { where(organization_id: user.organization_id) }
@alexmchale
alexmchale / 20140417202724_replace_newlines_in_links.rb
Last active August 29, 2015 14:01
A hot-fix for servers that fail on migration 20140417202724_replace_newlines_in_links.rb
@alexmchale
alexmchale / s4_create_campaign.php
Last active August 29, 2015 14:02
Example for creating a GreenArrow Studio 4 Campaign with the API in PHP
<?php
define('GAS_BASE_URL', 'http://localhost:3000/api');
define('GAS_API_KEY', '1:2768cac8a813c4419f167749e58219bfe4bca2d7');
function greenarrow_studio_create_campaign($params) {
// Gather parameters needed to communicate with GA Studio.
$listID = $params['mailing_list_id'];
$url = GAS_BASE_URL.'/v2/mailing_lists/'.$listID.'/campaigns';
$ch = curl_init($url);
# The following tables will be initialized when the testing framework starts up.
input:
ss_banned_emails:
-
banid: 1
emailaddress: "ace@drh.net"
list: "g"
bandate: 1405079491
-
banid:
require 'active_support/core_ext/kernel/reporting'
require 'active_support/core_ext/module/aliasing'
require 'monitor'
# This is my variation on Tom Meier's monkey-patch to fix a problem in Rails
# I've run into with RSpec. I've tweaked it a bit to use `alias_method_chain`
# instead of just overloading the original method and copying its code.
#
# See the original Gist here:
#
class MigrateOldCreditTrackingData < ActiveRecord::Migration
def up
if ActiveRecord::Base.connection.tables.include? "credit_usage_daily_log"
execute <<-SQL
INSERT INTO s_credit_daily_usages ( date, organization_id, name, num_subscribers, num_users, credits )
SELECT date::date, id, MAX(name), MAX(num_subscribers), MAX(num_users), MAX(credits)
FROM credit_usage_daily_log
WHERE date::date NOT IN (SELECT date FROM s_credit_daily_usages)
GROUP BY id, date::date;
class JustEchoSomethingFun_20140805211326 < GreenArrow::Migration
# The `before_up` method runs the specified command or Ruby block. If a shell
# command returns non-success or if the Ruby command returns false then the
# migration chain will not be run. The `before_up` logic is executed before
# an UP migration is executed, but not before a DOWN.
### before_up "ls /"
### before_up "echo 'Hello, world!'"
### before_up { [ 1, 2, 3 ].include? 2 }
@alexmchale
alexmchale / jobs.rake
Created September 1, 2014 01:26
A rake task used to connect a local copy of an app to a remote Postgres database over SSH
require 'net/ssh/gateway'
namespace :jobs do
task :create_port_tunnel do
# Make sure the use rhas specified where we're connecting.
(username, hostname) = ENV["REMOTE_HOST"].to_s.split("@", 2)
if [ username, hostname ].any? &:blank?
puts "Please specify env var REMOTE_HOST containing a hostname like 'bob@example.com'."
exit 1