Skip to content

Instantly share code, notes, and snippets.

View chechaoyang's full-sized avatar

Che Chaoyang chechaoyang

View GitHub Profile
@chechaoyang
chechaoyang / regions.txt
Created March 22, 2013 04:04
中国省、市、区/县数据(截止2012年10月31日)
110000 北京市
110100 市辖区
110101 东城区
110102 西城区
110105 朝阳区
110106 丰台区
110107 石景山区
110108 海淀区
110109 门头沟区
110111 房山区
@chechaoyang
chechaoyang / clear_active_model_validators.rb
Created December 12, 2012 08:20
在某些特殊情况下,会出现这种需求:你希望清除掉 Model 中继承来的 validators。 (慎用!!小心因两个 Model 中的校验规则不一致,导致产生脏数据!) (activemodel 3.2.x)
# coding: utf-8
module ClearActiveModelValidators
extend ActiveSupport::Concern
module ClassMethods
# 清除指定属性上的 validators
# 参数说明(请全部使用 symbol):
# :all 清除所有属性上的 validators
# 属性名列表 清除指定属性上的 validators
#
@chechaoyang
chechaoyang / template_default.json
Created December 3, 2012 10:36
ElasticSearch source compress template: config/templates/
{
"template_default" : {
"template" : "*",
"mappings" : {
"_default_" : {
"_source" : {
"compress": true
}
}
}
@chechaoyang
chechaoyang / ruby_symbol_gc
Created July 27, 2012 04:31
Ruby Symbol Garbage Collection
1.9.3p194 :005 > hash = { test_symbol_gc: "test" }
=> {:test_symbol_gc=>"test"}
1.9.3p194 :006 > Symbol.all_symbols.find { |s| s.to_s == "test_symbol_gc" }
=> :test_symbol_gc
1.9.3p194 :007 > hash = nil
=> nil
1.9.3p194 :008 > GC.start
=> nil
1.9.3p194 :009 > Symbol.all_symbols.find { |s| s.to_s == "test_symbol_gc" }
=> :test_symbol_gc
class AlterUsersIdStartFrom10001 < ActiveRecord::Migration
def self.up
execute "ALTER TABLE users AUTO_INCREMENT = 10001;"
end
def self.down
execute "ALTER TABLE users AUTO_INCREMENT = 1;"
end
end
@chechaoyang
chechaoyang / session_store.rb
Created September 23, 2011 06:34
Uploadify with active record sessions store in Rails 3
#
# 这只是针对active_record_store情况下要做的修改,完整的cookie_store mode看这里
# https://github.com/websymphony/Rails3-Paperclip-Uploadify
#
Uploader::Application.config.session_store :active_record_store, :key => '_uploader_session'
Rails.application.config.middleware.insert_before(
Rails.application.config.session_store,
FlashSessionCookieMiddleware,
@chechaoyang
chechaoyang / watermark.rb
Created May 5, 2011 09:36
Paperclip Watermarking
# from https://github.com/mpelos/nova_marine/blob/2dc882dac31532024d4edb196e4a632801738341/lib/paperclip_processors/watermark.rb
# Rails.root /lib/paperclip_processors/watermark.rb
module Paperclip
class Watermark < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
@watermark_path = options[:watermark_path]
@position = options[:position].nil? ? "SouthEast" : options[:position]
end