Skip to content

Instantly share code, notes, and snippets.

View xiaohui-zhangxh's full-sized avatar

xiaohui xiaohui-zhangxh

  • Tanmer Inc.
  • 中国
View GitHub Profile
@xiaohui-zhangxh
xiaohui-zhangxh / create_engine
Last active March 6, 2023 06:00
Rails 项目在 engines/ 目录下生成子模块 engine
#!/usr/bin/env ruby
name = ARGV[0]
if name.to_s.strip.empty?
puts "usage: bin/create_engine <name>"
exit(1)
end
cmd = <<-CMD
bin/rails plugin new engines/#{name}_engine \
@xiaohui-zhangxh
xiaohui-zhangxh / liquid_template_tags_thread_safe.rb
Last active March 9, 2023 02:22
Make liquid template's tags to be thread safe
# 使用方法:
#
# Liquid::Template.include Liquid::TemplateTagsThreadSafe
#
# Liquid::Template.with_thread_safe do
# Liquid::Template.register_tag('test', Liquid::Tag)
# Liquid::Template.tags['test'] # => Liquid::Tag
# end
#
# Liquid::Template.tags['test'] # => nil
@xiaohui-zhangxh
xiaohui-zhangxh / application_controller.rb
Last active November 19, 2024 03:07
Make active_model_serializers render Pagy result as jsonapi response, with pagination meta and links
class Api::ApplicationController < ActionController::API
include Pagy::Backend
include PagyCollectionSerializer::ControllerMixin
end
@xiaohui-zhangxh
xiaohui-zhangxh / dynamic_cookie_store.rb
Created December 31, 2023 10:49
Make dymanic session store key for Rails on Multi-Tenant Arch
@xiaohui-zhangxh
xiaohui-zhangxh / responsive_attr_controller.js
Last active February 28, 2025 02:51
Responsive HTML Attributes like TailwindCSS (StimulusJS Controller)
import { Controller } from "@hotwired/stimulus";
/**
* 响应式属性控制器 / Responsive Attribute Controller
*
* 这个控制器用于根据屏幕尺寸动态改变元素的属性值。
* This controller is used to dynamically change element attributes based on screen size.
*
* ## 使用方法 / Usage
*
* 1. 在父元素上添加控制器 / Add controller to parent element: