Skip to content

Instantly share code, notes, and snippets.

@khoan
khoan / gist:1390486
Created November 24, 2011 02:18
Rails 3.1 url helper in sprockets with relative_url_root
# So I was trying to use named route helpers in sprockets, and the app was hosted in a subdirectory.
# This is what I came up with. Please share your hackeries.
#
# using named routes in sprocket Rails 3.1
# you can use this in app/assets/javascripts/bam.js.erb
# or any other erb sprocket asset
Rails.application.routes.url_helpers.bam_path(
:script_name => ApplicationController.config.relative_url_root
)
@khoan
khoan / user.rb
Created July 20, 2012 02:33
Devise Authentication unscoped
# Our User model with default_scope of active users,
# and a :vip scope
#
class User
include Mongoid::Document
field :status
# now only active users can be authenticated
default_scope where(status: 'active')
@khoan
khoan / vip_authenticatable.rb
Created July 20, 2012 02:35
Devise Authentication unscoped
# lib/vip_authenticatable.rb
#
# We introduce :vip_authenticatable strategy to Warden.
# With this strategy your login form might look something like
#
# <form action="/path/to/create/session">
# <input name="vip[:id]" placeholder="Enter your VIP No.">
# <input type="submit">
# </form>
#
@khoan
khoan / devise.rb
Created July 20, 2012 02:36
Devise Authentication unscoped
# config/initializers/devise.rb
#
# We now ask Warden to use :vip_authenticatable when authenticating.
#
# Also, we instruct Warden to load User from session without any default scoping.
#
Devise.setup do |config|
config.warden do |manager|
require Rails.root+'lib/vip_authenticable'
manager.default_strategies(scope: :user).unshift :vip_authenticatable
@khoan
khoan / gist:3384201
Created August 18, 2012 03:44
Data Context Interaction in a minute
Caveat: I have not read James Coplien book about DCI.
But to put simply: Data Context Interaction (Roles) is a technique to capture the run-time behavior of your system. I'll leave the benefits to your imagination, and gleaning from further reading of numerous sources about the subject.
How do we apply DCI to Rails? Here's one proposed folder structure:
app/contexts/
app/roles/
app/models/
@khoan
khoan / gist:3426553
Created August 22, 2012 15:06
ruby defined? operator

Run this on ruby 1.9

require 'delegate'

class Bam < SimpleDelegator
  def bam
    defined? Bam
  end
@khoan
khoan / gist:4110151
Created November 19, 2012 11:11
rails eager load app/ directory
# ruby 1.9.3-p125-perf
# Rails 3.2.0
# app/bam/ham.rb
module Bam
class Ham
end
end
# on rails console
class SimulateTraffic
def initialize
@dates = (first_date_of_last_month..Date.today).to_a
@types = [:pageviews, :subscriptions, :shares, :clicks]
end
def first_date_of_last_month
prev_month = Date.today.prev_month
Date.new(prev_month.year, prev_month.month, 1)
$ cat ~/Library/LaunchAgents/org.memcached.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.memcached</string>
<key>KeepAlive</key>
<true/>
<key>ProgramArguments</key>
#!/bin/sh
#
# usage: heroku_pg_pull [appname] [local database name]
#
function heroku_pg_pull(){
echo "! WARNING: Data in the local database '$2' will be destroyed."
echo " Type '$2' to overwrite data in local database '$2'"
read -p "> " local_database_name
echo
if [ "$local_database_name" == "$2" ]; then