Skip to content

Instantly share code, notes, and snippets.

@HungYuHei
HungYuHei / jquery-scroll
Created August 31, 2011 11:39
jquery scroll
$('html,body').animate({scrollTop: 540}, 700);
@HungYuHei
HungYuHei / rvm-ruby-dependencies
Created August 31, 2011 11:44
rvm ruby dependencies
# For RVM
yum install -y bash curl git libxslt # NOTE: For git you need the EPEL repository enabled
apt-get install bash curl git-core
# OS => Redhat/CentOS/Fedora
yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel
yum install -y make bzip2
yum install -y iconv-devel # NOTE: For centos 5.4 final iconv-devel might not be available :(
# OS => Debian/Ubuntu
@HungYuHei
HungYuHei / reject-blank-params
Created September 5, 2011 12:53
reject-blank-params
MODEL.where(params[:student].select{|k,v| !v.blank?})
MODEL.where(params[:student].reject{|k,v| v.blank?})
@HungYuHei
HungYuHei / gist:1199677
Created September 7, 2011 03:24
reject-blank
Array.reject(&:blank?)
@HungYuHei
HungYuHei / import_file_to_gridfs.rake
Created September 8, 2011 06:19 — forked from huacnlee/import_file_to_gridfs.rake
将 File System 的上传文件导入到 Mongodb 的 GridFS 里面
# coding: UTF-8
#
# 以下为 Rake 任务,功能是将普通文件系统里面的东西转移到 MongoDB GridFS 里面
# 此代码片段来自于 Homeland 项目: https://github.com/huacnlee/homeland/tree/mysql
# 场景:
# 老架构 Linux File Store, Paperclip, hash 目录:"https://github.com/huacnlee/homeland/blob/ca0bdd8ab26da7b780e2dae7eba12b79f41e6d65/config/initializers/paperclip_hashpath.rb"
# 新架构 Mongodb GridFS, Garrierwave, 继续沿用 Paperclip 目录兼容: https://github.com/huacnlee/homeland/tree/7100ce4c506cc2c4387f25e50c533e5bbcac6cc2/app/uploaders
# 整个过程不会修改任何原始数据库和上传文件
#
require 'mongo'
@HungYuHei
HungYuHei / gist:1203663
Created September 8, 2011 15:22
nested_attributes_accesible
has_many :doors
accepts_nested_attributes_for :doors
attr_accesible :address, :doors_attributes
# 注意是复数
@HungYuHei
HungYuHei / .gitconfig
Created September 9, 2011 15:41
Useful gitconfig aliases
[alias]
st = status
rso = remote show origin
# e.g. git graphviz --first-parent master | dotty /dev/stdin
graphviz = "!f() { echo 'digraph git {' ; git log --pretty='format: %h -> { %p }' \"$@\" | sed 's/[0-9a-f][0-9a-f]*/\"&\"/g' ; echo '}'; }; f"
# Pretty log
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
# Print out all commits whose hash starts with a given string
abbr = "!sh -c 'git rev-list --all | grep ^$1 | while read commit; do git --no-pager log -n1 --pretty=format:\"%H %ci %an %s%n\" $commit; done' -"
# Two little tweaks to diff
@HungYuHei
HungYuHei / gist:1206810
Created September 9, 2011 17:25
ruby openssl or readline loaderror
安装了rvm后,openssl和readline LoadError
当不能load openssl的时候,用以下方法解决
$ sudo apt-get install libssl-dev
$ cd ~/.rvm/src/ruby-1.8.7/ext/openssl
$ ruby extconf.rb
$ make && make install
@HungYuHei
HungYuHei / HABTM Checkboxes
Created September 15, 2011 06:53
has_and_belongs_to checkbox
// VIEW:
<% for category in Category.find(:all) %>
<div>
<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
<%= category.name %>
</div>
<% end %>
// CONTROLLER:
@HungYuHei
HungYuHei / .gitconfig
Created September 16, 2011 10:09
gitconfig
[core]
editor = vim
[user]
name = HungYuHei
email = [email protected]
[alias]
aa = add .
st = status
ss = stash
sss = stash save