Skip to content

Instantly share code, notes, and snippets.

View hungmi's full-sized avatar
🏠
Working from home

Hungmi hungmi

🏠
Working from home
View GitHub Profile
@hungmi
hungmi / hello.html
Created May 4, 2024 09:13
Turbo broadcast_*_to via ActionCable
<!-- app/views/hello.html -->
<h1>Hello!</h1>
module WebI18nLocale
extend ActiveSupport::Concern
included do
prepend_before_action :digest_locale_params
before_action :set_locale
helper_method :set_locale_path
end
private
@hungmi
hungmi / galleries.html.erb
Last active January 21, 2022 17:17
Asynchronously load images with Stimulus.js. Also supports media queries.
<% # I have a slide contains 6 images. %>
<% # And also I have different versions of them based on media queries. %>
<% # We can load the 2nd to 6th images asynchronously. %>
<% (2..6).each do |num| %>
<div data-controller="preload-img" data-preload-img-src-value='<%= asset_path("slides/pc/#{num}.jpg") %>' data-preload-img-media-value="(min-width: 992px)"></div>
<div data-controller="preload-img" data-preload-img-src-value='<%= asset_path("slides/mobile/#{num}.jpg") %>' data-preload-img-media-value="(max-width: 991px)"></div>
<% end %>
import TwCitySelector from 'tw-city-selector/dist/tw-city-selector.min.js'
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
let controller = this
new TwCitySelector({
el: '.city-selector-set',
elCounty: '.county',
elDistrict: '.district',
@hungmi
hungmi / action_text_parser.rb
Last active June 16, 2021 17:39
Rails ActionText: support prism syntax highlight code blocks. And transform <action-text-attachment/> to <img/> to work with any frontend.
# lib/action_text_parser.rb
# require 'action_text_parser' in application.rb
class ActionTextParser
def self.run(action_text_record)
html = action_text_record.body.to_html
nodes = Nokogiri::HTML::DocumentFragment.parse html
nodes = self.parse_attachments(nodes)
nodes = self.parse_code(nodes)
nodes.to_html
end
@hungmi
hungmi / demo.js
Last active December 18, 2019 08:08
Rails 4 with jquery-ujs: event listeners for remote link js success and error events
//// For Rails projects with rails-ujs Check official guide: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers
// In xxx.html.erb, we can write
// <%= link_to 'xxx', url, remote: true, data: { type: :json, disalbe_with: "...", confirm: "ok?" }, class: 'js-remote-link' %>
$(document).on("ajax:success", ".js-remote-link", function(e) {
var data = event.detail[0]
var status = event.detail[1]
var xhr = event.detail[2]
console.log(data) // {message: "SUCCESS &check;"}
console.log(status) // OK
@hungmi
hungmi / puma.service of RVM
Last active March 13, 2020 14:14
Use systemd to restart puma after reboot. Put this file at /etc/systemd/system/puma.service. Please check those paths in ExecStart and ExecStop carefully.
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/apps/shiftweb/current
Environment=RAILS_ENV=production
ExecStart=/home/deploy/.rvm/bin/rvm ruby-2.6.5 do bundle exec puma -C /home/deploy/apps/shiftweb/shared/puma.rb
@hungmi
hungmi / edit.html.erb
Last active February 6, 2018 16:10
say you have a model named Article, and you wanna use Quilljs to edit it's content.
<!-- Include Quill stylesheet -->
<link href="https://cdn.quilljs.com/1.3.5/quill.snow.css" rel="stylesheet">
<div id="form-container">
<%= form_for @portfolio do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
@hungmi
hungmi / DomainName
Last active October 12, 2018 08:19
/etc/nginx/sites-available/DomainName
# config/nginx.conf
upstream puma {
server unix:///home/deploy/apps/AppName/shared/tmp/sockets/puma.sock;
}
server {
server_name example.com www.example.com.tw;
root /home/deploy/apps/AppName/current/public;
access_log /home/deploy/apps/AppName/current/log/nginx.access.log;
error_log /home/deploy/apps/AppName/current/log/nginx.error.log info;
@hungmi
hungmi / options-ssl-nginx.conf
Last active August 29, 2018 01:21
/etc/letsencrypt/options-ssl-nginx.conf
# /etc/letsencrypt/options-ssl-nginx.conf
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1440m;
ssl_session_cache shared:ssl_session_cache:10m;
ssl_session_tickets off;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';