Skip to content

Instantly share code, notes, and snippets.

@Ji-Yuhang
Ji-Yuhang / logger.rb
Created May 24, 2016 07:21
custom rails log by open class: ActiveSupport::Logger::SimpleFormatter
# You can enable this feature by: LOGGER=crazy rails s
# File : config/initializers/logger.rb
if ENV["LOGGER"] == "crazy"
class ActiveSupport::Logger::SimpleFormatter
def call(severity, time, progname, msg)
"#{severity} #{time}: #{progname} #{caller.grep(/phoenix/)} -- : #{msg}\n"
end
end
end
@Ji-Yuhang
Ji-Yuhang / ctrlp-ignore.vim
Created May 25, 2016 02:51
ctrlp-still-searches-the-ignored-directory
if has("mac")
let g:ctrlp_user_command = 'gfind %s -type f'
" Sane Ignore For ctrlp
endif
if exists("g:ctrlp_user_command")
unlet g:ctrlp_user_command
endif
set wildignore+=*\\vendor\\**
set wildignore+=*/tmp/*,*.so,*.swp,*.zip " MacOSX/Linux
set wildignore+=*\\tmp\\*,*.swp,*.zip,*.exe " Windows
@Ji-Yuhang
Ji-Yuhang / longest_common_substr.rb
Created June 23, 2016 01:58
longest common substring, 一组字符串中找出最长的相同部分
def longest_common_substr(strings)
shortest = strings.min_by &:length
maxlen = shortest.length
maxlen.downto(0) do |len|
0.upto(maxlen - len) do |start|
substr = shortest[start,len]
return substr if strings.all?{|str| str.include? substr }
end
end
end
@Ji-Yuhang
Ji-Yuhang / color_string.rb
Created July 6, 2016 09:25
ruby colorize string
class String
def black; "\e[30m#{self}\e[0m" end
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
def brown; "\e[33m#{self}\e[0m" end
def blue; "\e[34m#{self}\e[0m" end
def magenta;"\e[35m#{self}\e[0m" end
def cyan; "\e[36m#{self}\e[0m" end
def gray; "\e[37m#{self}\e[0m" end
end
@Ji-Yuhang
Ji-Yuhang / grape_format_with_if.rb
Created September 21, 2017 02:23
grape expose format_with: datetime, if: {type: :full}
module Entities::Admin
class OptionType < Grape::Entity
expose :id, documentation: {type: String, desc: 'ID'}
expose :name, documentation: {type: String, desc: '商品属性名称'}
expose :group_name, documentation: {type: String, desc: '属性群名称'}
expose :required, documentation: {type: Boolean, desc: '是否必填'}
expose :position, documentation: {type: Boolean, desc: '排序号'}
expose :phone_category_id, documentation: {type: String, desc: '商品类别ID'}, if: { type: :full }
expose :values, documentation: {type: String, desc: '商品属性值'}, if: { type: :full }
expose :category_names, documentation: {type: String, desc: '商品属性值'}, if: { type: :full }
@Ji-Yuhang
Ji-Yuhang / grape_format_with.rb
Created September 21, 2017 02:24
grape custom format
Grape::Entity.format_with :utc do |date|
date.strftime('%Y年%m月%d日 %H:%M') if date
end
Grape::Entity.format_with :date do |date|
date.strftime('%Y年%m月%d日') if date
end
Grape::Entity.format_with :datetime do |date|
date.strftime('%Y年%m月%d日 %H:%M:%S') if date
end
@Ji-Yuhang
Ji-Yuhang / mongo_id_get_all_model_names.rb
Created September 22, 2017 01:40
rails mongo get all model names
desc 'get Models'do
headers "Authentication-Token"=>{description: 'token',required: true }
end
get 'all_models' do
Rails.application.eager_load! if Rails.env.development?
models = Mongoid::Config.models
#fields.values.map{|f| o = f.options;{name: f.name,type: o[:type], kclass: o[:klass].to_s }}
present :models, models.map{|m|{model_name: m.to_s, relations: m.relations, field_names: m.fields.keys, fields: m.fields.values.map{|f| o = f.options;{name: f.name,type: o[:type].to_s, kclass: o[:klass].to_s }}}}
end
utf8_msg = msg.dup.force_encoding(Encoding::GBK).encode('utf-8') # GBK to UTF-8
@Ji-Yuhang
Ji-Yuhang / add_pg_trgm_extension_to_db.rb
Created October 17, 2017 05:29
pg_trgm gin postgre
class AddPgTrgmExtensionToDb < ActiveRecord::Migration[5.0]
# def change
# # execute "remove extension pg_trgm;"
# execute "create extension IF NOT EXISTS pg_trgm;"
# execute "CREATE EXTENSION IF NOT EXISTS btree_gin;"
#
# remove_index :words, :word
# add_index :words, :word, using: :btree_gin
# end
def up
@Ji-Yuhang
Ji-Yuhang / oracle_pagination.sql
Created April 11, 2019 11:30
oracle数据库在排序后分页需要3层select from
select OuterWrap.*
from (
SELECT ROWNUM as AfterOrderRowNum, Wrap.*
FROM (
SELECT "RechargeOrder"."CREATE_TIME" AS "RechargeOrder_CREATE_TIME" ,
ROWNUM AS "BeforeOrderRowNum"
FROM "T_RECHARGE_ORDER" "RechargeOrder"
ORDER BY "RechargeOrder"."CREATE_TIME" DESC
) Wrap