Skip to content

Instantly share code, notes, and snippets.

View longlostnick's full-sized avatar

Nick Larson longlostnick

View GitHub Profile
@longlostnick
longlostnick / date_time_validator.rb
Created January 18, 2012 03:50
Validates DateTime values in a Rails app
# this sucker takes care of validating datetime fields before rails gets there and
# messes everything up. it should preserve the local time zone from the user input,
# and check for nil. takes date strings of the format m/d/yyyy m:h (am/pm)
# this goes in the model
validates_with DateTimeValidator, :fields => [:add_date]
# this goes in some place like lib/date_time_validator.rb
class DateTimeValidator < ActiveModel::Validator
DATETIME_FORMAT = "%m/%d/%Y %I:%M %P"
@longlostnick
longlostnick / Dockerfile
Created August 6, 2017 21:51 — forked from yefim/Dockerrun.aws.json
Build a Docker image, push it to AWS EC2 Container Registry, then deploy it to AWS Elastic Beanstalk
# Example Dockerfile
FROM hello-world
@longlostnick
longlostnick / uploads_controller.rb
Created June 17, 2014 18:20
Rails JSON file upload with carrierwave (from base64 string)
class Api::UploadsController < ApiController
def create
@upload = Upload.new(upload_params)
ensure
clean_tempfile
end
private
@longlostnick
longlostnick / gist:1251339
Created September 29, 2011 17:24
Delete all empty/false elements from hash recursively
# (v.respond_to?(:empty?) ? v.empty? : !v) is basically rails' .blank? in plain ruby
class Hash
def delete_blank
delete_if do |k, v|
(v.respond_to?(:empty?) ? v.empty? : !v) or v.instance_of?(Hash) && v.delete_blank.empty?
end
end
end
@longlostnick
longlostnick / cuid.sql
Created October 26, 2022 14:48 — forked from srfrog/cuid.sql
CUIDs for PL/PgSQL
-- Collision-resistant ids optimized for horizontal scaling and performance, for PL/PgSQL.
-- Based on https://github.com/ericelliott/cuid
-- Version 1.0.0
-- Usage: SELECT cuid();
-- BEGIN CONFIG ---
-- Put a unique host ID (int) here per server instance.
-- Once set, this value should not be changed.
@longlostnick
longlostnick / .vimrc
Last active December 20, 2021 23:19
My barebones .vimrc
"-----------------------------------------------------------------------------
" General Stuff
"-----------------------------------------------------------------------------
set nohls
set incsearch
" set the search scan so that it ignores case when the search is all lower
" case but recognizes uppercase if it's specified
set ignorecase
@longlostnick
longlostnick / README.md
Last active August 23, 2021 07:52
Read, transform, and write a csv file
this.editor = new wysihtml5.Editor("textarea");
this.editor.observe("load", function () {
var $iframe = $(this.composer.iframe);
var $body = $(this.composer.element);
$body
.css({
'min-height': 0,
'line-height': '20px',
'overflow': 'hidden',
blockquote {
background: #f7f7f7;
font-family: Georgia;
padding: 14px 14px 12px 12px !important;
border: 1px solid #e7e7e7;
margin: 1em 3em;
text-align: right;
font-size: 10pt;
-webkit-border-radius: 5px;
@longlostnick
longlostnick / git_author_stats.sh
Created January 11, 2019 04:57
Git stats by author
git log --shortstat --pretty="%cE" | sed 's/\(.*\)@.*/\1/' | grep -v "^$" | awk 'BEGIN { line=""; } !/^ / { if (line=="" || !match(line, $0)) {line = $0 "," line }} /^ / { print line " # " $0; line=""}' | sort | sed -E 's/# //;s/ files? changed,//;s/([0-9]+) ([0-9]+ deletion)/\1 0 insertions\(+\), \2/;s/\(\+\)$/\(\+\), 0 deletions\(-\)/;s/insertions?\(\+\), //;s/ deletions?\(-\)//' | awk 'BEGIN {name=""; files=0; insertions=0; deletions=0;} {if ($1 != name && name != "") { print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net"; files=0; insertions=0; deletions=0; name=$1; } name=$1; files+=$2; insertions+=$3; deletions+=$4} END {print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net";}'