Skip to content

Instantly share code, notes, and snippets.

@rainchen
rainchen / gist:6363948
Created August 28, 2013 09:13
Install specific version of Homebrew formula
# Install specific version of Homebrew formula
# usage example:
# $ brew-install ssh-copy-id 6.0p1
function brew-install {
local formula version formula_hash
formula=$1
version=$2
cd `brew --prefix`
git_last_commit_id=`git rev-parse --short HEAD`
@rainchen
rainchen / deploy.rb
Last active December 18, 2015 16:29
private_pub capistrano tasks
namespace :private_pub do
desc "Start private_pub server"
task :start do
run "cd #{current_path};RAILS_ENV=#{rails_env} bundle exec rackup private_pub.ru -s thin -E #{rails_env} -D -P tmp/pids/private_pub.pid"
end
desc "Stop private_pub server"
task :stop do
run "cd #{current_path};if [ -f tmp/pids/private_pub.pid ] && [ -e /proc/$(cat tmp/pids/private_pub.pid) ]; then kill -9 `cat tmp/pids/private_pub.pid`; fi"
end
@rainchen
rainchen / photo.rb
Created June 5, 2013 04:43
Clever enums in rails using module style
class Photo < ActiveRecord::Base
module Statues
QUEUED = 'queued'
NEW = 'new'
def self.values
constants.map(&method(:const_get))
end
# you can define more special methods as you wanted for Statues
end

Capybara

save_and_open_page

Matchers

have_button(locator)
data:text/html, <style type="text/css">#e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
@rainchen
rainchen / admin_user.rb
Created November 16, 2012 02:05
add auto complete in ActiveAdmin
ActiveAdmin.register AdminUser do
# define routes for "autocomplete :admin_user, :email"
collection_action :autocomplete_admin_user_email, :method => :get
controller do
autocomplete :admin_user, :email
end
index do
column :email
@rainchen
rainchen / formtastic_localizer_patch.rb
Created September 25, 2012 04:11
this patch allowed to use a proc to :label option, then we can pass a proc to ActiveAdmin `filter` method
# ActiveAdmin use Formtastic as the "filter" field builder
# this patch allowed to use a proc to :label option, then we can pass a proc to `filter` method
# e.g.: filter :foo, :label => proc { I18n.tr('activerecord.attributes.foo.bar') }
module Formtastic
class Localizer
def localize_with_proc_value(key, value, type, options = {})
if value.is_a?(::Proc)
value.call(key)
else
localize_without_proc_value(key, value, type, options = {})
@rainchen
rainchen / methodfinder_what.rb
Created September 17, 2012 08:03 — forked from wtaysom/methodfinder_what.rb
_why's Ruby MethodFinder addition tweaked and revised.
require 'methodfinder'
# _why's MethodFinder addition tweaked and revised.
# <http://redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.html>
class MethodFinder
def self.with_redirected_streams
redirect_streams
yield
ensure
RewriteRule ^/jobs/([^-^/]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*) /house/$8/trade=$1&category=$2&subclass=$3&district=$4&education=$5&education=$6&experience=$7 [C,NC]
RewriteRule ^/jobs/([^-^/]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)/(.*) jobs-list.php?$6&wage=$1&nature=$2&searchtype=$3&key=$4&page=$5 [QSA,L,NC]
// Fixed for prototype extend issue
function hackjQuery(jQuery){
if(jQuery.fn.jquery == '1.3.2'){
jQuery.event.handle = function(event) {
// returned undefined or false
var all, handlers;
event = arguments[0] = jQuery.event.fix( event || window.event );
event.currentTarget = this;