Skip to content

Instantly share code, notes, and snippets.

@rainchen
rainchen / gist:6363948
Created Aug 28, 2013
Install specific version of Homebrew formula
View gist:6363948
# 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 Dec 18, 2015
private_pub capistrano tasks
View deploy.rb
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 Jun 5, 2013
Clever enums in rails using module style
View photo.rb
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
View capybara.md

Capybara

save_and_open_page

Matchers

have_button(locator)
View Ruby Notepad Bookmarklet
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 Nov 16, 2012
add auto complete in ActiveAdmin
View admin_user.rb
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 Sep 25, 2012
this patch allowed to use a proc to :label option, then we can pass a proc to ActiveAdmin `filter` method
View formtastic_localizer_patch.rb
# 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 Sep 17, 2012 — forked from wtaysom/methodfinder_what.rb
_why's Ruby MethodFinder addition tweaked and revised.
View methodfinder_what.rb
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
View gist:854664
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]
View gist:464231
// 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;