Skip to content

Instantly share code, notes, and snippets.

View pobing's full-sized avatar

Jed pobing

View GitHub Profile
@pobing
pobing / gist:f546ecf643f9a35f613c
Created June 6, 2014 02:13
Get key value from Yaml file on different environment
# encoding: utf-8
module Setting
class << self
@@hashes = {}
# environments: development, test and production.
def environments()
%w[test production development]
@pobing
pobing / gist:f8d5662e0746bcc4c7da
Created August 15, 2014 03:47
Generate random str(slug) with ruby
def slug(length=10)
chars = ('A'..'Z').to_a + ('a'..'z').to_a + (0..9).to_a
chars.sample(length).join
end
#usage:
slug
=> "x5ZlWb4Ymf"
slug(6)
@pobing
pobing / gist:98cf74905abbd350e285
Created August 19, 2014 06:49
ruby readline csv file and create new file with sth
require 'csv'
task :token do
require "./config/boot.rb"
res_file = File.new("db/token.txt","w+") # create res file
CSV.foreach("db/token.csv") do |row|
access_token = row.split(';')[0] # get first element
oauth_token = OauthToken.first(:access_token => access_token)
if oauth_token
client_id = oauth_token.client_id
@pobing
pobing / gist:48b37dcd69e408036b1f
Created October 22, 2014 03:05
clockwork use demo
#encoding: utf-8
# https://github.com/tomykaira/clockwork
# Usage:
# clockwork workers/clock_worker.rb & or
# clockworkd -c workers/clock_worker.rb run
require 'clockwork'
require File.expand_path('../../config/boot', __FILE__)
ENV['RACK_ENV'] = 'test'
@pobing
pobing / gist:5ab97690230cd564241c
Created December 24, 2014 07:53
Ruby query db res and generate csv file
# encoding: utf-8
require 'csv'
desc 'Get all user email'
task :send_coupon do
date = Time.now.strftime("%Y%m%d%H%M%S")
path = "tmp/send_coupon_emails_#{date}.csv"
CSV.open(path, "wb") do |csv|
header = ["email","created_at","survey_count","passport_id"]
@pobing
pobing / gist:b7b3bba28c7b76b61ba6
Last active August 29, 2015 14:24
ruby tips 1
#flatten.uniq 去数组中的重复元素
@my_orgunits = (current_user.orgunits + current_user.groups+ current_user.watch_open_groups).flatten.uniq
# use db
ActiveRecord::Base.connection.execute("use db")
# gsub
Mblog.find_each do |mblog|
unless mblog.content.nil?
mblog.content=mblog.content.gsub(/\[(\w+)\]/) do |c|
@pobing
pobing / checkboxSelect.coffee
Last active February 2, 2016 06:57
js checkAll or subCheck
attached: ->
$subQuestion = @$("input[name='question_ids[]']")
checkedLengh = @$("input[name='question_ids[]']:checked").length
allChecked = checkedLengh > 0 and $subQuestion.length is checkedLengh
@$(".question-check-all").prop("checked", allChecked)
checkAll: (e) ->
checkboxes = $('input[name="question_ids[]"]')
_.each checkboxes, (box) =>
@pobing
pobing / jsArraySort.coffee
Created February 2, 2016 06:55
js array sort by two propertys
comparator: (prev,next) ->
return 1 if prev.get('page_number') > next.get('page_number')
return -1 if prev.get('page_number') < next.get('page_number')
if prev.get('page_number') is next.get('page_number')
return prev.get('number') - next.get('number')
return 0
@keyframes spin {
0% {
transform:rotate(0deg);
}
100% {
transform:rotate(360deg);
}
}
.animate-spin {
@pobing
pobing / rails_template.rb
Created March 23, 2017 10:14
use rails template create project
## Gem part
remove_file 'Gemfile'
run 'touch Gemfile'
add_source 'https://gems.ruby-china.org'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'