Skip to content

Instantly share code, notes, and snippets.

View huacnlee's full-sized avatar

Jason Lee huacnlee

View GitHub Profile
@huacnlee
huacnlee / nginx_location_example.conf
Created March 7, 2013 08:27
Nginx 配置支持 Rails Page Caching
location / {
if ($request_method = POST) {
proxy_pass http://maclabs_backend;
break;
}
if (-f $document_root/$uri/index.html) {
rewrite (.*) /$1/index.html break;
}
@huacnlee
huacnlee / gist:3936299
Created October 23, 2012 02:19
Mac 清理 TextMate 2 不断升级带出一堆多余的打开方式
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
@huacnlee
huacnlee / _form.html.erb
Created August 21, 2012 10:55
UpYun 表单上传验证信息 helper 方法,用于生成 input tag
<form action="http://v0.api.upyun.com/rbtest/" method="post" enctype="multipart/form-data">
<%= upyun_form_input_tag(:return_url => new_post_path) %>
<input type="file" name="file" />
<button type="submit">上传图片</button>
</form>
@huacnlee
huacnlee / ssi_helper.rb
Created July 17, 2012 06:46
render SSI
# Render SSI
def render_ssi(path)
if Rails.env.development?
elid = "ssi_#{Digest::MD5.hexdigest(path)}"
raw %(
<div id="#{elid}"></div>
<script type="text/javascript">
$.get("#{path}", function(re){
$("##{elid}").html(re);
})
@huacnlee
huacnlee / in_rails3.rb
Created May 24, 2012 13:24
fluent-rails3-log
class Rails3Input < Fluent::TailInput
Fluent::Plugin.register_input('rails3', self)
IP_ADDRESS_REGEX = %r!\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}!
def configure_parser(conf)
@_time = Time.now.to_i
@_record = {}
end
def parse_line(line)
if line.start_with?("Started")
@huacnlee
huacnlee / easy_captcha.rb
Created May 4, 2012 10:10
easy_captcha config 调整好的样式
EasyCaptcha.setup do |config|
# Cache
config.cache = true
# Cache temp dir from Rails.root
config.cache_temp_dir = Rails.root + 'tmp' + 'captchas'
# Cache size
config.cache_size = 10
# Cache expire
config.cache_expire = 10.seconds
@huacnlee
huacnlee / em-synchrony-demo.rb
Created April 5, 2012 14:27
EventMachine synchrony fetch url
require "em-synchrony"
require "em-synchrony/em-http"
EM.synchrony do
concurrency = 10
urls = %w(taobao.com 163.com ifanr.com ruby-china.org v2ex.com github.com google.com baidu.com).collect { |w| "http://#{w}" }
# iterator will execute async blocks until completion, .each, .inject also work!
EM::Synchrony::Iterator.new(urls, concurrency).map do |url, iter|
@huacnlee
huacnlee / will_paginate.js
Created March 23, 2012 07:48
will_paginate.js for Rails template
// Keyboard shortcuts for browsing pages of lists
(function($) {
$(document).keydown(handleKey);
function handleKey(e) {
var left_arrow = 37;
var right_arrow = 39;
if (e.target.nodeName == 'BODY' || e.target.nodeName == 'HTML') {
if (!e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
var code = e.which;
if (code == left_arrow) {
@huacnlee
huacnlee / rails_template_for_mongoid.rb
Created March 23, 2012 07:34
A custom Rails template for Mongoid.
# coding: utf-8
# This is a Rails init template with MongoDB projects
#
# Mongoid, Devise, Bootstrap, jQuery, Redis, Cells, will_paginate, Carrierwave, simple_form, Settingslogic, Thin
#
# Usage
#
# $ rails new app_name -m https://raw.github.com/gist/2168014
#
@huacnlee
huacnlee / base_uploader.rb
Created March 23, 2012 07:29
Carrierwave Uploader base class for Rails template
# encoding: utf-8
require 'carrierwave/processing/mini_magick'
class BaseUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"#{model.class.to_s.underscore}"
end