Skip to content

Instantly share code, notes, and snippets.

View tedgrubb's full-sized avatar

Ted Grubb tedgrubb

View GitHub Profile
@tedgrubb
tedgrubb / ttransparency.rb
Last active May 4, 2017 17:33
Paperclip Processor that finds the background color and replaces it with a transparent background. Also simulates anti-aliasing using '-fuzz' to get rid of pixelated artifacts.
# lib/paperclip_processors/transparency.rb
module Paperclip
class Transparency < Thumbnail
# Find the background and replace with transparency.
# -fuzz 20% simulates antialiasing
CONVERT_OPTIONS = [
'-alpha', 'set',
'-fill', 'white',
'-draw', "'color 0,0 replace'",
@tedgrubb
tedgrubb / concept.scss
Last active August 29, 2015 14:05
SCSS Property Concept
@mixin base()
.selector {
@include base(
positioning: {
position: absolute;
},
box-model: {
width: 100%;
},
@tedgrubb
tedgrubb / property_groups.scss
Last active August 29, 2015 14:05
SASS Property Groups
$property-groups: positioning, box-model, color, text;
@each $property-group in $property-groups {
@mixin $property-group {
@content;
}
}
.selector {
@include positioning {
@tedgrubb
tedgrubb / git-assign.sh
Created May 22, 2014 21:01
Find authors to assign a pull request based on the files you've changed
#!/bin/bash
# Setup:
# Add this file to your $PATH
# Usage:
# git assign
changed_files=$(git diff --name-only HEAD master)
@tedgrubb
tedgrubb / git-assign
Last active August 29, 2015 14:01
Bash script to recommend a code reviewer based on the files you changed.
function git-assign() {
changed_files=$(git diff --name-only HEAD master)
authors=$(echo "$changed_files" | xargs -L1 git log --follow | grep Author:)
printf "Authors in order of commits made to these files:\n\n"
printf "$changed_files\n\n"
printf "$authors" | sort | uniq -c | sort -r | sed 's/Author:/Commits:/g'
}
@tedgrubb
tedgrubb / active_resource.rb
Created July 16, 2013 17:29
ActiveResource PATCH with ActiveModel::Dirty. Redefine 'update' to use PATCH instead of PUT. To have a true PATCH we only want to send the changed attributes. We tap into ActiveModel::Dirty's use of 'attribute_will_change!' method before saving the object. This writes changes to the 'changes' hash in our update method.
class ActiveResource::Base
include ActiveModel::Dirty
def update_attribute(name, value)
self.send :"#{name}_will_change!"
self.send("#{name}=".to_sym, value)
self.save
end
def update_attributes(attributes)
@tedgrubb
tedgrubb / log_subscriber.rb
Created July 16, 2013 17:18
ActiveResource LogSubscriber exception logging instead of breaking on nil result. Fixes undefined method 'code' for NilClass when ActiveResource throws a TimeoutError.
module ActiveResource
class LogSubscriber < ActiveSupport::LogSubscriber
def request(event)
result = event.payload[:result]
info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}"
if event.payload.has_key?(:exception)
error "#{event.payload[:exception].join(' ')}\n"
else
info "STATUS %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration]
info "BODY #{result.body}\n"
class ActiveResource::Base
attr_accessor :dirty_attributes
def update_attribute(name, value)
dirty_attributes ||= []
dirty_attributes << name.to_sym
super(*args)
end
@tedgrubb
tedgrubb / show_exceptions.rb
Created June 26, 2013 19:24
Render exceptions via ErrorsController
require 'action_dispatch/middleware/show_exceptions'
module ActionDispatch
class ShowExceptions
private
def render_exception_with_template(env, exception)
body = ErrorsController.action(ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.name]).call(env)
body[1]['Content-Type'] = "application/json; charset=utf-8"
body
rescue => e
@tedgrubb
tedgrubb / grid_overlay.js
Last active December 18, 2015 10:29
Self contained 16 column grid overlay. Toggle the overlay by pressing the 'G' key.
$(function() {
$('body').prepend('<div id="grid-overlay" style="display:none;width:100%;height:100%;position:fixed;z-index:99999999;margin-left:-20px;"></div>');
$(new Array(30)).each(function() {
$('#grid-overlay').append('<div style="float:left;width:60px;height:100%;margin-left:20px;background:rgba(255,153,153,0.2);"></div>');
});
keydown_show_grid = function(event) {
if(event.which == 71) {
$('#grid-overlay').toggle();
}
};