Skip to content

Instantly share code, notes, and snippets.

View lingceng's full-sized avatar

Kexian Zhong lingceng

View GitHub Profile
@lingceng
lingceng / plain_chatgpt_client.rb
Created May 29, 2023 00:34
Ruby chatgpt chat api when stream mode, deal with error handle and chunk break.
class Chatgpt::PlainChatgptClient
def self.chat(parameters)
@client ||= generate_client
res = @client.post("/v1/chat/completions") do |req|
if parameters[:stream].is_a?(Proc)
req.options.on_data = to_json_stream(user_proc: parameters[:stream])
parameters[:stream] = true
end
@lingceng
lingceng / importmap.rb
Created May 28, 2023 10:05
Dynamic import highlight.js language in Rails7
# For code syntax highlight
pin "highlight.js/lib/core", to: "https://ga.jspm.io/npm:highlight.js@11.8.0/lib/core.js"
common_langs = ['xml', 'bash', 'c', 'cpp', 'csharp', 'css', 'markdown', 'diff', 'ruby',
'go', 'graphql', 'ini', 'java', 'javascript', 'json', 'kotlin', 'less',
'lua', 'makefile', 'perl', 'objectivec', 'php', 'php-template', 'plaintext',
'python', 'python-repl', 'r', 'rust', 'scss', 'shell', 'sql', 'swift', 'yaml',
'typescript', 'vbnet', 'wasm']
common_langs.each do |lang|
pin "highlight.js/lib/languages/#{lang}", to: "https://ga.jspm.io/npm:highlight.js@11.8.0/lib/languages/#{lang}.js"
end
@lingceng
lingceng / clearAliveCache.js
Created November 27, 2019 09:08
Function to clear vue keep-alive cache
function remove (arr, item) {
if (arr.length) {
let index = arr.indexOf(item);
if (index > -1) {
return arr.splice(index, 1)
}
}
}
function pruneCacheEntry (cache, key, keys, current) {
@lingceng
lingceng / convert_time_zone.rb
Created May 14, 2018 09:41
Rails connect another database tips.
# app/models/concerns/convert_time_zone.rb
module ConvertTimeZone
extend ActiveSupport::Concern
included do
date_columns = columns.select { |column| column.sql_type == 'datetime' }.map(&:name)
convert_time_zone date_columns
def timestamp_attributes_for_update
[:last_updated]
@lingceng
lingceng / select2_input_tags.js
Last active July 18, 2017 11:32
Make select2 4.0 above tags works with text input field
// https://gist.github.com/lingceng/d1cd5ca5db9a777b31487769242c422f
// See the issue here: https://github.com/select2/select2/issues/3022
function select2InputTags(queryStr) {
var $input = $(queryStr)
var $select = $('<select class="'+ $input.attr('class') + '" multiple="multiple"><select>')
if ($input.val() != "") {
$input.val().split(',').forEach(function(item) {
$select.append('<option value="' + item + '" selected="selected">' + item + '</option>')
@lingceng
lingceng / prepare-commit-msg
Created April 1, 2016 09:22
Add recent logs to commit message. Put file at .git/hooks/prepare-commit-msg
#!/usr/bin/env ruby
template = ARGV[0]
File.open(template, "a") do |file|
logs = `git log -n 10 --oneline`
logs = logs.each_line.map { |line| '# ' + line }.join()
file.puts logs
end
@lingceng
lingceng / .vimrc
Created April 1, 2016 09:21
Send text to tmux
Plugin 'benmills/vimux'
function! VimuxSlime()
call VimuxOpenRunner()
let lines = split(@v, '\n')
let text = ''
let index = 0
while index < len(lines)
let line = lines[index]
let l:text = l:text . line
@lingceng
lingceng / SyntaxInclude.vim
Last active February 1, 2016 10:16
Vim slime embeded coffee sass syntax. Put at ~/.vim/after/syntax/slim/SyntaxInclude.vim
" Require SyntaxRange vim plugin
call SyntaxRange#IncludeEx('matchgroup=NonText keepend start="^sass" end="^\S"he=e-1,re=e-1 containedin=ALL', 'sass')
call SyntaxRange#IncludeEx('matchgroup=NonText keepend start="^coffee" end="^\S"he=e-1,re=e-1 containedin=ALL', 'coffee')
@lingceng
lingceng / rails_callbacks.rb
Created January 22, 2016 09:07
Test rails callbacks order
# https://github.com/rails/rails/pull/15106
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
@lingceng
lingceng / index.html.erb
Last active December 22, 2017 10:45
Custom ransacker predicates to query date range
<%= search_form_for @q, url: production_status_changes_path, class: 'form-inline' do |f| %>
<%= f.label 'Create At' %>
<%= f.search_field :created_at_end_of_day_lteq, class: 'form-control input-sm', 'datepicker' => true %>
<% end %>