Skip to content

Instantly share code, notes, and snippets.

View aliang's full-sized avatar

Alvin Liang aliang

View GitHub Profile
@aliang
aliang / devise_http_rememberable.rb
Created June 13, 2011 07:55 — forked from jamiew/devise_http_rememberable.rb
Monkeypatch Devise to support saving sessions via HTTP auth (e.g. via bookmarklets or browser addons)
# config/initializers/devise_http_rememberable.rb
# Monkeypatch Devise so HTTP authentication sets remember_user_token cookie
# ABR: Always Be Remembering
module Devise
module Strategies
class Authenticatable < Base
def remember_me?
# Devise's implementation of this is:
# valid_params? && Devise::TRUE_VALUES.include?(params_auth_hash[:remember_me])
@aliang
aliang / mymodel.rb
Created June 13, 2011 06:25
render from model in Rails 3
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
# http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
ActionView::Base.new(Rails.configuration.paths.app.views.first).render(
@aliang
aliang / stream_itunes_over_ssh.sh
Created June 12, 2011 08:39
stream iTunes over SSH (requires dns-sd)
# Shamelessly taken from http://blog.iharder.net/2009/09/28/itunes-stream-itunes-over-ssh/
# Your remote library will appear in your local iTunes as a shared library
# You need an ssh server running on the remote library's computer
#!/bin/sh
dns-sd -P "Home iTunes" _daap._tcp local 3689 localhost.local. \
127.0.0.1 "Arbitrary text record" &
PID=$!
ssh -C -N -L 3689:localhost:3689 yourhost.dyndns.org
kill $PID
@aliang
aliang / application.rb
Created June 12, 2011 08:12
monkeypatch Rails::Application.load_console to load app-specific .irbrc
# taken from http://railsbros.de/2010/11/29/a-custom-irbrc-for-rails-3.html
# config/application.rb that is
module MyApp
class Application < Rails::Application
# config stuff comes here
def load_console(sandbox=false)
super
if File.exists?(project_specific_irbrc = File.join(Rails.root, ".irbrc"))
puts "Loading project specific .irbrc ..."
@aliang
aliang / omniauth.rb
Created June 7, 2011 18:02 — forked from dira/omniauth.rb
OmniAuth strategy for a custom provider
module OmniAuth
module Strategies
# tell OmniAuth to load our strategy
autoload :Pixelation, 'lib/pixelation_authorization'
end
end
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, "app_name", "secret"
provider :facebook, "app_name", "secret", :scope => ''
@aliang
aliang / routes.rb
Created June 6, 2011 15:41
Force SSL on routes, version 2, doesn't use Devise
YourApplication::Application.routes.draw do
class SSL
def self.matches?(request)
# This way you don't need SSL for your development server
return true unless Rails.env.production?
request.ssl?
end
end
# Reusable Proc for redirection. Will redirect with 301 (on Rails 3.0, at least)
@aliang
aliang / migrate_s3.rake
Created June 6, 2011 15:22
rake task to migrate paperclip attachments to Amazon S3
namespace :attachments do
task :migrate_to_s3 => :environment do
require 'aws/s3'
# Load credentials
s3_options = YAML.load_file(File.join(Rails.root, 'config/s3.yml')).symbolize_keys
bucket = s3_options[:bucket_name]
# Establish S3 connection
@aliang
aliang / watir.rb
Created May 26, 2011 22:56
watir basics that I always forget
require 'rubygems'
require 'watir'
#start the browser up
Watir::Browser.default = "firefox"
browser = Watir::Browser.start "http://www.weather.com/"
browser.text_field(:id, "whatwhereForm2").set("san diego, ca")
browser.button(:src,"http://i.imwx.com/web/common/searchbutton.gif").click
@aliang
aliang / fakeout.rake
Created April 3, 2011 01:42 — forked from matthutchinson/fakeout.rake
fill your database with crap using faker
# a simple/configurable rake task that generates some random fake data for the app (using faker) at various sizes
# NOTE: requires the faker or ffaker gem
# sudo gem install faker - http://faker.rubyforge.org
# OR
# sudo gem install ffaker - http://github.com/EmmanuelOga/ffaker
require 'faker'
class Fakeout
@aliang
aliang / optimize-legibility.html
Created April 2, 2011 01:53 — forked from kolber/optimize-legibility.html
kerning/ligatures using text-rendering:optimizeLegibility CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cross-browser kerning-pairs & ligatures</title>
<style>
body { font-family: sans-serif; background: #f4f3f3; color: rgba(40, 30, 0, 1); width: 500px; margin: 80px auto; padding: 0px; }
a { color: rgba(15, 10, 0, 0.8); text-decoration: none; border-bottom: 1px solid; padding: 1px 1px 0px; -webkit-transition: background 1s ease; }
a:hover { background: rgba(0, 220, 220, 0.2); }
p, li { line-height: 1.5; padding: 0em 1em 0em 0em; margin: 0em 0em 0.5em; }