Skip to content

Instantly share code, notes, and snippets.

class DimensionsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# I'm not sure about this:
dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
# But this is what you need to know:
width = options[:width]
height = options[:height]
record.errors[attribute] << "Width must be #{width}px" unless dimensions.width == width
record.errors[attribute] << "Height must be #{height}px" unless dimensions.height == height
@zgohr
zgohr / make pull use rebase.sh
Created January 22, 2014 14:34
instruct git so that any pull uses rebase instead than merge
git config --global branch.autosetuprebase always
git config --global pull.rebase preserve
@zgohr
zgohr / views.py
Created December 10, 2013 22:23
Basic django class based JSON view that can handle JSONP callbacks
from django.http import HttpResponse
from django.views import generic
import json
class JSONView(generic.View):
callback_parameter = 'callback'
def get_json_data(self, request, *args, **kwargs):
"""
@zgohr
zgohr / GenerateEditLinks.gs
Created October 29, 2013 19:23
Google Apps script for adding a menu and action to a Google Spreadsheet attached to a Google Form. When used will generate edit links in the first empty column. Provides for a nice GUI for editing form values, especially helpful for forms with drop down selections where editing in the spreadsheet itself is unsatisfactory.
@zgohr
zgohr / pillow.md
Created August 28, 2013 15:50
OSX python pillow with jpeg support

pip install --upgrade --no-install pillow

This downloads and unpacks the library.

vim /my/venv/build/pillow/setup.py

Change JPEG_ROOT = None to JPEG_ROOT = libinclude("/Users/zach.gohr/Developer/Cellar/jpeg/8d") or wherever your libjpeg exists (I installed using brew install libjpeg and found via find / -name 'libjpeg'

If the libinclude function does not exist (exists in the case of PIL, not pillow) paste this into setup.py as well:

@zgohr
zgohr / supervisord.conf
Created May 26, 2013 16:59
Supervisor/Strider conf
[supervisord]
nodaemon=true
[program:mongodb]
user=mongodb
command=mongod -f /etc/mongodb.conf
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
@zgohr
zgohr / Dockerfile
Last active December 17, 2015 17:29
MongoDB/Strider Dockerfile
#
# docker run $USER/strider strider addUser
# -e SERVER_NAME=
# -e DB_URI=
# -e GITHUB_APP_ID=
# -e GITHUB_APP_SECRET=
# -e SMTP_HOST=
# -e SMTP_PORT=
# -e SMTP_USER=
@zgohr
zgohr / kill_docker.sh
Created May 24, 2013 16:12
Remove all non-running docker containers
docker ps -a -q | xargs -L1 docker rm -v
@zgohr
zgohr / pre-commit
Created March 13, 2013 22:50
Pre commit hook to grep through modified files for forbidden words and reject if found. A better hook would grunt your repo with things like jshint and follow a set of static analysis project standards.
#!/bin/bash
# Grep through modified files for forbidden words and reject commit if found
FILE_PATTERN='\.(js)(\..+)?$' # Separate more file types with pipes here
FORBIDDEN=( debugger ) # Separate more forbidden strings with spaces here
for i in "${FORBIDDEN[@]}"
do
git diff --cached --name-only | \
@zgohr
zgohr / gist:4988556
Created February 19, 2013 18:31
Javascript recursion (recursive function) with promises
var getAll = function(url, page, acquired, deferred) {
var self = this;
page = page || 1;
deferred = deferred || $q.defer();
var page_url = [url, '&page=', page].join("");
$http.get(page_url).then(function(results) {
acquired = acquired ? acquired.concat(results.data) : results.data;
if (results.data.length === 100) { // Assuming the max is 100 per page...
return self.getAll(url, ++page, acquired, deferred);
} else {