Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
jhjguxin / jquery.parseparams.js
Last active August 29, 2015 13:59 — forked from kares/jquery.parseparams.js
This function is meant to be used with an URL like the window.location
// Add an URL parser to JQuery that returns an object
// This function is meant to be used with an URL like the window.location
// Use: $.parseParams('http://mysite.com/?var=string') or $.parseParams() to parse the window.location
// Simple variable: ?var=abc returns {var: "abc"}
// Simple object: ?var.length=2&var.scope=123 returns {var: {length: "2", scope: "123"}}
// Simple array: ?var[]=0&var[]=9 returns {var: ["0", "9"]}
// Array with index: ?var[0]=0&var[1]=9 returns {var: ["0", "9"]}
// Nested objects: ?my.var.is.here=5 returns {my: {var: {is: {here: "5"}}}}
// All together: ?var=a&my.var[]=b&my.cookie=no returns {var: "a", my: {var: ["b"], cookie: "no"}}
// You just cant have an object in an array, ?var[1].test=abc DOES NOT WORK
Array.prototype.each_slice = function (size, callback){
for (var i = 0, l = this.length; i < l; i += size){
callback.call(this, this.slice(i, i + size));
}
};
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each_slice(3, function (slice){
console.log(slice);
});
@jhjguxin
jhjguxin / active_record_join_dup.md
Created August 26, 2014 09:45
Preload, Eagerload, Includes and Joins
user_id = 11

Task.joins("left JOIN ownerships ON ownerships.subject_id = tasks.id AND ownerships.subject_type = 'Task'").where("ownerships.subject_type" => "Task", user_id: 11).select("distinct '#{subject_klass.table_name}.id'").select("#{subject_klass.table_name}.*")

Task.joins("left JOIN ownerships ON ownerships.subject_id = tasks.id AND ownerships.subject_type = 'Task'").where("ownerships.subject_type" =&gt; "Task", user_id: 11).group("#{subject_klass.table_name}.id")
@jhjguxin
jhjguxin / active_record_timezone.md
Created September 4, 2014 03:35
active record timezone without rails

这次应该搞定了

Francis.J 2014-9-4 11:30:02

zone_default = Time.find_zone!("Asia/Shanghai")
Time.zone_default = zone_default

ActiveRecord::Base.time_zone_aware_attributes = true
ActiveRecord::Base.default_timezone = :utc
@jhjguxin
jhjguxin / production.rb
Created September 19, 2014 03:59
how to config log level within production env rails + unicorn
# Set to :debug to see everything in the log.
config.log_level = ENV["LOG_LEVEL"].present? ? ENV["LOG_LEVEL"] : :info
@jhjguxin
jhjguxin / gitlab.md
Last active August 29, 2015 14:07
gitlab deploy tips
@jhjguxin
jhjguxin / elasticsearch_cn.md
Last active August 29, 2015 14:08
config zh-cn elasticsearch plugin
https://github.com/medcl/elasticsearch-analysis-ik/archive/v1.2.6.zip
unzip v1.2.6.zip


sudo -i

cd /usr/share/elasticsearch
cd plugins
bin/plugin -install medcl/elasticsearch-analysis-ik/1.2.6
@jhjguxin
jhjguxin / remote_debug_android_chrome_on_linux.md
Last active August 29, 2015 14:13
remote debug mobile web page on linux through chrome
chrome://inspect/#devices
localhost:8999

chrome://flags/#enable-devtools-experiments

http://www.html5rocks.com/en/tutorials/developertools/revolutions2013/?redirect_from_locale=zh
adb forward tcp:8999 localabstract:chrome_devtools_remote

http://stackoverflow.com/questions/3877027/jquery-callback-on-image-load-even-when-the-image-is-cached
@jhjguxin
jhjguxin / __init__.py
Created May 24, 2012 15:17 — forked from dongyi/__init__.py
OAuth包,实现了sina,QQ,网易,搜狐的OAuth认证
# -*- Encoding: utf-8 -*-
import base64
import binascii
import cgi
import hashlib
import hmac
import logging
import time
import urllib
import urlparse