Skip to content

Instantly share code, notes, and snippets.

Avatar

Larz Conwell larzconwell

View GitHub Profile
@larzconwell
larzconwell / gist:1796546
Created Feb 11, 2012
Interactive shell script prompt
View gist:1796546
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 Mar 8, 2012
Rails sessions with Redis, the output.
View gist:2002893
### 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 Mar 17, 2012
Used in my Rails app to create a DateTime object with the user's utc offset
View Ruby from Time to DateTime
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
View gist:2177951
###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
View Attr_accessible role inheritance
# 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
View Inheriting attr_accessible roles
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
View .zshrc
# 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'
View gist:2591486
// 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
Ruby natural language format
View gist:2599109
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
View gist:2689980
#!/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