Skip to content

Instantly share code, notes, and snippets.

View larzconwell's full-sized avatar

Larz Conwell larzconwell

View GitHub Profile
@larzconwell
larzconwell / gist:1796546
Created February 11, 2012 04:58
Interactive shell script prompt
init_num=0
init() {
init_num=$(expr $init_num + 1)
if [[ "$init_num" -le "9" ]]; then
read -p "Uni:00$init_num> " input
elif [[ "$init_num" -ge "10" ]] && [[ "$init_num" -le "99" ]]; then
read -p "Uni:0$init_num> " input
elif [[ "$init_num" -ge "100" ]] && [[ "$init_num" -le "999" ]]; then
read -p "Uni:$init_num> " input
else
@larzconwell
larzconwell / gist:2002893
Created March 8, 2012 19:38
Rails sessions with Redis, the output.
### Gemfile
gem "redis"
gem "redis-rails", "~> 3.2.1.rc"
### app/controllers/sessions_controller.rb
def create
user = User.where(email: params[:email]).first
# 'authenticate' comes from the 'has_secure_password' method in the User model
if user && user.authenticate(params[:password])
session[:user_id] = user.id # Set a session from the id
@larzconwell
larzconwell / Ruby from Time to DateTime
Created March 17, 2012 21:02
Used in my Rails app to create a DateTime object with the user's utc offset
require 'date'
time = Time.new(2012,5,1,10,30)
date = DateTime.parse(time.to_s)
puts date
#=> 2012-03-02T10:30:00-05:00
###I'm trying to get Ajax working for my sign in forms, but I can't get it work. It literally does nothing!
###Here's my controller
def create
session[:user_id] = nil if session[:user_id] # Destroy any current sessions
user = User.where(email: params[:email]).first
respond_to do |format|
if !params[:bot].blank?
# If the bot hidden field has a value, assume it's a bot
# Before role inheritance
class User < ActiveRecord::Base
attr_accessible :fname, :lname, :email, :password, :password_digest
attr_accessible :fname, :lname, :email, :password, :password_digest, :admin, :as => :admin
end
# After role inheritance
class User < ActiveRecord::Base
class User < ActiveRecord::Base
# Original
attr_accessible :name, :email
attr_accessible :name, :email, :location, :as => :location
attr_accessible :name, :email, :location, :admin, :as => :admin
# OR
# Tenderlove style
# Options for ZSH
HISTFIL=~/.zshhist
HISTSIZE=1000
SAVEHIST=1000
setopt autocd
setopt appendhistory
unsetopt beep
bindkey -e # eval "$(sed -n 's/^/bindkey /; s/: / /p' /etc/inputrc)"
zstyle :compinstall filename '/home/larz/.zshrc'
// MemePoster is the fastest way to get Hungry Academy Memes out to the world!
//
// post <image url> - puts an image on hungryacademymemes.com
var http = require('http')
, response
, options
, request;
module.exports = function(robot) {
@larzconwell
larzconwell / gist:2599109
Created May 5, 2012 02:14
Ruby natural language format
require 'date'
class DateTime
def to_human(opts = {})
date = self
today = opts[:today] || DateTime.now
# Difference between `today` and `date`
diff = (date.to_date - today.to_date).to_i
#!/usr/bin/env bash
find . | grep "\[" | while read file; do
oldfile=$(readlink -f "$file")
echo "$oldfile"
newfile=$(echo "$oldfile" | sed 's/[^/A-Za-z0-9_ ]//g')
newfile=$(echo "$newfile" | sed 's/[ *]/_/g')
echo "$newfile"
mv "$oldfile" "$newfile"
done