Skip to content

Instantly share code, notes, and snippets.

@clarkdave
clarkdave / cloudfront-invalidation-grunt.js
Created October 21, 2013 12:18
Create an AWS Cloudfront invalidation in a Grunt task
grunt.registerTask('cloudfront:invalidate', 'invalidate lib on cloudfront', function() {
var finished = this.async(),
aws = require('aws-sdk'),
path = '/libs/ll.sdk-' + sdk_version + '.js'
;
// set aws keys & region from config
aws.config.update({
@clarkdave
clarkdave / pg_interval.rb
Last active July 9, 2019 00:57
Support for PostgreSQL `interval` type in Rails 4. Although ActiveRecord supports `interval` by default, it turns it into a string (and tells Postgres the column is type string, too). This means you don't get any proper interval goodness. By sticking this code into an initialiser, ActiveRecord will create proper `interval` column types in Postgr…
#
# This will force ActiveRecord to create proper `interval` column types in PostgreSQL
#
# def change
# add_column :leases, :period, :interval
# end
#
# This applies to a generated `schema.rb` file too.
#
# No special OID type is applied to an `interval` type. Rails will treat it as a string, although
@clarkdave
clarkdave / json_enhancements.rb
Created July 17, 2013 14:55
(CHEF) Install PostgreSQL json_enhancements & hstore
package 'postgresql-server-dev-9.2'
package 'postgresql-contrib-9.2'
# without hstore this won't build without fiddling, so it's easiest just to install hstore too
bash 'build_json_enhancements' do
code <<-CODE
cd /var/cache/chef
wget https://bitbucket.org/qooleot/json_enhancements/get/master.tar.gz
tar -xvzf master.tar.gz
@clarkdave
clarkdave / knife-ssh-on-vpc-servers.sh
Last active December 19, 2015 19:49
(CHEF) knife ssh on servers in an Amazon VPC
knife ssh 'name:*' 'uptime' -G user@gateway.example.com -a private_ipaddress -x user
# to run chef-client on all servers:
knife ssh 'name:*' 'sudo chef-client' -G user@gateway.example.com -a private_ipaddress -x user
@clarkdave
clarkdave / pg_enum.rb
Last active July 18, 2019 15:37
basic support for PostgreSQL Enums in ActiveRecord - to be placed in an initializer
##
# This provides `create_enum` and `drop_enum` methods for migrations, which creates
# a Postgres ENUM type with the provided values.
#
# It'll also dump these into the schema.rb to be loaded into another DB (e.g. rake db:test:load)
#
# In order to reference the new enums as actual types, ActiveRecord needs to know about them, so
# make sure they are all represented in the `types` array below
#
# Then you can reference them in your migrations, e.g.
@clarkdave
clarkdave / pg_sequence.rb
Last active December 19, 2015 09:39
basic support for PostgreSQL sequences in ActiveRecord migrations - place in an initializer
module PgSequence
module PostgreSQLAdapter
def create_sequence(name)
execute "CREATE SEQUENCE #{name}"
end
def drop_sequence(name)
execute "DROP SEQUENCE #{name}"
end
end
@clarkdave
clarkdave / zombie-post-request.js
Last active August 1, 2019 14:44
How to make arbitrary POST requests using Zombie
var browser = new Browser();
browser.visit('/hello', function() {
// do some checks
// I'm not sure if browser.resources is an official part of the Zombie API (I found it by searching the src)
// so be wary in case it changes
browser.resources.post(
'/authenticate?email=hello@example.com&password=' + auth_token,
@clarkdave
clarkdave / chef-insert-line-if-no-match.rb
Created June 13, 2013 11:12
[CHEF] Insert a line in a file if it doesn't already exist
line = '127.0.0.1 gateway.internal gateway'
file = Chef::Util::FileEdit.new('/etc/hosts')
file.insert_line_if_no_match(/#{line}/, line)
file.write_file
@clarkdave
clarkdave / toggle-popup.coffee
Created May 12, 2013 13:27
Toggle a popup menu and close it when clicking outside (example using Backbone)
toggleUserMenu: (e) ->
e.preventDefault() if e
container = @$('.user')
# remove body listener if present
$('body').off 'click.menu'
$('.menu', container).toggleClass('visible')
container.toggleClass('open')
@clarkdave
clarkdave / log-user-out-rails.coffee
Created May 12, 2013 12:56
Log user out in Rails via JS
$('.logout').on 'click', (e) ->
link = $(e.currentTarget)
csrf_token = $('meta[name="csrf-token"]').attr('content')
form = $('<form>')
form.hide()
form.attr('method', 'post').attr('action', link.attr('href'))
$("<input type='hidden' name='authenticity_token' value='#{csrf_token}'>").appendTo(form)